nashorn/samples/javafoovars.js
author lana
Sat, 09 Aug 2014 21:27:07 -0700
changeset 25887 f2a451de5953
parent 25586 cac6440168e2
child 26067 b32ccc3a76c9
permissions -rw-r--r--
Added tag jdk9-b26 for changeset 3a55fad9854a
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.
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
     5
 * 
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:
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
     9
 * 
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.
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    12
 * 
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.
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    16
 * 
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.
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    20
 * 
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
 */
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    33
 
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
}
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    41
 
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 FileVisitOption = Java.type("java.nio.file.FileVisitOption");
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    46
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
    47
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
    48
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
    49
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
    50
var VariableTree = Java.type("com.sun.source.tree.VariableTree");
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    51
 
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    52
// 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
    53
function countFoo() {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    54
    // get the system compiler tool
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    55
    var compiler = ToolProvider.systemJavaCompiler;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    56
    // get standard file manager
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    57
    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
    58
    // 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
    59
    var compUnits = fileMgr.getJavaFileObjects(
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    60
        Java.to(arguments, StringArray));
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    61
    // create a new compilation task
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    62
    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
    63
    // 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
    64
    var FooCounterVisitor = Java.extend(TreeScanner);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    65
    var fooCount = 0;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    66
 
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    67
    var visitor = new FooCounterVisitor() {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    68
        visitVariable: function (node, p) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    69
            if (node.name.toString() == "foo") {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    70
                fooCount++;
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
        }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    73
    }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    74
 
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    75
    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
    76
        cu.accept(visitor, null);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    77
    }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    78
    return fooCount;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    79
}
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    80
 
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    81
// 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
    82
function main(dir) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    83
    var totalCount = 0;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    84
    Files.walk(dir.toPath(), FileVisitOption.FOLLOW_LINKS).
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    85
      forEach(function(p) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    86
        var name = p.toFile().absolutePath;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    87
        if (name.endsWith(".java")) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    88
            var count = 0;
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    89
            try {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    90
                count = countFoo(p.toFile().getAbsolutePath());
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    91
            } catch (e) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    92
                print(e);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    93
            }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    94
            if (count != 0) {
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    95
                print(name + ": " + count);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    96
            }
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
    97
            totalCount += count;
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
      });
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
   100
    print("Total foo count: " + totalCount);
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
   101
}
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
   102
 
cac6440168e2 8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
sundar
parents:
diff changeset
   103
main(new File(arguments[0]));