nashorn/samples/find_nonfinals.js
author lana
Thu, 27 Apr 2017 16:07:56 +0000
changeset 44833 87af7c1d14a8
parent 30984 844283fdfa54
permissions -rw-r--r--
Added tag jdk-9+167 for changeset c5de7263722b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26070
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     1
/*
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     3
 *
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     6
 * are met:
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     7
 *
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    10
 *
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    14
 *
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    18
 *
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    30
 */
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    31
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    32
/**
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    33
 * Nashorn project uses "final" modifier for method parameters
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    34
 * (like 'val' of Scala). This tool finds method parameters that
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    35
 * miss final modifier.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    36
 */
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    37
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    38
// Usage: jjs -J-Djava.ext.dirs=<your_nashorn_jar_dir> find_nonfinals.js
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    39
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    40
var Class = Java.type("java.lang.Class");
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    41
var System = Java.type("java.lang.System");
30984
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    42
var Thread = Java.type("java.lang.Thread");
26070
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    43
var File = Java.type("java.io.File");
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    44
var JarFile = Java.type("java.util.jar.JarFile");
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    45
var Modifier = Java.type("java.lang.reflect.Modifier");
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    46
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    47
// locate nashorn.jar from java.ext.dirs
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    48
function findNashorn() {
26235
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents: 26070
diff changeset
    49
    var paths = System.getProperty("java.ext.dirs").split(File.pathSeparator);
26070
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    50
    for each (var p in paths) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    51
        var nashorn = p + File.separator + "nashorn.jar";
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    52
        if (new File(nashorn).exists()) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    53
            return nashorn;
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    54
        }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    55
    }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    56
}
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    57
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    58
// analyze a single Class and print info on non-final parameters
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    59
function analyzeClass(cls) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    60
    var methods = cls.getDeclaredMethods();
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    61
    for each (var method in methods) {
30984
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    62
        var methodModifiers = method.modifiers;
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    63
        if (Modifier.isAbstract(methodModifiers) || Modifier.isNative(methodModifiers)) {
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    64
            continue;
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    65
        }
26070
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    66
        // this requires -parameters option when compiling java sources
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    67
        var params = method.parameters;
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    68
        for each (var p in params) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    69
           var modifiers = p.modifiers;
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    70
           if (!Modifier.isFinal(modifiers)) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    71
               if (! method.name.startsWith("access$")) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    72
                   print(method);
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    73
                   print(" ->", p);
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    74
               }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    75
           }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    76
        }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    77
    }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    78
}
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    79
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    80
var jarFile = findNashorn();
30984
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    81
var ctxtLoader = Thread.currentThread().contextClassLoader;
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    82
26070
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    83
// load each class and use reflection to analyze each Class
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    84
new JarFile(jarFile).stream().forEach(
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    85
    function(entry) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    86
        var name = entry.name;
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    87
        if (name.endsWith(".class")) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    88
            var clsName = name.substring(0, name.lastIndexOf('.class'));
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    89
            clsName = clsName.replace(/\//g, '.');
30984
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    90
            try {
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    91
                // don't initialize to avoid for possible initialization errors 
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    92
                var cls = Class.forName(clsName, false, ctxtLoader);
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    93
                analyzeClass(cls);
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    94
            } catch (e) {
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    95
                // print exception and continue analysis for other classes
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    96
                print("Failed to analyze " + clsName);
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    97
                e.printStackTrace();
844283fdfa54 8081809: Missing final modifier in method parameters (nashorn code convention)
sundar
parents: 26235
diff changeset
    98
            }
26070
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    99
        }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
   100
    }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
   101
)