nashorn/samples/find_underscores.js
author kbarrett
Tue, 17 Nov 2015 16:40:52 -0500
changeset 34148 6efbc7ffd767
parent 33534 28065d9f5521
permissions -rw-r--r--
8143014: Access PtrQueue member offsets through derived classes Summary: Moved accessors to derived classes and updated callers. Reviewed-by: tschatzl, jmasa, twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
     1
/*
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
     3
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
     6
 * are met:
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
     7
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    10
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    14
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    18
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    30
 */
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    31
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    32
// Usage: jjs find_underscores.js -- <directory>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    33
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    34
if (arguments.length == 0) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    35
    print("Usage: jjs find_underscores.js -- <directory>");
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    36
    exit(1);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    37
}
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    38
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    39
// Java types used
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    40
var File = Java.type("java.io.File");
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    41
var Files = Java.type("java.nio.file.Files");
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    42
var StringArray = Java.type("java.lang.String[]");
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    43
var ToolProvider = Java.type("javax.tools.ToolProvider");
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    44
var Tree = Java.type("com.sun.source.tree.Tree");
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    45
var Trees = Java.type("com.sun.source.util.Trees");
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    46
var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    47
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    48
function findUnderscores() {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    49
    // get the system compiler tool
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    50
    var compiler = ToolProvider.systemJavaCompiler;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    51
    // get standard file manager
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    52
    var fileMgr = compiler.getStandardFileManager(null, null, null);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    53
    // Using Java.to convert script array (arguments) to a Java String[]
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    54
    var compUnits = fileMgr.getJavaFileObjects(Java.to(arguments, StringArray));
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    55
    // create a new compilation task
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    56
    var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    57
    var sourcePositions = Trees.instance(task).sourcePositions;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    58
    // subclass SimpleTreeVisitor - to find underscore variable names
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    59
    var UnderscoreFinder = Java.extend(TreeScanner);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    60
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    61
    var visitor = new UnderscoreFinder() {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    62
        // override to capture information on current compilation unit
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    63
        visitCompilationUnit: function(compUnit, p) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    64
            this.compUnit = compUnit;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    65
            this.lineMap = compUnit.lineMap;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    66
            this.fileName = compUnit.sourceFile.name;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    67
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    68
            return Java.super(visitor).visitCompilationUnit(compUnit, p);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    69
        },
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    70
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    71
        // override to check variable name
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    72
        visitVariable: function(node, p) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    73
            if (node.name.toString() == "_") {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    74
                var pos = sourcePositions.getStartPosition(this.compUnit, node);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    75
                var line = this.lineMap.getLineNumber(pos);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    76
                var col = this.lineMap.getColumnNumber(pos);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    77
                print(node + " @ " + this.fileName + ":" + line + ":" + col);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    78
            }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    79
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    80
            return Java.super(visitor).visitVariable(node, p);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    81
        }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    82
    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    83
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    84
    for each (var cu in task.parse()) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    85
        cu.accept(visitor, null);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    86
    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    87
}
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    88
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    89
// for each ".java" file in directory (recursively).
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    90
function main(dir) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    91
    var totalCount = 0;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    92
    Files.walk(dir.toPath()).
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    93
      forEach(function(p) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    94
          var name = p.toFile().absolutePath;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    95
          if (name.endsWith(".java")) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    96
              findUnderscores(p);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    97
          }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    98
      });
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
    99
}
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
   100
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents:
diff changeset
   101
main(new File(arguments[0]));