Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/samples/javafoovars.js
32278 views
#// Usage: jjs javafoovars.js -- <directory>12/*3* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* - Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* - Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* - Neither the name of Oracle nor the names of its17* contributors may be used to endorse or promote products derived18* from this software without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS21* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,22* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR23* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR24* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,25* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,26* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR27* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF28* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING29* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS30* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233// This example demonstrates Java subclassing by Java.extend34// and javac Compiler and Tree API. This example counts number35// of variables called "foo" in the given java source files!36if (arguments.length == 0) {37print("Usage: jjs javafoovars.js -- <directory>");38exit(1);39}4041// Java types used42var File = Java.type("java.io.File");43var Files = Java.type("java.nio.file.Files");44var StringArray = Java.type("java.lang.String[]");45var ToolProvider = Java.type("javax.tools.ToolProvider");46var Tree = Java.type("com.sun.source.tree.Tree");47var TreeScanner = Java.type("com.sun.source.util.TreeScanner");48var VariableTree = Java.type("com.sun.source.tree.VariableTree");4950// count "foo"-s in the given .java files51function countFoo() {52// get the system compiler tool53var compiler = ToolProvider.systemJavaCompiler;54// get standard file manager55var fileMgr = compiler.getStandardFileManager(null, null, null);56// Using Java.to convert script array (arguments) to a Java String[]57var compUnits = fileMgr.getJavaFileObjects(58Java.to(arguments, StringArray));59// create a new compilation task60var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);61// subclass SimpleTreeVisitor - to count variables called "foo"62var FooCounterVisitor = Java.extend(TreeScanner);63var fooCount = 0;6465var visitor = new FooCounterVisitor() {66visitVariable: function (node, p) {67if (node.name.toString() == "foo") {68fooCount++;69}70}71}7273for each (var cu in task.parse()) {74cu.accept(visitor, null);75}76return fooCount;77}7879// for each ".java" file in directory (recursively) count "foo".80function main(dir) {81var totalCount = 0;82Files.walk(dir.toPath()).83forEach(function(p) {84var name = p.toFile().absolutePath;85if (name.endsWith(".java")) {86var count = 0;87try {88count = countFoo(p.toFile().getAbsolutePath());89} catch (e) {90print(e);91}92if (count != 0) {93print(name + ": " + count);94}95totalCount += count;96}97});98print("Total foo count: " + totalCount);99}100101main(new File(arguments[0]));102103104