nashorn/samples/javafoovars.js
author rehn
Wed, 24 Aug 2016 20:38:21 +0200
changeset 40668 60d799be5dda
parent 32692 8f60bd284bf4
permissions -rw-r--r--
8164208: Update tests with redefine classes UL options and tags​ Reviewed-by: coleenp, gtriantafill
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
     1
#// Usage: jjs javafoovars.js -- <directory>
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
     2
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
     3
/*
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
     4
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
     5
 *
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
     8
 * are met:
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
     9
 *
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    11
 *     notice, this list of conditions and the following disclaimer.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
    12
 *
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    15
 *     documentation and/or other materials provided with the distribution.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
    16
 *
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    19
 *     from this software without specific prior written permission.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
    20
 *
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    32
 */
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
    33
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    34
// This example demonstrates Java subclassing by Java.extend
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    35
// and javac Compiler and Tree API. This example counts number
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    36
// of variables called "foo" in the given java source files!
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    37
if (arguments.length == 0) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    38
    print("Usage: jjs javafoovars.js -- <directory>");
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    39
    exit(1);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    40
}
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
    41
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    42
// Java types used
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    43
var File = Java.type("java.io.File");
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    44
var Files = Java.type("java.nio.file.Files");
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    45
var StringArray = Java.type("java.lang.String[]");
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    46
var ToolProvider = Java.type("javax.tools.ToolProvider");
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    47
var Tree = Java.type("com.sun.source.tree.Tree");
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    48
var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    49
var VariableTree = Java.type("com.sun.source.tree.VariableTree");
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
    50
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    51
// count "foo"-s in the given .java files
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    52
function countFoo() {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    53
    // get the system compiler tool
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    54
    var compiler = ToolProvider.systemJavaCompiler;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    55
    // get standard file manager
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    56
    var fileMgr = compiler.getStandardFileManager(null, null, null);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    57
    // Using Java.to convert script array (arguments) to a Java String[]
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    58
    var compUnits = fileMgr.getJavaFileObjects(
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    59
        Java.to(arguments, StringArray));
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    60
    // create a new compilation task
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    61
    var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    62
    // subclass SimpleTreeVisitor - to count variables called "foo"
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    63
    var FooCounterVisitor = Java.extend(TreeScanner);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    64
    var fooCount = 0;
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
    65
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    66
    var visitor = new FooCounterVisitor() {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    67
        visitVariable: function (node, p) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    68
            if (node.name.toString() == "foo") {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    69
                fooCount++;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    70
            }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    71
        }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    72
    }
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
    73
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    74
    for each (var cu in task.parse()) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    75
        cu.accept(visitor, null);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    76
    }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    77
    return fooCount;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    78
}
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
    79
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    80
// for each ".java" file in directory (recursively) count "foo".
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    81
function main(dir) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    82
    var totalCount = 0;
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 26067
diff changeset
    83
    Files.walk(dir.toPath()).
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    84
      forEach(function(p) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    85
        var name = p.toFile().absolutePath;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    86
        if (name.endsWith(".java")) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    87
            var count = 0;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    88
            try {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    89
                count = countFoo(p.toFile().getAbsolutePath());
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    90
            } catch (e) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    91
                print(e);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    92
            }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    93
            if (count != 0) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    94
                print(name + ": " + count);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    95
            }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    96
            totalCount += count;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    97
        }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    98
      });
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    99
    print("Total foo count: " + totalCount);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
   100
}
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25586
diff changeset
   101
25586
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
   102
main(new File(arguments[0]));