Wed, 07 Oct 2015 10:01:40 -0700
Merge
sundar@965 | 1 | #// Usage: jjs find_nonfinals2.js -- <directory> |
sundar@965 | 2 | |
sundar@965 | 3 | /* |
sundar@965 | 4 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
sundar@965 | 5 | * |
sundar@965 | 6 | * Redistribution and use in source and binary forms, with or without |
sundar@965 | 7 | * modification, are permitted provided that the following conditions |
sundar@965 | 8 | * are met: |
sundar@965 | 9 | * |
sundar@965 | 10 | * - Redistributions of source code must retain the above copyright |
sundar@965 | 11 | * notice, this list of conditions and the following disclaimer. |
sundar@965 | 12 | * |
sundar@965 | 13 | * - Redistributions in binary form must reproduce the above copyright |
sundar@965 | 14 | * notice, this list of conditions and the following disclaimer in the |
sundar@965 | 15 | * documentation and/or other materials provided with the distribution. |
sundar@965 | 16 | * |
sundar@965 | 17 | * - Neither the name of Oracle nor the names of its |
sundar@965 | 18 | * contributors may be used to endorse or promote products derived |
sundar@965 | 19 | * from this software without specific prior written permission. |
sundar@965 | 20 | * |
sundar@965 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
sundar@965 | 22 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
sundar@965 | 23 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
sundar@965 | 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
sundar@965 | 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
sundar@965 | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
sundar@965 | 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
sundar@965 | 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
sundar@965 | 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
sundar@965 | 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
sundar@965 | 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
sundar@965 | 32 | */ |
sundar@965 | 33 | |
sundar@965 | 34 | // This example demonstrates Java subclassing by Java.extend |
sundar@965 | 35 | // and javac Compiler and Tree API. This example finds method |
sundar@965 | 36 | // parameters without "final" keyword and prints info on those. |
sundar@965 | 37 | |
sundar@965 | 38 | if (arguments.length == 0) { |
sundar@965 | 39 | print("Usage: jjs find_nonfinals2.js -- <directory>"); |
sundar@965 | 40 | exit(1); |
sundar@965 | 41 | } |
sundar@965 | 42 | |
sundar@965 | 43 | // Java types used |
sundar@965 | 44 | var File = Java.type("java.io.File"); |
sundar@965 | 45 | var Files = Java.type("java.nio.file.Files"); |
sundar@965 | 46 | var StringArray = Java.type("java.lang.String[]"); |
sundar@965 | 47 | var ToolProvider = Java.type("javax.tools.ToolProvider"); |
sundar@965 | 48 | var Tree = Java.type("com.sun.source.tree.Tree"); |
sundar@965 | 49 | var TreeScanner = Java.type("com.sun.source.util.TreeScanner"); |
sundar@965 | 50 | var Modifier = Java.type("javax.lang.model.element.Modifier"); |
sundar@965 | 51 | |
sundar@965 | 52 | function checkNonFinalParams(p) { |
sundar@965 | 53 | // get the system compiler tool |
sundar@965 | 54 | var compiler = ToolProvider.systemJavaCompiler; |
sundar@965 | 55 | // get standard file manager |
sundar@965 | 56 | var fileMgr = compiler.getStandardFileManager(null, null, null); |
sundar@965 | 57 | // Using Java.to convert script array (arguments) to a Java String[] |
sundar@965 | 58 | var compUnits = fileMgr.getJavaFileObjects( |
sundar@965 | 59 | Java.to(arguments, StringArray)); |
sundar@965 | 60 | // create a new compilation task |
sundar@965 | 61 | var task = compiler.getTask(null, fileMgr, null, null, null, compUnits); |
sundar@965 | 62 | // subclass SimpleTreeVisitor - to find non-final method params |
sundar@965 | 63 | var NonFinalsFinder = Java.extend(TreeScanner); |
sundar@965 | 64 | |
sundar@965 | 65 | function printMethod(method) { |
sundar@965 | 66 | print(method.modifiers + " "+ method.returnType + " " + |
sundar@965 | 67 | method.name + "(" + method.parameters + ")"); |
sundar@965 | 68 | } |
sundar@965 | 69 | |
sundar@965 | 70 | var pkgName, clsName, compUnitName, lineMap; |
sundar@965 | 71 | var visitor = new NonFinalsFinder() { |
sundar@965 | 72 | visitCompilationUnit: function(compUnit, p) { |
sundar@965 | 73 | pkgName = compUnit.packageName; |
sundar@965 | 74 | compUnitName = compUnit.sourceFile.name; |
sundar@965 | 75 | lineMap = compUnit.lineMap; |
sundar@965 | 76 | return Java.super(visitor).visitCompilationUnit(compUnit, p); |
sundar@965 | 77 | }, |
sundar@965 | 78 | |
sundar@965 | 79 | visitClass: function(clazz, p) { |
sundar@965 | 80 | clsName = clazz.name; |
sundar@965 | 81 | return Java.super(visitor).visitClass(clazz, p); |
sundar@965 | 82 | }, |
sundar@965 | 83 | |
sundar@965 | 84 | visitMethod: function (method, p) { |
sundar@965 | 85 | var params = method.parameters; |
sundar@965 | 86 | for each (var p in params) { |
sundar@965 | 87 | var modifiers = p.modifiers; |
sundar@965 | 88 | if (! modifiers.flags.contains(Modifier.FINAL)) { |
sundar@965 | 89 | print(compUnitName); |
sundar@965 | 90 | print(pkgName + "." + clsName); |
sundar@965 | 91 | printMethod(method); |
sundar@965 | 92 | print("->", p, |
sundar@965 | 93 | " @ " + lineMap.getLineNumber(p.pos) + ":" + |
sundar@965 | 94 | lineMap.getColumnNumber(p.pos)); |
sundar@965 | 95 | } |
sundar@965 | 96 | } |
sundar@965 | 97 | } |
sundar@965 | 98 | } |
sundar@965 | 99 | |
sundar@965 | 100 | for each (var cu in task.parse()) { |
sundar@965 | 101 | cu.accept(visitor, null); |
sundar@965 | 102 | } |
sundar@965 | 103 | } |
sundar@965 | 104 | |
sundar@965 | 105 | // for each ".java" file in directory (recursively). |
sundar@965 | 106 | function main(dir) { |
sundar@965 | 107 | var totalCount = 0; |
sundar@1531 | 108 | Files.walk(dir.toPath()). |
sundar@965 | 109 | forEach(function(p) { |
sundar@965 | 110 | var name = p.toFile().absolutePath; |
sundar@965 | 111 | if (name.endsWith(".java")) { |
sundar@965 | 112 | checkNonFinalParams(p); |
sundar@965 | 113 | } |
sundar@965 | 114 | }); |
sundar@965 | 115 | } |
sundar@965 | 116 | |
sundar@965 | 117 | main(new File(arguments[0])); |