1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/samples/javacastcounter.js Wed Apr 27 01:36:41 2016 +0800 1.3 @@ -0,0 +1,107 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1.11 + * - Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 1.14 + * - Redistributions in binary form must reproduce the above copyright 1.15 + * notice, this list of conditions and the following disclaimer in the 1.16 + * documentation and/or other materials provided with the distribution. 1.17 + * 1.18 + * - Neither the name of Oracle nor the names of its 1.19 + * contributors may be used to endorse or promote products derived 1.20 + * from this software without specific prior written permission. 1.21 + * 1.22 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.23 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 1.24 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1.25 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.26 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.27 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.28 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.29 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.30 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.31 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.32 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.33 + */ 1.34 + 1.35 +// Usage: jjs javacastcounter.js -- <.java files> 1.36 + 1.37 +// This example demonstrates Nashorn Java.extend API 1.38 +// to subclass a Java class from script. 1.39 + 1.40 +// This example uses Javac Compiler and Tree API 1.41 +// to list type casts used in java source files. 1.42 + 1.43 +if (arguments.length == 0) { 1.44 + print("Usage: jjs javacastcounter.js -- <.java files>"); 1.45 + exit(1); 1.46 +} 1.47 + 1.48 +// Java types used 1.49 +var ToolProvider = Java.type("javax.tools.ToolProvider"); 1.50 +var TreeScanner = Java.type("com.sun.source.util.TreeScanner"); 1.51 +var Trees = Java.type("com.sun.source.util.Trees"); 1.52 +var StringArray = Java.type("java.lang.String[]"); 1.53 + 1.54 +// get the system compiler tool 1.55 +var compiler = ToolProvider.systemJavaCompiler; 1.56 + 1.57 +// get standard file manager 1.58 +var fileMgr = compiler.getStandardFileManager(null, null, null); 1.59 + 1.60 +// make a list of compilation unit from command line argument file names 1.61 +// Using Java.to convert script array (arguments) to a Java String[] 1.62 +var compUnits = fileMgr.getJavaFileObjects(Java.to(arguments, StringArray)); 1.63 + 1.64 +// create a new compilation task 1.65 +var task = compiler.getTask(null, fileMgr, null, null, null, compUnits); 1.66 + 1.67 +// SourcePositions object to get positions of AST nodes 1.68 +var sourcePositions = Trees.instance(task).sourcePositions; 1.69 + 1.70 +// Subclass TreeScanner class 1.71 +var CastCounter = Java.extend(TreeScanner); 1.72 + 1.73 +var counter = new CastCounter() { 1.74 + // current CompilationUnitTree 1.75 + compUnit: null, 1.76 + // current LineMap (pos -> line, column) 1.77 + lineMap: null, 1.78 + // current compilation unit's file name 1.79 + fileName: null, 1.80 + 1.81 + // overrides of TreeScanner methods 1.82 + 1.83 + visitCompilationUnit: function(node, p) { 1.84 + // capture info about current Compilation unit 1.85 + this.compUnit = node; 1.86 + this.lineMap = node.lineMap; 1.87 + this.fileName = node.sourceFile.name; 1.88 + 1.89 + // Using Java.super API to call super class method here 1.90 + return Java.super(counter).visitCompilationUnit(node, p); 1.91 + }, 1.92 + 1.93 + visitTypeCast: function(node, p) { 1.94 + // print information on this type cast node 1.95 + var pos = sourcePositions.getStartPosition(this.compUnit, node); 1.96 + var line = this.lineMap.getLineNumber(pos); 1.97 + var col = this.lineMap.getColumnNumber(pos); 1.98 + print(node + " @ " + this.fileName + ":" + line + ":" + col); 1.99 + 1.100 + // count one more type cast 1.101 + return 1; 1.102 + }, 1.103 + 1.104 + reduce: function(r1, r2) { 1.105 + return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); 1.106 + } 1.107 +}; 1.108 + 1.109 +// print total number of type cast nodes seen 1.110 +print("Total casts:", counter.scan(task.parse(), null));