samples/javafoovars.js

Fri, 05 Jun 2015 12:38:53 +0200

author
mhaupt
date
Fri, 05 Jun 2015 12:38:53 +0200
changeset 1398
2f1b9f4daec1
parent 963
e2497b11a021
child 1531
ab48ce00c634
permissions
-rw-r--r--

8080087: Nashorn $ENV.PWD is originally undefined
Summary: On Windows, the PWD environment variable does not exist and cannot be imported in scripting mode, so it is set explicitly.
Reviewed-by: lagergren, sundar

attila@963 1 #// Usage: jjs javafoovars.js -- <directory>
attila@963 2
attila@963 3 /*
attila@963 4 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
attila@963 5 *
attila@963 6 * Redistribution and use in source and binary forms, with or without
attila@963 7 * modification, are permitted provided that the following conditions
attila@963 8 * are met:
attila@963 9 *
attila@963 10 * - Redistributions of source code must retain the above copyright
attila@963 11 * notice, this list of conditions and the following disclaimer.
attila@963 12 *
attila@963 13 * - Redistributions in binary form must reproduce the above copyright
attila@963 14 * notice, this list of conditions and the following disclaimer in the
attila@963 15 * documentation and/or other materials provided with the distribution.
attila@963 16 *
attila@963 17 * - Neither the name of Oracle nor the names of its
attila@963 18 * contributors may be used to endorse or promote products derived
attila@963 19 * from this software without specific prior written permission.
attila@963 20 *
attila@963 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
attila@963 22 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
attila@963 23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
attila@963 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
attila@963 25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
attila@963 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
attila@963 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
attila@963 28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
attila@963 29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
attila@963 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
attila@963 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
attila@963 32 */
attila@963 33
attila@963 34 // This example demonstrates Java subclassing by Java.extend
attila@963 35 // and javac Compiler and Tree API. This example counts number
attila@963 36 // of variables called "foo" in the given java source files!
attila@963 37 if (arguments.length == 0) {
attila@963 38 print("Usage: jjs javafoovars.js -- <directory>");
attila@963 39 exit(1);
attila@963 40 }
attila@963 41
attila@963 42 // Java types used
attila@963 43 var File = Java.type("java.io.File");
attila@963 44 var Files = Java.type("java.nio.file.Files");
attila@963 45 var FileVisitOption = Java.type("java.nio.file.FileVisitOption");
attila@963 46 var StringArray = Java.type("java.lang.String[]");
attila@963 47 var ToolProvider = Java.type("javax.tools.ToolProvider");
attila@963 48 var Tree = Java.type("com.sun.source.tree.Tree");
attila@963 49 var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
attila@963 50 var VariableTree = Java.type("com.sun.source.tree.VariableTree");
attila@963 51
attila@963 52 // count "foo"-s in the given .java files
attila@963 53 function countFoo() {
attila@963 54 // get the system compiler tool
attila@963 55 var compiler = ToolProvider.systemJavaCompiler;
attila@963 56 // get standard file manager
attila@963 57 var fileMgr = compiler.getStandardFileManager(null, null, null);
attila@963 58 // Using Java.to convert script array (arguments) to a Java String[]
attila@963 59 var compUnits = fileMgr.getJavaFileObjects(
attila@963 60 Java.to(arguments, StringArray));
attila@963 61 // create a new compilation task
attila@963 62 var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
attila@963 63 // subclass SimpleTreeVisitor - to count variables called "foo"
attila@963 64 var FooCounterVisitor = Java.extend(TreeScanner);
attila@963 65 var fooCount = 0;
attila@963 66
attila@963 67 var visitor = new FooCounterVisitor() {
attila@963 68 visitVariable: function (node, p) {
attila@963 69 if (node.name.toString() == "foo") {
attila@963 70 fooCount++;
attila@963 71 }
attila@963 72 }
attila@963 73 }
attila@963 74
attila@963 75 for each (var cu in task.parse()) {
attila@963 76 cu.accept(visitor, null);
attila@963 77 }
attila@963 78 return fooCount;
attila@963 79 }
attila@963 80
attila@963 81 // for each ".java" file in directory (recursively) count "foo".
attila@963 82 function main(dir) {
attila@963 83 var totalCount = 0;
attila@963 84 Files.walk(dir.toPath(), FileVisitOption.FOLLOW_LINKS).
attila@963 85 forEach(function(p) {
attila@963 86 var name = p.toFile().absolutePath;
attila@963 87 if (name.endsWith(".java")) {
attila@963 88 var count = 0;
attila@963 89 try {
attila@963 90 count = countFoo(p.toFile().getAbsolutePath());
attila@963 91 } catch (e) {
attila@963 92 print(e);
attila@963 93 }
attila@963 94 if (count != 0) {
attila@963 95 print(name + ": " + count);
attila@963 96 }
attila@963 97 totalCount += count;
attila@963 98 }
attila@963 99 });
attila@963 100 print("Total foo count: " + totalCount);
attila@963 101 }
attila@963 102
attila@963 103 main(new File(arguments[0]));

mercurial