Wed, 07 Oct 2015 10:01:40 -0700
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
attila@962 | 3 | * |
aoqi@0 | 4 | * Redistribution and use in source and binary forms, with or without |
aoqi@0 | 5 | * modification, are permitted provided that the following conditions |
aoqi@0 | 6 | * are met: |
attila@962 | 7 | * |
aoqi@0 | 8 | * - Redistributions of source code must retain the above copyright |
aoqi@0 | 9 | * notice, this list of conditions and the following disclaimer. |
attila@962 | 10 | * |
aoqi@0 | 11 | * - Redistributions in binary form must reproduce the above copyright |
aoqi@0 | 12 | * notice, this list of conditions and the following disclaimer in the |
aoqi@0 | 13 | * documentation and/or other materials provided with the distribution. |
attila@962 | 14 | * |
aoqi@0 | 15 | * - Neither the name of Oracle nor the names of its |
aoqi@0 | 16 | * contributors may be used to endorse or promote products derived |
aoqi@0 | 17 | * from this software without specific prior written permission. |
attila@962 | 18 | * |
aoqi@0 | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
aoqi@0 | 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
aoqi@0 | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
aoqi@0 | 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
aoqi@0 | 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
aoqi@0 | 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
aoqi@0 | 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
aoqi@0 | 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
aoqi@0 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
aoqi@0 | 30 | */ |
aoqi@0 | 31 | |
aoqi@0 | 32 | // Usage: jjs javacastcounter.js -- <.java files> |
aoqi@0 | 33 | |
aoqi@0 | 34 | // This example demonstrates Nashorn Java.extend API |
aoqi@0 | 35 | // to subclass a Java class from script. |
aoqi@0 | 36 | |
aoqi@0 | 37 | // This example uses Javac Compiler and Tree API |
aoqi@0 | 38 | // to list type casts used in java source files. |
aoqi@0 | 39 | |
aoqi@0 | 40 | if (arguments.length == 0) { |
aoqi@0 | 41 | print("Usage: jjs javacastcounter.js -- <.java files>"); |
aoqi@0 | 42 | exit(1); |
aoqi@0 | 43 | } |
aoqi@0 | 44 | |
aoqi@0 | 45 | // Java types used |
aoqi@0 | 46 | var ToolProvider = Java.type("javax.tools.ToolProvider"); |
aoqi@0 | 47 | var TreeScanner = Java.type("com.sun.source.util.TreeScanner"); |
aoqi@0 | 48 | var Trees = Java.type("com.sun.source.util.Trees"); |
aoqi@0 | 49 | var StringArray = Java.type("java.lang.String[]"); |
aoqi@0 | 50 | |
aoqi@0 | 51 | // get the system compiler tool |
aoqi@0 | 52 | var compiler = ToolProvider.systemJavaCompiler; |
aoqi@0 | 53 | |
aoqi@0 | 54 | // get standard file manager |
aoqi@0 | 55 | var fileMgr = compiler.getStandardFileManager(null, null, null); |
aoqi@0 | 56 | |
aoqi@0 | 57 | // make a list of compilation unit from command line argument file names |
aoqi@0 | 58 | // Using Java.to convert script array (arguments) to a Java String[] |
aoqi@0 | 59 | var compUnits = fileMgr.getJavaFileObjects(Java.to(arguments, StringArray)); |
aoqi@0 | 60 | |
aoqi@0 | 61 | // create a new compilation task |
aoqi@0 | 62 | var task = compiler.getTask(null, fileMgr, null, null, null, compUnits); |
aoqi@0 | 63 | |
aoqi@0 | 64 | // SourcePositions object to get positions of AST nodes |
aoqi@0 | 65 | var sourcePositions = Trees.instance(task).sourcePositions; |
aoqi@0 | 66 | |
aoqi@0 | 67 | // Subclass TreeScanner class |
aoqi@0 | 68 | var CastCounter = Java.extend(TreeScanner); |
aoqi@0 | 69 | |
aoqi@0 | 70 | var counter = new CastCounter() { |
aoqi@0 | 71 | // current CompilationUnitTree |
aoqi@0 | 72 | compUnit: null, |
aoqi@0 | 73 | // current LineMap (pos -> line, column) |
aoqi@0 | 74 | lineMap: null, |
aoqi@0 | 75 | // current compilation unit's file name |
aoqi@0 | 76 | fileName: null, |
aoqi@0 | 77 | |
aoqi@0 | 78 | // overrides of TreeScanner methods |
aoqi@0 | 79 | |
aoqi@0 | 80 | visitCompilationUnit: function(node, p) { |
aoqi@0 | 81 | // capture info about current Compilation unit |
aoqi@0 | 82 | this.compUnit = node; |
aoqi@0 | 83 | this.lineMap = node.lineMap; |
aoqi@0 | 84 | this.fileName = node.sourceFile.name; |
aoqi@0 | 85 | |
attila@962 | 86 | // Using Java.super API to call super class method here |
aoqi@0 | 87 | return Java.super(counter).visitCompilationUnit(node, p); |
aoqi@0 | 88 | }, |
aoqi@0 | 89 | |
aoqi@0 | 90 | visitTypeCast: function(node, p) { |
aoqi@0 | 91 | // print information on this type cast node |
aoqi@0 | 92 | var pos = sourcePositions.getStartPosition(this.compUnit, node); |
aoqi@0 | 93 | var line = this.lineMap.getLineNumber(pos); |
aoqi@0 | 94 | var col = this.lineMap.getColumnNumber(pos); |
aoqi@0 | 95 | print(node + " @ " + this.fileName + ":" + line + ":" + col); |
aoqi@0 | 96 | |
aoqi@0 | 97 | // count one more type cast |
aoqi@0 | 98 | return 1; |
aoqi@0 | 99 | }, |
aoqi@0 | 100 | |
aoqi@0 | 101 | reduce: function(r1, r2) { |
aoqi@0 | 102 | return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); |
aoqi@0 | 103 | } |
aoqi@0 | 104 | }; |
aoqi@0 | 105 | |
aoqi@0 | 106 | // print total number of type cast nodes seen |
aoqi@0 | 107 | print("Total casts:", counter.scan(task.parse(), null)); |