nashorn/samples/find_nonfinals2.js
author sundar
Wed, 20 Aug 2014 21:32:09 +0530
changeset 26235 62342aaa8ff9
child 32692 8f60bd284bf4
permissions -rw-r--r--
8055687: Wrong "this" passed to JSObject.eval call Reviewed-by: lagergren, hannesw, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26235
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
     1
#// Usage: jjs find_nonfinals2.js -- <directory>
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
     2
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
     3
/*
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
     4
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
     5
 *
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
     8
 * are met:
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
     9
 *
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    11
 *     notice, this list of conditions and the following disclaimer.
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    12
 *
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    15
 *     documentation and/or other materials provided with the distribution.
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    16
 *
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    19
 *     from this software without specific prior written permission.
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    20
 *
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    32
 */
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    33
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    34
// This example demonstrates Java subclassing by Java.extend
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    35
// and javac Compiler and Tree API. This example finds method
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    36
// parameters without "final" keyword and prints info on those.
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    37
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    38
if (arguments.length == 0) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    39
    print("Usage: jjs find_nonfinals2.js -- <directory>");
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    40
    exit(1);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    41
}
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    42
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    43
// Java types used
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    44
var File = Java.type("java.io.File");
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    45
var Files = Java.type("java.nio.file.Files");
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    46
var FileVisitOption = Java.type("java.nio.file.FileVisitOption");
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    47
var StringArray = Java.type("java.lang.String[]");
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    48
var ToolProvider = Java.type("javax.tools.ToolProvider");
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    49
var Tree = Java.type("com.sun.source.tree.Tree");
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    50
var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    51
var Modifier = Java.type("javax.lang.model.element.Modifier");
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    52
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    53
function checkNonFinalParams(p) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    54
    // get the system compiler tool
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    55
    var compiler = ToolProvider.systemJavaCompiler;
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    56
    // get standard file manager
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    57
    var fileMgr = compiler.getStandardFileManager(null, null, null);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    58
    // Using Java.to convert script array (arguments) to a Java String[]
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    59
    var compUnits = fileMgr.getJavaFileObjects(
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    60
        Java.to(arguments, StringArray));
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    61
    // create a new compilation task
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    62
    var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    63
    // subclass SimpleTreeVisitor - to find non-final method params
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    64
    var NonFinalsFinder = Java.extend(TreeScanner);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    65
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    66
    function printMethod(method) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    67
        print(method.modifiers + " "+ method.returnType + " " +
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    68
            method.name + "(" + method.parameters + ")");
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    69
    }
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    70
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    71
    var pkgName, clsName, compUnitName, lineMap;
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    72
    var visitor = new NonFinalsFinder() {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    73
        visitCompilationUnit: function(compUnit, p) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    74
            pkgName = compUnit.packageName;
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    75
            compUnitName = compUnit.sourceFile.name;
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    76
            lineMap = compUnit.lineMap;
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    77
            return Java.super(visitor).visitCompilationUnit(compUnit, p);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    78
        },
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    79
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    80
        visitClass: function(clazz, p) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    81
            clsName = clazz.name;
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    82
            return Java.super(visitor).visitClass(clazz, p);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    83
        },
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    84
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    85
        visitMethod: function (method, p) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    86
            var params = method.parameters;
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    87
            for each (var p in params) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    88
                var modifiers = p.modifiers;
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    89
                if (! modifiers.flags.contains(Modifier.FINAL)) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    90
                    print(compUnitName);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    91
                    print(pkgName + "." + clsName);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    92
                    printMethod(method);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    93
                    print("->", p,
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    94
                     " @ " + lineMap.getLineNumber(p.pos) + ":" +
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    95
                           lineMap.getColumnNumber(p.pos));
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    96
                }
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    97
            }
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    98
        }
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
    99
    }
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   100
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   101
    for each (var cu in task.parse()) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   102
        cu.accept(visitor, null);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   103
    }
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   104
}
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   105
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   106
// for each ".java" file in directory (recursively).
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   107
function main(dir) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   108
    var totalCount = 0;
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   109
    Files.walk(dir.toPath(), FileVisitOption.FOLLOW_LINKS).
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   110
      forEach(function(p) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   111
        var name = p.toFile().absolutePath;
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   112
        if (name.endsWith(".java")) {
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   113
            checkNonFinalParams(p);
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   114
        }
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   115
      });
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   116
}
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   117
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents:
diff changeset
   118
main(new File(arguments[0]));