aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. attila@962: * aoqi@0: * Redistribution and use in source and binary forms, with or without aoqi@0: * modification, are permitted provided that the following conditions aoqi@0: * are met: attila@962: * aoqi@0: * - Redistributions of source code must retain the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer. attila@962: * aoqi@0: * - Redistributions in binary form must reproduce the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer in the aoqi@0: * documentation and/or other materials provided with the distribution. attila@962: * aoqi@0: * - Neither the name of Oracle nor the names of its aoqi@0: * contributors may be used to endorse or promote products derived aoqi@0: * from this software without specific prior written permission. attila@962: * aoqi@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS aoqi@0: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, aoqi@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR aoqi@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR aoqi@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, aoqi@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, aoqi@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR aoqi@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF aoqi@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING aoqi@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS aoqi@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. aoqi@0: */ aoqi@0: aoqi@0: // Usage: jjs javacastcounter.js -- <.java files> aoqi@0: aoqi@0: // This example demonstrates Nashorn Java.extend API aoqi@0: // to subclass a Java class from script. aoqi@0: aoqi@0: // This example uses Javac Compiler and Tree API aoqi@0: // to list type casts used in java source files. aoqi@0: aoqi@0: if (arguments.length == 0) { aoqi@0: print("Usage: jjs javacastcounter.js -- <.java files>"); aoqi@0: exit(1); aoqi@0: } aoqi@0: aoqi@0: // Java types used aoqi@0: var ToolProvider = Java.type("javax.tools.ToolProvider"); aoqi@0: var TreeScanner = Java.type("com.sun.source.util.TreeScanner"); aoqi@0: var Trees = Java.type("com.sun.source.util.Trees"); aoqi@0: var StringArray = Java.type("java.lang.String[]"); aoqi@0: aoqi@0: // get the system compiler tool aoqi@0: var compiler = ToolProvider.systemJavaCompiler; aoqi@0: aoqi@0: // get standard file manager aoqi@0: var fileMgr = compiler.getStandardFileManager(null, null, null); aoqi@0: aoqi@0: // make a list of compilation unit from command line argument file names aoqi@0: // Using Java.to convert script array (arguments) to a Java String[] aoqi@0: var compUnits = fileMgr.getJavaFileObjects(Java.to(arguments, StringArray)); aoqi@0: aoqi@0: // create a new compilation task aoqi@0: var task = compiler.getTask(null, fileMgr, null, null, null, compUnits); aoqi@0: aoqi@0: // SourcePositions object to get positions of AST nodes aoqi@0: var sourcePositions = Trees.instance(task).sourcePositions; aoqi@0: aoqi@0: // Subclass TreeScanner class aoqi@0: var CastCounter = Java.extend(TreeScanner); aoqi@0: aoqi@0: var counter = new CastCounter() { aoqi@0: // current CompilationUnitTree aoqi@0: compUnit: null, aoqi@0: // current LineMap (pos -> line, column) aoqi@0: lineMap: null, aoqi@0: // current compilation unit's file name aoqi@0: fileName: null, aoqi@0: aoqi@0: // overrides of TreeScanner methods aoqi@0: aoqi@0: visitCompilationUnit: function(node, p) { aoqi@0: // capture info about current Compilation unit aoqi@0: this.compUnit = node; aoqi@0: this.lineMap = node.lineMap; aoqi@0: this.fileName = node.sourceFile.name; aoqi@0: attila@962: // Using Java.super API to call super class method here aoqi@0: return Java.super(counter).visitCompilationUnit(node, p); aoqi@0: }, aoqi@0: aoqi@0: visitTypeCast: function(node, p) { aoqi@0: // print information on this type cast node aoqi@0: var pos = sourcePositions.getStartPosition(this.compUnit, node); aoqi@0: var line = this.lineMap.getLineNumber(pos); aoqi@0: var col = this.lineMap.getColumnNumber(pos); aoqi@0: print(node + " @ " + this.fileName + ":" + line + ":" + col); aoqi@0: aoqi@0: // count one more type cast aoqi@0: return 1; aoqi@0: }, aoqi@0: aoqi@0: reduce: function(r1, r2) { aoqi@0: return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // print total number of type cast nodes seen aoqi@0: print("Total casts:", counter.scan(task.parse(), null));