samples/exceptionswallow.js

Wed, 16 Mar 2016 00:10:12 -0700

author
asaha
date
Wed, 16 Mar 2016 00:10:12 -0700
changeset 1746
09abd795d1d1
parent 1531
ab48ce00c634
permissions
-rw-r--r--

Added tag jdk8u77-b01 for changeset 678b645aa10a

sundar@1531 1 #// Usage: jjs exceptionswallow.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 // empty catch blocks ("exception swallow") and reports those.
sundar@1531 37
sundar@1531 38 if (arguments.length == 0) {
sundar@1531 39 print("Usage: jjs exceptionswallow.js -- <directory>");
sundar@1531 40 exit(1);
sundar@1531 41 }
sundar@1531 42
sundar@1531 43 // Java types used
sundar@1531 44 var File = Java.type("java.io.File");
sundar@1531 45 var Files = Java.type("java.nio.file.Files");
sundar@1531 46 var StringArray = Java.type("java.lang.String[]");
sundar@1531 47 var ToolProvider = Java.type("javax.tools.ToolProvider");
sundar@1531 48 var Tree = Java.type("com.sun.source.tree.Tree");
sundar@1531 49 var EmptyStatementTree = Java.type("com.sun.source.tree.EmptyStatementTree");
sundar@1531 50 var Trees = Java.type("com.sun.source.util.Trees");
sundar@1531 51 var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
sundar@1531 52
sundar@1531 53 // printEmptyCatch
sundar@1531 54
sundar@1531 55 function printEmptyCatch() {
sundar@1531 56 // get the system compiler tool
sundar@1531 57 var compiler = ToolProvider.systemJavaCompiler;
sundar@1531 58 // get standard file manager
sundar@1531 59 var fileMgr = compiler.getStandardFileManager(null, null, null);
sundar@1531 60 // Using Java.to convert script array (arguments) to a Java String[]
sundar@1531 61 var compUnits = fileMgr.getJavaFileObjects(
sundar@1531 62 Java.to(arguments, StringArray));
sundar@1531 63 // create a new compilation task
sundar@1531 64 var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
sundar@1531 65
sundar@1531 66 // SourcePositions object to get positions of AST nodes
sundar@1531 67 var sourcePositions = Trees.instance(task).sourcePositions;
sundar@1531 68
sundar@1531 69 // subclass SimpleTreeVisitor - to print empty catch
sundar@1531 70 var EmptyCatchFinder = Java.extend(TreeScanner);
sundar@1531 71
sundar@1531 72 function hasOnlyEmptyStats(stats) {
sundar@1531 73 var itr = stats.iterator();
sundar@1531 74 while (itr.hasNext()) {
sundar@1531 75 if (! (itr.next() instanceof EmptyStatementTree)) {
sundar@1531 76 return false;
sundar@1531 77 }
sundar@1531 78 }
sundar@1531 79
sundar@1531 80 return true;
sundar@1531 81 }
sundar@1531 82
sundar@1531 83 var visitor = new EmptyCatchFinder() {
sundar@1531 84 // current CompilationUnitTree
sundar@1531 85 compUnit: null,
sundar@1531 86 // current LineMap (pos -> line, column)
sundar@1531 87 lineMap: null,
sundar@1531 88 // current compilation unit's file name
sundar@1531 89 fileName: null,
sundar@1531 90
sundar@1531 91 // overrides of TreeScanner methods
sundar@1531 92
sundar@1531 93 visitCompilationUnit: function(node, p) {
sundar@1531 94 // capture info about current Compilation unit
sundar@1531 95 this.compUnit = node;
sundar@1531 96 this.lineMap = node.lineMap;
sundar@1531 97 this.fileName = node.sourceFile.name;
sundar@1531 98
sundar@1531 99 // Using Java.super API to call super class method here
sundar@1531 100 return Java.super(visitor).visitCompilationUnit(node, p);
sundar@1531 101 },
sundar@1531 102
sundar@1531 103 visitCatch: function (node, p) {
sundar@1531 104 var stats = node.block.statements;
sundar@1531 105 if (stats.empty || hasOnlyEmptyStats(stats)) {
sundar@1531 106 // print information on this empty catch
sundar@1531 107 var pos = sourcePositions.getStartPosition(this.compUnit, node);
sundar@1531 108 var line = this.lineMap.getLineNumber(pos);
sundar@1531 109 var col = this.lineMap.getColumnNumber(pos);
sundar@1531 110 print("Exception swallow" + " @ " + this.fileName + ":" + line + ":" + col);
sundar@1531 111 // print(node);
sundar@1531 112 }
sundar@1531 113 }
sundar@1531 114 }
sundar@1531 115
sundar@1531 116 for each (var cu in task.parse()) {
sundar@1531 117 cu.accept(visitor, null);
sundar@1531 118 }
sundar@1531 119 }
sundar@1531 120
sundar@1531 121 // for each ".java" file in directory (recursively) and check it!
sundar@1531 122 function main(dir) {
sundar@1531 123 Files.walk(dir.toPath()).
sundar@1531 124 forEach(function(p) {
sundar@1531 125 var name = p.toFile().absolutePath;
sundar@1531 126 if (name.endsWith(".java")) {
sundar@1531 127 try {
sundar@1531 128 printEmptyCatch(p.toFile().getAbsolutePath());
sundar@1531 129 } catch (e) {
sundar@1531 130 print(e);
sundar@1531 131 }
sundar@1531 132 }
sundar@1531 133 });
sundar@1531 134 }
sundar@1531 135
sundar@1531 136 main(new File(arguments[0]));

mercurial