Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/samples/exceptionswallow.js
32278 views
#// Usage: jjs exceptionswallow.js -- <directory>12/*3* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* - Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* - Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* - Neither the name of Oracle nor the names of its17* contributors may be used to endorse or promote products derived18* from this software without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS21* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,22* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR23* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR24* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,25* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,26* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR27* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF28* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING29* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS30* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233// This example demonstrates Java subclassing by Java.extend34// and javac Compiler and Tree API. This example looks for35// empty catch blocks ("exception swallow") and reports those.3637if (arguments.length == 0) {38print("Usage: jjs exceptionswallow.js -- <directory>");39exit(1);40}4142// Java types used43var File = Java.type("java.io.File");44var Files = Java.type("java.nio.file.Files");45var StringArray = Java.type("java.lang.String[]");46var ToolProvider = Java.type("javax.tools.ToolProvider");47var Tree = Java.type("com.sun.source.tree.Tree");48var EmptyStatementTree = Java.type("com.sun.source.tree.EmptyStatementTree");49var Trees = Java.type("com.sun.source.util.Trees");50var TreeScanner = Java.type("com.sun.source.util.TreeScanner");5152// printEmptyCatch5354function printEmptyCatch() {55// get the system compiler tool56var compiler = ToolProvider.systemJavaCompiler;57// get standard file manager58var fileMgr = compiler.getStandardFileManager(null, null, null);59// Using Java.to convert script array (arguments) to a Java String[]60var compUnits = fileMgr.getJavaFileObjects(61Java.to(arguments, StringArray));62// create a new compilation task63var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);6465// SourcePositions object to get positions of AST nodes66var sourcePositions = Trees.instance(task).sourcePositions;6768// subclass SimpleTreeVisitor - to print empty catch69var EmptyCatchFinder = Java.extend(TreeScanner);7071function hasOnlyEmptyStats(stats) {72var itr = stats.iterator();73while (itr.hasNext()) {74if (! (itr.next() instanceof EmptyStatementTree)) {75return false;76}77}7879return true;80}8182var visitor = new EmptyCatchFinder() {83// current CompilationUnitTree84compUnit: null,85// current LineMap (pos -> line, column)86lineMap: null,87// current compilation unit's file name88fileName: null,8990// overrides of TreeScanner methods9192visitCompilationUnit: function(node, p) {93// capture info about current Compilation unit94this.compUnit = node;95this.lineMap = node.lineMap;96this.fileName = node.sourceFile.name;9798// Using Java.super API to call super class method here99return Java.super(visitor).visitCompilationUnit(node, p);100},101102visitCatch: function (node, p) {103var stats = node.block.statements;104if (stats.empty || hasOnlyEmptyStats(stats)) {105// print information on this empty catch106var pos = sourcePositions.getStartPosition(this.compUnit, node);107var line = this.lineMap.getLineNumber(pos);108var col = this.lineMap.getColumnNumber(pos);109print("Exception swallow" + " @ " + this.fileName + ":" + line + ":" + col);110// print(node);111}112}113}114115for each (var cu in task.parse()) {116cu.accept(visitor, null);117}118}119120// for each ".java" file in directory (recursively) and check it!121function main(dir) {122Files.walk(dir.toPath()).123forEach(function(p) {124var name = p.toFile().absolutePath;125if (name.endsWith(".java")) {126try {127printEmptyCatch(p.toFile().getAbsolutePath());128} catch (e) {129print(e);130}131}132});133}134135main(new File(arguments[0]));136137138