attila@963: #// Usage: jjs javafoovars.js -- attila@963: attila@963: /* attila@963: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. attila@963: * attila@963: * Redistribution and use in source and binary forms, with or without attila@963: * modification, are permitted provided that the following conditions attila@963: * are met: attila@963: * attila@963: * - Redistributions of source code must retain the above copyright attila@963: * notice, this list of conditions and the following disclaimer. attila@963: * attila@963: * - Redistributions in binary form must reproduce the above copyright attila@963: * notice, this list of conditions and the following disclaimer in the attila@963: * documentation and/or other materials provided with the distribution. attila@963: * attila@963: * - Neither the name of Oracle nor the names of its attila@963: * contributors may be used to endorse or promote products derived attila@963: * from this software without specific prior written permission. attila@963: * attila@963: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS attila@963: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, attila@963: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR attila@963: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR attila@963: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, attila@963: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, attila@963: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR attila@963: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF attila@963: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING attila@963: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS attila@963: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. attila@963: */ attila@963: attila@963: // This example demonstrates Java subclassing by Java.extend attila@963: // and javac Compiler and Tree API. This example counts number attila@963: // of variables called "foo" in the given java source files! attila@963: if (arguments.length == 0) { attila@963: print("Usage: jjs javafoovars.js -- "); attila@963: exit(1); attila@963: } attila@963: attila@963: // Java types used attila@963: var File = Java.type("java.io.File"); attila@963: var Files = Java.type("java.nio.file.Files"); attila@963: var StringArray = Java.type("java.lang.String[]"); attila@963: var ToolProvider = Java.type("javax.tools.ToolProvider"); attila@963: var Tree = Java.type("com.sun.source.tree.Tree"); attila@963: var TreeScanner = Java.type("com.sun.source.util.TreeScanner"); attila@963: var VariableTree = Java.type("com.sun.source.tree.VariableTree"); attila@963: attila@963: // count "foo"-s in the given .java files attila@963: function countFoo() { attila@963: // get the system compiler tool attila@963: var compiler = ToolProvider.systemJavaCompiler; attila@963: // get standard file manager attila@963: var fileMgr = compiler.getStandardFileManager(null, null, null); attila@963: // Using Java.to convert script array (arguments) to a Java String[] attila@963: var compUnits = fileMgr.getJavaFileObjects( attila@963: Java.to(arguments, StringArray)); attila@963: // create a new compilation task attila@963: var task = compiler.getTask(null, fileMgr, null, null, null, compUnits); attila@963: // subclass SimpleTreeVisitor - to count variables called "foo" attila@963: var FooCounterVisitor = Java.extend(TreeScanner); attila@963: var fooCount = 0; attila@963: attila@963: var visitor = new FooCounterVisitor() { attila@963: visitVariable: function (node, p) { attila@963: if (node.name.toString() == "foo") { attila@963: fooCount++; attila@963: } attila@963: } attila@963: } attila@963: attila@963: for each (var cu in task.parse()) { attila@963: cu.accept(visitor, null); attila@963: } attila@963: return fooCount; attila@963: } attila@963: attila@963: // for each ".java" file in directory (recursively) count "foo". attila@963: function main(dir) { attila@963: var totalCount = 0; sundar@1531: Files.walk(dir.toPath()). attila@963: forEach(function(p) { attila@963: var name = p.toFile().absolutePath; attila@963: if (name.endsWith(".java")) { attila@963: var count = 0; attila@963: try { attila@963: count = countFoo(p.toFile().getAbsolutePath()); attila@963: } catch (e) { attila@963: print(e); attila@963: } attila@963: if (count != 0) { attila@963: print(name + ": " + count); attila@963: } attila@963: totalCount += count; attila@963: } attila@963: }); attila@963: print("Total foo count: " + totalCount); attila@963: } attila@963: attila@963: main(new File(arguments[0]));