1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/samples/resourcetrysuggester.js Tue Sep 15 16:17:33 2015 +0530 1.3 @@ -0,0 +1,156 @@ 1.4 +#// Usage: jjs resourcetrysuggester.js -- <directory> 1.5 + 1.6 +/* 1.7 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions 1.11 + * are met: 1.12 + * 1.13 + * - Redistributions of source code must retain the above copyright 1.14 + * notice, this list of conditions and the following disclaimer. 1.15 + * 1.16 + * - Redistributions in binary form must reproduce the above copyright 1.17 + * notice, this list of conditions and the following disclaimer in the 1.18 + * documentation and/or other materials provided with the distribution. 1.19 + * 1.20 + * - Neither the name of Oracle nor the names of its 1.21 + * contributors may be used to endorse or promote products derived 1.22 + * from this software without specific prior written permission. 1.23 + * 1.24 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.25 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 1.26 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1.27 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.28 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.29 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.30 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.31 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.32 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.33 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.34 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.35 + */ 1.36 + 1.37 +// This example demonstrates Java subclassing by Java.extend 1.38 +// and javac Compiler and Tree API. This example looks for 1.39 +// finally clauses with "close" call and suggests "resource try"! 1.40 + 1.41 +if (arguments.length == 0) { 1.42 + print("Usage: jjs resourcetrysuggester.js -- <directory>"); 1.43 + exit(1); 1.44 +} 1.45 + 1.46 +// Java types used 1.47 +var ExpressionStatementTree = Java.type("com.sun.source.tree.ExpressionStatementTree"); 1.48 +var File = Java.type("java.io.File"); 1.49 +var Files = Java.type("java.nio.file.Files"); 1.50 +var MemberSelectTree = Java.type("com.sun.source.tree.MemberSelectTree"); 1.51 +var MethodInvocationTree = Java.type("com.sun.source.tree.MethodInvocationTree"); 1.52 +var StringArray = Java.type("java.lang.String[]"); 1.53 +var ToolProvider = Java.type("javax.tools.ToolProvider"); 1.54 +var Tree = Java.type("com.sun.source.tree.Tree"); 1.55 +var Trees = Java.type("com.sun.source.util.Trees"); 1.56 +var TreeScanner = Java.type("com.sun.source.util.TreeScanner"); 1.57 + 1.58 +// resourceTrySuggestions 1.59 + 1.60 +function resourceTrySuggestions() { 1.61 + // get the system compiler tool 1.62 + var compiler = ToolProvider.systemJavaCompiler; 1.63 + // get standard file manager 1.64 + var fileMgr = compiler.getStandardFileManager(null, null, null); 1.65 + // Using Java.to convert script array (arguments) to a Java String[] 1.66 + var compUnits = fileMgr.getJavaFileObjects( 1.67 + Java.to(arguments, StringArray)); 1.68 + // create a new compilation task 1.69 + var task = compiler.getTask(null, fileMgr, null, null, null, compUnits); 1.70 + 1.71 + // SourcePositions object to get positions of AST nodes 1.72 + var sourcePositions = Trees.instance(task).sourcePositions; 1.73 + 1.74 + // subclass SimpleTreeVisitor - to print resource try suggestions 1.75 + var ResourceTrySuggester = Java.extend(TreeScanner); 1.76 + 1.77 + function hasOnlyEmptyStats(stats) { 1.78 + var itr = stats.iterator(); 1.79 + while (itr.hasNext()) { 1.80 + if (! (itr.next() instanceof EmptyStatementTree)) { 1.81 + return false; 1.82 + } 1.83 + } 1.84 + 1.85 + return true; 1.86 + } 1.87 + 1.88 + // does the given statement list has an expression statement which 1.89 + // calls "close" method (don't worry about types - just crude one will do) 1.90 + function hasCloseCall(stats) { 1.91 + var itr = stats.iterator(); 1.92 + while (itr.hasNext()) { 1.93 + var stat = itr.next(); 1.94 + if (stat instanceof ExpressionStatementTree) { 1.95 + var expr = stat.expression; 1.96 + if (expr instanceof MethodInvocationTree) { 1.97 + var method = expr.methodSelect; 1.98 + if (method instanceof MemberSelectTree) { 1.99 + return method.identifier.toString().equals("close"); 1.100 + } 1.101 + } 1.102 + } 1.103 + } 1.104 + return false; 1.105 + } 1.106 + 1.107 + var visitor = new ResourceTrySuggester() { 1.108 + // current CompilationUnitTree 1.109 + compUnit: null, 1.110 + // current LineMap (pos -> line, column) 1.111 + lineMap: null, 1.112 + // current compilation unit's file name 1.113 + fileName: null, 1.114 + 1.115 + // overrides of TreeScanner methods 1.116 + 1.117 + visitCompilationUnit: function(node, p) { 1.118 + // capture info about current Compilation unit 1.119 + this.compUnit = node; 1.120 + this.lineMap = node.lineMap; 1.121 + this.fileName = node.sourceFile.name; 1.122 + 1.123 + // Using Java.super API to call super class method here 1.124 + return Java.super(visitor).visitCompilationUnit(node, p); 1.125 + }, 1.126 + 1.127 + visitTry: function (node, p) { 1.128 + var finallyBlk = node.finallyBlock; 1.129 + if (finallyBlk != null && hasCloseCall(finallyBlk.statements)) { 1.130 + var pos = sourcePositions.getStartPosition(this.compUnit, node); 1.131 + var line = this.lineMap.getLineNumber(pos); 1.132 + var col = this.lineMap.getColumnNumber(pos); 1.133 + print("Consider resource try statement " + " @ " + this.fileName + ":" + line + ":" + col); 1.134 + // print(node); 1.135 + } 1.136 + } 1.137 + } 1.138 + 1.139 + for each (var cu in task.parse()) { 1.140 + cu.accept(visitor, null); 1.141 + } 1.142 +} 1.143 + 1.144 +// for each ".java" file in directory (recursively) and check it! 1.145 +function main(dir) { 1.146 + Files.walk(dir.toPath()). 1.147 + forEach(function(p) { 1.148 + var name = p.toFile().absolutePath; 1.149 + if (name.endsWith(".java")) { 1.150 + try { 1.151 + resourceTrySuggestions(p.toFile().getAbsolutePath()); 1.152 + } catch (e) { 1.153 + print(e); 1.154 + } 1.155 + } 1.156 + }); 1.157 +} 1.158 + 1.159 +main(new File(arguments[0]));