samples/getclassnpe.js

changeset 1232
09bd5b8abcba
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/getclassnpe.js	Fri Feb 06 19:28:26 2015 +0530
     1.3 @@ -0,0 +1,122 @@
     1.4 +#// Usage: jjs getclassnpe.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 +/*
    1.38 + * java.lang.Object.getClass() is sometimes used to do null check. This
    1.39 + * obfuscating Object.getClass() check relies on non-related intrinsic
    1.40 + * performance, which is potentially not available everywhere.
    1.41 + * See also http://cr.openjdk.java.net/~shade/scratch/NullChecks.java
    1.42 + * This nashorn script checks for such uses in your .java files in the
    1.43 + * given directory (recursively).
    1.44 + */
    1.45 +
    1.46 +if (arguments.length == 0) {
    1.47 +    print("Usage: jjs getclassnpe.js -- <directory>");
    1.48 +    exit(1);
    1.49 +}
    1.50 +
    1.51 +// Java types used
    1.52 +var File = Java.type("java.io.File");
    1.53 +var Files = Java.type("java.nio.file.Files");
    1.54 +var StringArray = Java.type("java.lang.String[]");
    1.55 +var ToolProvider = Java.type("javax.tools.ToolProvider");
    1.56 +var MethodInvocationTree = Java.type("com.sun.source.tree.MethodInvocationTree");
    1.57 +var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
    1.58 +
    1.59 +// parse a specific .java file to check if it uses
    1.60 +// Object.getClass() for null check.
    1.61 +function checkGetClassNPE() {
    1.62 +    // get the system compiler tool
    1.63 +    var compiler = ToolProvider.systemJavaCompiler;
    1.64 +    // get standard file manager
    1.65 +    var fileMgr = compiler.getStandardFileManager(null, null, null);
    1.66 +    // Using Java.to convert script array (arguments) to a Java String[]
    1.67 +    var compUnits = fileMgr.getJavaFileObjects(
    1.68 +        Java.to(arguments, StringArray));
    1.69 +    // create a new compilation task
    1.70 +    var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
    1.71 +    // subclass SimpleTreeVisitor - to check for obj.getClass(); statements
    1.72 +    var GetClassNPEChecker = Java.extend(TreeScanner);
    1.73 +
    1.74 +    var visitor = new GetClassNPEChecker() {
    1.75 +        lineMap: null,
    1.76 +        sourceFile: null,
    1.77 +
    1.78 +        // save compilation unit details for reporting
    1.79 +        visitCompilationUnit: function(node, p) {
    1.80 +           this.sourceFile = node.sourceFile;
    1.81 +           this.lineMap = node.lineMap;
    1.82 +           return Java.super(visitor).visitCompilationUnit(node, p);
    1.83 +        },
    1.84 +
    1.85 +        // look for "foo.getClass();" expression statements
    1.86 +        visitExpressionStatement: function(node, p) {
    1.87 +            var expr = node.expression;
    1.88 +            if (expr instanceof MethodInvocationTree) {
    1.89 +                var name = String(expr.methodSelect.identifier);
    1.90 +
    1.91 +                // will match any "getClass" call with zero arguments!
    1.92 +                if (name == "getClass" && expr.arguments.size() == 0) {
    1.93 +                    print(this.sourceFile.getName()
    1.94 +                     + " @ "
    1.95 +                     + this.lineMap.getLineNumber(node.pos)
    1.96 +                     + ":"
    1.97 +                     + this.lineMap.getColumnNumber(node.pos));
    1.98 +
    1.99 +                    print("\t", node);
   1.100 +                }
   1.101 +            }
   1.102 +        }
   1.103 +    }
   1.104 +
   1.105 +    for each (var cu in task.parse()) {
   1.106 +        cu.accept(visitor, null);
   1.107 +    }
   1.108 +}
   1.109 +
   1.110 +// for each ".java" file in the directory (recursively)
   1.111 +function main(dir) {
   1.112 +    Files.walk(dir.toPath()).
   1.113 +      forEach(function(p) {
   1.114 +          var name = p.toFile().absolutePath;
   1.115 +          if (name.endsWith(".java")) {
   1.116 +              try {
   1.117 +                  checkGetClassNPE(p.toFile().getAbsolutePath());
   1.118 +              } catch (e) {
   1.119 +                  print(e);
   1.120 +              }
   1.121 +          }
   1.122 +      });
   1.123 +}
   1.124 +
   1.125 +main(new File(arguments[0]));

mercurial