nashorn/samples/javacastcounter.js
author mullan
Thu, 18 Sep 2014 15:37:40 -0400
changeset 26639 a58e059dc53e
parent 26067 b32ccc3a76c9
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     1
/*
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     2
 * 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: 24283
diff changeset
     3
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     6
 * are met:
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
     7
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    10
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    14
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    18
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    30
 */
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    31
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    32
// Usage: jjs javacastcounter.js -- <.java files>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    33
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    34
// This example demonstrates Nashorn Java.extend API
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    35
// to subclass a Java class from script.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    36
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    37
// This example uses Javac Compiler and Tree API
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    38
// to list type casts used in java source files.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    39
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    40
if (arguments.length == 0) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    41
    print("Usage: jjs javacastcounter.js -- <.java files>");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    42
    exit(1);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    43
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    44
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    45
// Java types used
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    46
var ToolProvider = Java.type("javax.tools.ToolProvider");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    47
var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    48
var Trees = Java.type("com.sun.source.util.Trees");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    49
var StringArray = Java.type("java.lang.String[]");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    50
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    51
// get the system compiler tool
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    52
var compiler = ToolProvider.systemJavaCompiler;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    53
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    54
// get standard file manager
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    55
var fileMgr = compiler.getStandardFileManager(null, null, null);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    56
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    57
// make a list of compilation unit from command line argument file names
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    58
// Using Java.to convert script array (arguments) to a Java String[]
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    59
var compUnits = fileMgr.getJavaFileObjects(Java.to(arguments, StringArray));
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    60
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    61
// create a new compilation task
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    62
var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    63
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    64
// SourcePositions object to get positions of AST nodes
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    65
var sourcePositions = Trees.instance(task).sourcePositions;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    66
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    67
// Subclass TreeScanner class
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    68
var CastCounter = Java.extend(TreeScanner);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    69
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    70
var counter = new CastCounter() {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    71
    // current CompilationUnitTree
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    72
    compUnit: null,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    73
    // current LineMap (pos -> line, column)
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    74
    lineMap: null,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    75
    // current compilation unit's file name
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    76
    fileName: null,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    77
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    78
    // overrides of TreeScanner methods
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    79
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    80
    visitCompilationUnit: function(node, p) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    81
        // capture info about current Compilation unit
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    82
        this.compUnit = node;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    83
        this.lineMap = node.lineMap;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    84
        this.fileName = node.sourceFile.name;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    85
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    86
        // Using Java.super API to call super class method here
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    87
        return Java.super(counter).visitCompilationUnit(node, p);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    88
    },
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    89
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    90
    visitTypeCast: function(node, p) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    91
        // print information on this type cast node
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    92
        var pos = sourcePositions.getStartPosition(this.compUnit, node);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    93
        var line = this.lineMap.getLineNumber(pos);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    94
        var col = this.lineMap.getColumnNumber(pos);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    95
        print(node + " @ " + this.fileName + ":" + line + ":" + col);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    96
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    97
        // count one more type cast
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    98
        return 1;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    99
    },
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   100
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   101
    reduce: function(r1, r2) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   102
        return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   103
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   104
};
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   105
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   106
// print total number of type cast nodes seen
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   107
print("Total casts:", counter.scan(task.parse(), null));