1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/samples/exceptionswallow.js Tue Sep 15 16:17:33 2015 +0530 1.3 @@ -0,0 +1,136 @@ 1.4 +#// Usage: jjs exceptionswallow.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 +// empty catch blocks ("exception swallow") and reports those. 1.40 + 1.41 +if (arguments.length == 0) { 1.42 + print("Usage: jjs exceptionswallow.js -- <directory>"); 1.43 + exit(1); 1.44 +} 1.45 + 1.46 +// Java types used 1.47 +var File = Java.type("java.io.File"); 1.48 +var Files = Java.type("java.nio.file.Files"); 1.49 +var StringArray = Java.type("java.lang.String[]"); 1.50 +var ToolProvider = Java.type("javax.tools.ToolProvider"); 1.51 +var Tree = Java.type("com.sun.source.tree.Tree"); 1.52 +var EmptyStatementTree = Java.type("com.sun.source.tree.EmptyStatementTree"); 1.53 +var Trees = Java.type("com.sun.source.util.Trees"); 1.54 +var TreeScanner = Java.type("com.sun.source.util.TreeScanner"); 1.55 + 1.56 +// printEmptyCatch 1.57 + 1.58 +function printEmptyCatch() { 1.59 + // get the system compiler tool 1.60 + var compiler = ToolProvider.systemJavaCompiler; 1.61 + // get standard file manager 1.62 + var fileMgr = compiler.getStandardFileManager(null, null, null); 1.63 + // Using Java.to convert script array (arguments) to a Java String[] 1.64 + var compUnits = fileMgr.getJavaFileObjects( 1.65 + Java.to(arguments, StringArray)); 1.66 + // create a new compilation task 1.67 + var task = compiler.getTask(null, fileMgr, null, null, null, compUnits); 1.68 + 1.69 + // SourcePositions object to get positions of AST nodes 1.70 + var sourcePositions = Trees.instance(task).sourcePositions; 1.71 + 1.72 + // subclass SimpleTreeVisitor - to print empty catch 1.73 + var EmptyCatchFinder = Java.extend(TreeScanner); 1.74 + 1.75 + function hasOnlyEmptyStats(stats) { 1.76 + var itr = stats.iterator(); 1.77 + while (itr.hasNext()) { 1.78 + if (! (itr.next() instanceof EmptyStatementTree)) { 1.79 + return false; 1.80 + } 1.81 + } 1.82 + 1.83 + return true; 1.84 + } 1.85 + 1.86 + var visitor = new EmptyCatchFinder() { 1.87 + // current CompilationUnitTree 1.88 + compUnit: null, 1.89 + // current LineMap (pos -> line, column) 1.90 + lineMap: null, 1.91 + // current compilation unit's file name 1.92 + fileName: null, 1.93 + 1.94 + // overrides of TreeScanner methods 1.95 + 1.96 + visitCompilationUnit: function(node, p) { 1.97 + // capture info about current Compilation unit 1.98 + this.compUnit = node; 1.99 + this.lineMap = node.lineMap; 1.100 + this.fileName = node.sourceFile.name; 1.101 + 1.102 + // Using Java.super API to call super class method here 1.103 + return Java.super(visitor).visitCompilationUnit(node, p); 1.104 + }, 1.105 + 1.106 + visitCatch: function (node, p) { 1.107 + var stats = node.block.statements; 1.108 + if (stats.empty || hasOnlyEmptyStats(stats)) { 1.109 + // print information on this empty catch 1.110 + var pos = sourcePositions.getStartPosition(this.compUnit, node); 1.111 + var line = this.lineMap.getLineNumber(pos); 1.112 + var col = this.lineMap.getColumnNumber(pos); 1.113 + print("Exception swallow" + " @ " + this.fileName + ":" + line + ":" + col); 1.114 + // print(node); 1.115 + } 1.116 + } 1.117 + } 1.118 + 1.119 + for each (var cu in task.parse()) { 1.120 + cu.accept(visitor, null); 1.121 + } 1.122 +} 1.123 + 1.124 +// for each ".java" file in directory (recursively) and check it! 1.125 +function main(dir) { 1.126 + Files.walk(dir.toPath()). 1.127 + forEach(function(p) { 1.128 + var name = p.toFile().absolutePath; 1.129 + if (name.endsWith(".java")) { 1.130 + try { 1.131 + printEmptyCatch(p.toFile().getAbsolutePath()); 1.132 + } catch (e) { 1.133 + print(e); 1.134 + } 1.135 + } 1.136 + }); 1.137 +} 1.138 + 1.139 +main(new File(arguments[0]));