nashorn/samples/find_nonfinals.js
author duke
Wed, 05 Jul 2017 20:35:22 +0200
changeset 30736 ff3fc75f3214
parent 26235 62342aaa8ff9
child 30984 844283fdfa54
permissions -rw-r--r--
Merge
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");
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    42
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
    43
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
    44
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
    45
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    46
// 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
    47
function findNashorn() {
26235
62342aaa8ff9 8055687: Wrong "this" passed to JSObject.eval call
sundar
parents: 26070
diff changeset
    48
    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
    49
    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
    50
        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
    51
        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
    52
            return nashorn;
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    53
        }
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
// 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
    58
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
    59
    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
    60
    for each (var method in methods) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    61
        // 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
    62
        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
    63
        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
    64
           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
    65
           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
    66
               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
    67
                   print(method);
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    68
                   print(" ->", p);
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    69
               }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    70
           }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    71
        }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    72
    }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    73
}
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
var jarFile = findNashorn();
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    76
// 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
    77
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
    78
    function(entry) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    79
        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
    80
        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
    81
            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
    82
            clsName = clsName.replace(/\//g, '.');
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    83
            var cls = Class.forName(clsName);
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    84
            analyzeClass(cls);
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    85
        }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    86
    }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    87
)