Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/samples/find_nonfinals2.js
32281 views
#// Usage: jjs find_nonfinals2.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 finds method35// parameters without "final" keyword and prints info on those.3637if (arguments.length == 0) {38print("Usage: jjs find_nonfinals2.js -- <directory>");39exit(1);40}4142// Java types used43var File = Java.type("java.io.File");44var Files = Java.type("java.nio.file.Files");45var StringArray = Java.type("java.lang.String[]");46var ToolProvider = Java.type("javax.tools.ToolProvider");47var Tree = Java.type("com.sun.source.tree.Tree");48var TreeScanner = Java.type("com.sun.source.util.TreeScanner");49var Modifier = Java.type("javax.lang.model.element.Modifier");5051function checkNonFinalParams(p) {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 find non-final method params62var NonFinalsFinder = Java.extend(TreeScanner);6364function printMethod(method) {65print(method.modifiers + " "+ method.returnType + " " +66method.name + "(" + method.parameters + ")");67}6869var pkgName, clsName, compUnitName, lineMap;70var visitor = new NonFinalsFinder() {71visitCompilationUnit: function(compUnit, p) {72pkgName = compUnit.packageName;73compUnitName = compUnit.sourceFile.name;74lineMap = compUnit.lineMap;75return Java.super(visitor).visitCompilationUnit(compUnit, p);76},7778visitClass: function(clazz, p) {79clsName = clazz.name;80return Java.super(visitor).visitClass(clazz, p);81},8283visitMethod: function (method, p) {84var params = method.parameters;85for each (var p in params) {86var modifiers = p.modifiers;87if (! modifiers.flags.contains(Modifier.FINAL)) {88print(compUnitName);89print(pkgName + "." + clsName);90printMethod(method);91print("->", p,92" @ " + lineMap.getLineNumber(p.pos) + ":" +93lineMap.getColumnNumber(p.pos));94}95}96}97}9899for each (var cu in task.parse()) {100cu.accept(visitor, null);101}102}103104// for each ".java" file in directory (recursively).105function main(dir) {106var totalCount = 0;107Files.walk(dir.toPath()).108forEach(function(p) {109var name = p.toFile().absolutePath;110if (name.endsWith(".java")) {111checkNonFinalParams(p);112}113});114}115116main(new File(arguments[0]));117118119