samples/resourcetrysuggester.js

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

mercurial