test/tools/javac/tree/T6993305.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 0
959103a6100f
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 6993305
    27  * @summary starting position of a method without modifiers and with type parameters is incorrect
    28  */
    30 import java.io.File;
    31 import javax.tools.JavaFileObject;
    32 import javax.tools.StandardJavaFileManager;
    34 import com.sun.source.tree.CompilationUnitTree;
    35 import com.sun.source.tree.MethodTree;
    36 import com.sun.source.util.JavacTask;
    37 import com.sun.source.util.SourcePositions;
    38 import com.sun.source.util.TreeScanner;
    39 import com.sun.source.util.Trees;
    40 import com.sun.tools.javac.api.JavacTool;
    41 import java.io.IOException;
    43 /*
    44  * Test verifies the starting position of all methods by computing the start position
    45  * of each method as the first non-white character on the first line containing
    46  * (" " + methodName + "("), and then comparing this value against the reported
    47  * value in the SourcePositions table.
    48  */
    49 public class T6993305 {
    50     <T> void test1(T t) { }  // this is the primary case to be tested
    51     public <T> void test2(T t) { }
    52     @Deprecated <T> void test3(T t) { }
    54     public static void main(String... args) throws Exception {
    55         new T6993305().run();
    56     }
    58     void run() throws Exception {
    59         File testSrc = new File(System.getProperty("test.src"));
    61         JavacTool tool = JavacTool.create();
    62         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    64         File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
    65         Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
    66         JavacTask task = tool.getTask(null, fm, null, null, null, fos);
    67         Iterable<? extends CompilationUnitTree> cus = task.parse();
    69         TestScanner s = new TestScanner();
    70         s.scan(cus, task);
    72         if (errors > 0)
    73             throw new Exception(errors + " errors occurred");
    74     }
    76     void error(String msg) {
    77         System.err.println("Error: " + msg);
    78         errors++;
    79     }
    81     int errors;
    83     class TestScanner extends TreeScanner<Void, JavacTask> {
    84         CompilationUnitTree cu;
    85         SourcePositions sourcePositions;
    86         String source;
    88         void show(String label, int pos) {
    89             System.err.println(label + ": " +
    90                     source.substring(pos, Math.min(source.length(), pos + 10)));
    91         }
    93         @Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
    94             cu = tree;
    95             Trees trees = Trees.instance(task);
    96             sourcePositions = trees.getSourcePositions();
    97             try {
    98                 source = String.valueOf(tree.getSourceFile().getCharContent(true));
    99             } catch (IOException e) {
   100                 throw new Error(e);
   101             }
   102             return super.visitCompilationUnit(tree, task);
   103         }
   105         // this is the core of the test
   106         @Override public Void visitMethod(MethodTree tree, JavacTask task) {
   107             String name = String.valueOf(tree.getName());
   108             int pos = source.indexOf(" " + name + "(");
   109             while (source.charAt(pos - 1) != '\n') pos--;
   110             while (source.charAt(pos) == ' ') pos++;
   111             int expectedStart = pos;
   112             int reportedStart = (int) sourcePositions.getStartPosition(cu, tree);
   113             System.err.println("Method " + name
   114                     + " expectedStart:" + expectedStart
   115                     + " reportedStart:" + reportedStart);
   116             if (expectedStart != reportedStart) {
   117                 error("Unexpected value for " + name);
   118                 show("expected", expectedStart);
   119                 show("reported", reportedStart);
   120             }
   121             return super.visitMethod(tree, task);
   122         }
   123     }
   124 }

mercurial