samples/resourcetrysuggester.js

Tue, 22 Mar 2016 21:31:52 -0700

author
asaha
date
Tue, 22 Mar 2016 21:31:52 -0700
changeset 1810
d0fba38a0705
parent 1531
ab48ce00c634
permissions
-rw-r--r--

Added tag jdk8u92-b13 for changeset e2294411edbd

sundar@1531 1 #// Usage: jjs resourcetrysuggester.js -- <directory>
sundar@1531 2
sundar@1531 3 /*
sundar@1531 4 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
sundar@1531 5 *
sundar@1531 6 * Redistribution and use in source and binary forms, with or without
sundar@1531 7 * modification, are permitted provided that the following conditions
sundar@1531 8 * are met:
sundar@1531 9 *
sundar@1531 10 * - Redistributions of source code must retain the above copyright
sundar@1531 11 * notice, this list of conditions and the following disclaimer.
sundar@1531 12 *
sundar@1531 13 * - Redistributions in binary form must reproduce the above copyright
sundar@1531 14 * notice, this list of conditions and the following disclaimer in the
sundar@1531 15 * documentation and/or other materials provided with the distribution.
sundar@1531 16 *
sundar@1531 17 * - Neither the name of Oracle nor the names of its
sundar@1531 18 * contributors may be used to endorse or promote products derived
sundar@1531 19 * from this software without specific prior written permission.
sundar@1531 20 *
sundar@1531 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
sundar@1531 22 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
sundar@1531 23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sundar@1531 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
sundar@1531 25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
sundar@1531 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
sundar@1531 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
sundar@1531 28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
sundar@1531 29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
sundar@1531 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
sundar@1531 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sundar@1531 32 */
sundar@1531 33
sundar@1531 34 // This example demonstrates Java subclassing by Java.extend
sundar@1531 35 // and javac Compiler and Tree API. This example looks for
sundar@1531 36 // finally clauses with "close" call and suggests "resource try"!
sundar@1531 37
sundar@1531 38 if (arguments.length == 0) {
sundar@1531 39 print("Usage: jjs resourcetrysuggester.js -- <directory>");
sundar@1531 40 exit(1);
sundar@1531 41 }
sundar@1531 42
sundar@1531 43 // Java types used
sundar@1531 44 var ExpressionStatementTree = Java.type("com.sun.source.tree.ExpressionStatementTree");
sundar@1531 45 var File = Java.type("java.io.File");
sundar@1531 46 var Files = Java.type("java.nio.file.Files");
sundar@1531 47 var MemberSelectTree = Java.type("com.sun.source.tree.MemberSelectTree");
sundar@1531 48 var MethodInvocationTree = Java.type("com.sun.source.tree.MethodInvocationTree");
sundar@1531 49 var StringArray = Java.type("java.lang.String[]");
sundar@1531 50 var ToolProvider = Java.type("javax.tools.ToolProvider");
sundar@1531 51 var Tree = Java.type("com.sun.source.tree.Tree");
sundar@1531 52 var Trees = Java.type("com.sun.source.util.Trees");
sundar@1531 53 var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
sundar@1531 54
sundar@1531 55 // resourceTrySuggestions
sundar@1531 56
sundar@1531 57 function resourceTrySuggestions() {
sundar@1531 58 // get the system compiler tool
sundar@1531 59 var compiler = ToolProvider.systemJavaCompiler;
sundar@1531 60 // get standard file manager
sundar@1531 61 var fileMgr = compiler.getStandardFileManager(null, null, null);
sundar@1531 62 // Using Java.to convert script array (arguments) to a Java String[]
sundar@1531 63 var compUnits = fileMgr.getJavaFileObjects(
sundar@1531 64 Java.to(arguments, StringArray));
sundar@1531 65 // create a new compilation task
sundar@1531 66 var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
sundar@1531 67
sundar@1531 68 // SourcePositions object to get positions of AST nodes
sundar@1531 69 var sourcePositions = Trees.instance(task).sourcePositions;
sundar@1531 70
sundar@1531 71 // subclass SimpleTreeVisitor - to print resource try suggestions
sundar@1531 72 var ResourceTrySuggester = Java.extend(TreeScanner);
sundar@1531 73
sundar@1531 74 function hasOnlyEmptyStats(stats) {
sundar@1531 75 var itr = stats.iterator();
sundar@1531 76 while (itr.hasNext()) {
sundar@1531 77 if (! (itr.next() instanceof EmptyStatementTree)) {
sundar@1531 78 return false;
sundar@1531 79 }
sundar@1531 80 }
sundar@1531 81
sundar@1531 82 return true;
sundar@1531 83 }
sundar@1531 84
sundar@1531 85 // does the given statement list has an expression statement which
sundar@1531 86 // calls "close" method (don't worry about types - just crude one will do)
sundar@1531 87 function hasCloseCall(stats) {
sundar@1531 88 var itr = stats.iterator();
sundar@1531 89 while (itr.hasNext()) {
sundar@1531 90 var stat = itr.next();
sundar@1531 91 if (stat instanceof ExpressionStatementTree) {
sundar@1531 92 var expr = stat.expression;
sundar@1531 93 if (expr instanceof MethodInvocationTree) {
sundar@1531 94 var method = expr.methodSelect;
sundar@1531 95 if (method instanceof MemberSelectTree) {
sundar@1531 96 return method.identifier.toString().equals("close");
sundar@1531 97 }
sundar@1531 98 }
sundar@1531 99 }
sundar@1531 100 }
sundar@1531 101 return false;
sundar@1531 102 }
sundar@1531 103
sundar@1531 104 var visitor = new ResourceTrySuggester() {
sundar@1531 105 // current CompilationUnitTree
sundar@1531 106 compUnit: null,
sundar@1531 107 // current LineMap (pos -> line, column)
sundar@1531 108 lineMap: null,
sundar@1531 109 // current compilation unit's file name
sundar@1531 110 fileName: null,
sundar@1531 111
sundar@1531 112 // overrides of TreeScanner methods
sundar@1531 113
sundar@1531 114 visitCompilationUnit: function(node, p) {
sundar@1531 115 // capture info about current Compilation unit
sundar@1531 116 this.compUnit = node;
sundar@1531 117 this.lineMap = node.lineMap;
sundar@1531 118 this.fileName = node.sourceFile.name;
sundar@1531 119
sundar@1531 120 // Using Java.super API to call super class method here
sundar@1531 121 return Java.super(visitor).visitCompilationUnit(node, p);
sundar@1531 122 },
sundar@1531 123
sundar@1531 124 visitTry: function (node, p) {
sundar@1531 125 var finallyBlk = node.finallyBlock;
sundar@1531 126 if (finallyBlk != null && hasCloseCall(finallyBlk.statements)) {
sundar@1531 127 var pos = sourcePositions.getStartPosition(this.compUnit, node);
sundar@1531 128 var line = this.lineMap.getLineNumber(pos);
sundar@1531 129 var col = this.lineMap.getColumnNumber(pos);
sundar@1531 130 print("Consider resource try statement " + " @ " + this.fileName + ":" + line + ":" + col);
sundar@1531 131 // print(node);
sundar@1531 132 }
sundar@1531 133 }
sundar@1531 134 }
sundar@1531 135
sundar@1531 136 for each (var cu in task.parse()) {
sundar@1531 137 cu.accept(visitor, null);
sundar@1531 138 }
sundar@1531 139 }
sundar@1531 140
sundar@1531 141 // for each ".java" file in directory (recursively) and check it!
sundar@1531 142 function main(dir) {
sundar@1531 143 Files.walk(dir.toPath()).
sundar@1531 144 forEach(function(p) {
sundar@1531 145 var name = p.toFile().absolutePath;
sundar@1531 146 if (name.endsWith(".java")) {
sundar@1531 147 try {
sundar@1531 148 resourceTrySuggestions(p.toFile().getAbsolutePath());
sundar@1531 149 } catch (e) {
sundar@1531 150 print(e);
sundar@1531 151 }
sundar@1531 152 }
sundar@1531 153 });
sundar@1531 154 }
sundar@1531 155
sundar@1531 156 main(new File(arguments[0]));

mercurial