Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/samples/javacastcounter.js
32278 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031// Usage: jjs javacastcounter.js -- <.java files>3233// This example demonstrates Nashorn Java.extend API34// to subclass a Java class from script.3536// This example uses Javac Compiler and Tree API37// to list type casts used in java source files.3839if (arguments.length == 0) {40print("Usage: jjs javacastcounter.js -- <.java files>");41exit(1);42}4344// Java types used45var ToolProvider = Java.type("javax.tools.ToolProvider");46var TreeScanner = Java.type("com.sun.source.util.TreeScanner");47var Trees = Java.type("com.sun.source.util.Trees");48var StringArray = Java.type("java.lang.String[]");4950// get the system compiler tool51var compiler = ToolProvider.systemJavaCompiler;5253// get standard file manager54var fileMgr = compiler.getStandardFileManager(null, null, null);5556// make a list of compilation unit from command line argument file names57// Using Java.to convert script array (arguments) to a Java String[]58var compUnits = fileMgr.getJavaFileObjects(Java.to(arguments, StringArray));5960// create a new compilation task61var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);6263// SourcePositions object to get positions of AST nodes64var sourcePositions = Trees.instance(task).sourcePositions;6566// Subclass TreeScanner class67var CastCounter = Java.extend(TreeScanner);6869var counter = new CastCounter() {70// current CompilationUnitTree71compUnit: null,72// current LineMap (pos -> line, column)73lineMap: null,74// current compilation unit's file name75fileName: null,7677// overrides of TreeScanner methods7879visitCompilationUnit: function(node, p) {80// capture info about current Compilation unit81this.compUnit = node;82this.lineMap = node.lineMap;83this.fileName = node.sourceFile.name;8485// Using Java.super API to call super class method here86return Java.super(counter).visitCompilationUnit(node, p);87},8889visitTypeCast: function(node, p) {90// print information on this type cast node91var pos = sourcePositions.getStartPosition(this.compUnit, node);92var line = this.lineMap.getLineNumber(pos);93var col = this.lineMap.getColumnNumber(pos);94print(node + " @ " + this.fileName + ":" + line + ":" + col);9596// count one more type cast97return 1;98},99100reduce: function(r1, r2) {101return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);102}103};104105// print total number of type cast nodes seen106print("Total casts:", counter.scan(task.parse(), null));107108109