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