1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/samples/find_nonfinals2.js Thu Aug 21 14:03:24 2014 +0530 1.3 @@ -0,0 +1,118 @@ 1.4 +#// Usage: jjs find_nonfinals2.js -- <directory> 1.5 + 1.6 +/* 1.7 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions 1.11 + * are met: 1.12 + * 1.13 + * - Redistributions of source code must retain the above copyright 1.14 + * notice, this list of conditions and the following disclaimer. 1.15 + * 1.16 + * - Redistributions in binary form must reproduce the above copyright 1.17 + * notice, this list of conditions and the following disclaimer in the 1.18 + * documentation and/or other materials provided with the distribution. 1.19 + * 1.20 + * - Neither the name of Oracle nor the names of its 1.21 + * contributors may be used to endorse or promote products derived 1.22 + * from this software without specific prior written permission. 1.23 + * 1.24 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.25 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 1.26 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1.27 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.28 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.29 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.30 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.31 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.32 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.33 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.34 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.35 + */ 1.36 + 1.37 +// This example demonstrates Java subclassing by Java.extend 1.38 +// and javac Compiler and Tree API. This example finds method 1.39 +// parameters without "final" keyword and prints info on those. 1.40 + 1.41 +if (arguments.length == 0) { 1.42 + print("Usage: jjs find_nonfinals2.js -- <directory>"); 1.43 + exit(1); 1.44 +} 1.45 + 1.46 +// Java types used 1.47 +var File = Java.type("java.io.File"); 1.48 +var Files = Java.type("java.nio.file.Files"); 1.49 +var FileVisitOption = Java.type("java.nio.file.FileVisitOption"); 1.50 +var StringArray = Java.type("java.lang.String[]"); 1.51 +var ToolProvider = Java.type("javax.tools.ToolProvider"); 1.52 +var Tree = Java.type("com.sun.source.tree.Tree"); 1.53 +var TreeScanner = Java.type("com.sun.source.util.TreeScanner"); 1.54 +var Modifier = Java.type("javax.lang.model.element.Modifier"); 1.55 + 1.56 +function checkNonFinalParams(p) { 1.57 + // get the system compiler tool 1.58 + var compiler = ToolProvider.systemJavaCompiler; 1.59 + // get standard file manager 1.60 + var fileMgr = compiler.getStandardFileManager(null, null, null); 1.61 + // Using Java.to convert script array (arguments) to a Java String[] 1.62 + var compUnits = fileMgr.getJavaFileObjects( 1.63 + Java.to(arguments, StringArray)); 1.64 + // create a new compilation task 1.65 + var task = compiler.getTask(null, fileMgr, null, null, null, compUnits); 1.66 + // subclass SimpleTreeVisitor - to find non-final method params 1.67 + var NonFinalsFinder = Java.extend(TreeScanner); 1.68 + 1.69 + function printMethod(method) { 1.70 + print(method.modifiers + " "+ method.returnType + " " + 1.71 + method.name + "(" + method.parameters + ")"); 1.72 + } 1.73 + 1.74 + var pkgName, clsName, compUnitName, lineMap; 1.75 + var visitor = new NonFinalsFinder() { 1.76 + visitCompilationUnit: function(compUnit, p) { 1.77 + pkgName = compUnit.packageName; 1.78 + compUnitName = compUnit.sourceFile.name; 1.79 + lineMap = compUnit.lineMap; 1.80 + return Java.super(visitor).visitCompilationUnit(compUnit, p); 1.81 + }, 1.82 + 1.83 + visitClass: function(clazz, p) { 1.84 + clsName = clazz.name; 1.85 + return Java.super(visitor).visitClass(clazz, p); 1.86 + }, 1.87 + 1.88 + visitMethod: function (method, p) { 1.89 + var params = method.parameters; 1.90 + for each (var p in params) { 1.91 + var modifiers = p.modifiers; 1.92 + if (! modifiers.flags.contains(Modifier.FINAL)) { 1.93 + print(compUnitName); 1.94 + print(pkgName + "." + clsName); 1.95 + printMethod(method); 1.96 + print("->", p, 1.97 + " @ " + lineMap.getLineNumber(p.pos) + ":" + 1.98 + lineMap.getColumnNumber(p.pos)); 1.99 + } 1.100 + } 1.101 + } 1.102 + } 1.103 + 1.104 + for each (var cu in task.parse()) { 1.105 + cu.accept(visitor, null); 1.106 + } 1.107 +} 1.108 + 1.109 +// for each ".java" file in directory (recursively). 1.110 +function main(dir) { 1.111 + var totalCount = 0; 1.112 + Files.walk(dir.toPath(), FileVisitOption.FOLLOW_LINKS). 1.113 + forEach(function(p) { 1.114 + var name = p.toFile().absolutePath; 1.115 + if (name.endsWith(".java")) { 1.116 + checkNonFinalParams(p); 1.117 + } 1.118 + }); 1.119 +} 1.120 + 1.121 +main(new File(arguments[0]));