nashorn/samples/checknames.js
author xuelei
Thu, 10 Nov 2016 15:46:40 +0000
changeset 43212 5aa719c66677
parent 39165 f0000753cecd
permissions -rw-r--r--
8168728: DSA signing improvments Reviewed-by: valeriep, vinnie, ahgross, asmotrak, robm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39165
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
     1
/*
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
     3
 *
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
     6
 * are met:
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
     7
 *
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    10
 *
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    14
 *
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    18
 *
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    30
 */
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    31
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    32
// Simple Java identifier name pattern checker. You can check
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    33
// class, method and variable names in java sources to confirm
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    34
// to specified patterns. Default check functions just check for
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    35
// 'too short' names. You can customize checkXYZName functions to
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    36
// have arbitrary name pattern checks.
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    37
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    38
// Usage: jjs checknames.js -- <directory>
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    39
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    40
if (arguments.length == 0) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    41
    print("Usage: jjs checknames.js -- <directory>");
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    42
    exit(1);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    43
}
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    44
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    45
// Java types used
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    46
var File = Java.type("java.io.File");
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    47
var Files = Java.type("java.nio.file.Files");
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    48
var StringArray = Java.type("java.lang.String[]");
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    49
var ToolProvider = Java.type("javax.tools.ToolProvider");
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    50
var Tree = Java.type("com.sun.source.tree.Tree");
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    51
var Trees = Java.type("com.sun.source.util.Trees");
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    52
var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    53
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    54
// replace these checkXYZ functions with checks you want!
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    55
function checkClassName(name) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    56
    return name.length < 3;
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    57
}
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    58
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    59
function checkMethodName(name) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    60
    return name.length < 3;
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    61
}
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    62
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    63
function checkVarName(name) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    64
    return name.length < 3;
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    65
}
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    66
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    67
function checkNames() {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    68
    // get the system compiler tool
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    69
    var compiler = ToolProvider.systemJavaCompiler;
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    70
    // get standard file manager
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    71
    var fileMgr = compiler.getStandardFileManager(null, null, null);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    72
    // Using Java.to convert script array (arguments) to a Java String[]
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    73
    var compUnits = fileMgr.getJavaFileObjects(Java.to(arguments, StringArray));
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    74
    // create a new compilation task
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    75
    var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    76
    var sourcePositions = Trees.instance(task).sourcePositions;
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    77
    // subclass SimpleTreeVisitor
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    78
    var NameChecker = Java.extend(TreeScanner);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    79
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    80
    var visitor = new NameChecker() {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    81
        report: function(node) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    82
            var pos = sourcePositions.getStartPosition(this.compUnit, node);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    83
            var line = this.lineMap.getLineNumber(pos);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    84
            var col = this.lineMap.getColumnNumber(pos);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    85
            print("Too short name: " + node.name + " @ " + this.fileName + ":" + line + ":" + col);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    86
        },
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    87
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    88
        // override to capture information on current compilation unit
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    89
        visitCompilationUnit: function(compUnit, p) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    90
            this.compUnit = compUnit;
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    91
            this.lineMap = compUnit.lineMap;
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    92
            this.fileName = compUnit.sourceFile.name;
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    93
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    94
            return Java.super(visitor).visitCompilationUnit(compUnit, p);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    95
        },
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    96
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    97
        // override to check class name
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    98
        visitClass: function(node, p) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
    99
            if (checkClassName(node.simpleName.toString())) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   100
                this.report(node);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   101
            }
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   102
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   103
            return Java.super(visitor).visitClass(node, p);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   104
        },
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   105
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   106
        // override to check method name
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   107
        visitMethod: function(node, p) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   108
            if (checkMethodName(node.name.toString())) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   109
                this.report(node);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   110
            }
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   111
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   112
            return Java.super(visitor).visitMethod(node, p);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   113
        },
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   114
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   115
        // override to check variable name
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   116
        visitVariable: function(node, p) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   117
            if (checkVarName(node.name.toString())) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   118
                this.report(node);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   119
            }
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   120
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   121
            return Java.super(visitor).visitVariable(node, p);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   122
        }
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   123
    }
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   124
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   125
    for each (var cu in task.parse()) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   126
        cu.accept(visitor, null);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   127
    }
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   128
}
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   129
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   130
// for each ".java" file in directory (recursively).
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   131
function main(dir) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   132
    var totalCount = 0;
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   133
    Files.walk(dir.toPath()).
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   134
      forEach(function(p) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   135
          var name = p.toFile().absolutePath;
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   136
          if (name.endsWith(".java")) {
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   137
              checkNames(p);
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   138
          }
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   139
      });
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   140
}
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   141
f0000753cecd 8160141: removed deprecated method calls in nashorn code
sundar
parents:
diff changeset
   142
main(new File(arguments[0]));