test/langtools/tools/javac/positions/TreeEndPosTest.java
changeset 50898 12133a6e2613
parent 49518 d0ff431a596e
equal deleted inserted replaced
50897:a4d7eaf58623 50898:12133a6e2613
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8017216 8019422 8019421 8054956
    26  * @bug 8017216 8019422 8019421 8054956 8205418
    27  * @summary verify start and end positions
    27  * @summary verify start and end positions
    28  * @modules java.compiler
    28  * @modules java.compiler
    29  *          jdk.compiler
    29  *          jdk.compiler
    30  * @run main TreeEndPosTest
    30  * @run main TreeEndPosTest
    31  */
    31  */
    42 import javax.tools.JavaCompiler;
    42 import javax.tools.JavaCompiler;
    43 import javax.tools.JavaFileManager;
    43 import javax.tools.JavaFileManager;
    44 import javax.tools.JavaFileObject;
    44 import javax.tools.JavaFileObject;
    45 import javax.tools.SimpleJavaFileObject;
    45 import javax.tools.SimpleJavaFileObject;
    46 import javax.tools.ToolProvider;
    46 import javax.tools.ToolProvider;
       
    47 import com.sun.source.tree.CompilationUnitTree;
       
    48 import com.sun.source.tree.Tree;
       
    49 import com.sun.source.tree.Tree.Kind;
       
    50 import com.sun.source.util.JavacTask;
       
    51 import com.sun.source.util.SourcePositions;
       
    52 import com.sun.source.util.TreeScanner;
       
    53 import com.sun.source.util.Trees;
    47 
    54 
    48 public class TreeEndPosTest {
    55 public class TreeEndPosTest {
    49     private static JavaFileManager getJavaFileManager(JavaCompiler compiler,
    56     private static JavaFileManager getJavaFileManager(JavaCompiler compiler,
    50             DiagnosticCollector dc) {
    57             DiagnosticCollector dc) {
    51         return compiler.getStandardFileManager(dc, null, null);
    58         return compiler.getStandardFileManager(dc, null, null);
    97             JavaSource js = new JavaSource(name + ".java", code.toString());
   104             JavaSource js = new JavaSource(name + ".java", code.toString());
    98             js.startPos = start;
   105             js.startPos = start;
    99             js.endPos = end;
   106             js.endPos = end;
   100             return js;
   107             return js;
   101         }
   108         }
       
   109 
       
   110         static JavaSource createFullJavaSource(String code) {
       
   111             final String name = "Bug";
       
   112             String[] parts = code.split("\\|", 3);
       
   113             JavaSource js = new JavaSource(name + ".java", parts[0] + parts[1] + parts[2]);
       
   114             js.startPos = parts[0].length();
       
   115             js.endPos = parts[0].length() + parts[1].length();
       
   116             return js;
       
   117         }
   102     }
   118     }
   103 
   119 
   104     public static void main(String... args) throws IOException {
   120     public static void main(String... args) throws IOException {
   105         testUninitializedVariable();
   121         testUninitializedVariable();
   106         testMissingAnnotationValue();
   122         testMissingAnnotationValue();
   107         testUnresolvableAnnotationAttribute();
   123         testUnresolvableAnnotationAttribute();
   108         testFinalVariableWithDefaultConstructor();
   124         testFinalVariableWithDefaultConstructor();
   109         testFinalVariableWithConstructor();
   125         testFinalVariableWithConstructor();
       
   126         testWholeTextSpan();
   110     }
   127     }
   111 
   128 
   112     static void testUninitializedVariable() throws IOException {
   129     static void testUninitializedVariable() throws IOException {
   113         compile(JavaSource.createJavaSource("Object o = new A().new B(); class A { }",
   130         compile(JavaSource.createJavaSource("Object o = new A().new B(); class A { }",
   114                 "B()"));
   131                 "B()"));
   129     }
   146     }
   130 
   147 
   131     static void testFinalVariableWithConstructor() throws IOException {
   148     static void testFinalVariableWithConstructor() throws IOException {
   132         compile(JavaSource.createJavaSource("public Bug (){} private static final String Foo; public void bar() { }",
   149         compile(JavaSource.createJavaSource("public Bug (){} private static final String Foo; public void bar() { }",
   133                 "{}"));
   150                 "{}"));
       
   151     }
       
   152 
       
   153     static void testWholeTextSpan() throws IOException {
       
   154         treeSpan(JavaSource.createFullJavaSource("|class X    |"));
   134     }
   155     }
   135 
   156 
   136     static void compile(JavaSource src) throws IOException {
   157     static void compile(JavaSource src) throws IOException {
   137         ByteArrayOutputStream ba = new ByteArrayOutputStream();
   158         ByteArrayOutputStream ba = new ByteArrayOutputStream();
   138         PrintWriter writer = new PrintWriter(ba);
   159         PrintWriter writer = new PrintWriter(ba);
   167                     throw new RuntimeException("error: trees don't match");
   188                     throw new RuntimeException("error: trees don't match");
   168                 }
   189                 }
   169             }
   190             }
   170         }
   191         }
   171     }
   192     }
       
   193 
       
   194     static void treeSpan(JavaSource src) throws IOException {
       
   195         ByteArrayOutputStream ba = new ByteArrayOutputStream();
       
   196         PrintWriter writer = new PrintWriter(ba);
       
   197         File tempDir = new File(".");
       
   198         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       
   199         DiagnosticCollector dc = new DiagnosticCollector();
       
   200         try (JavaFileManager javaFileManager = getJavaFileManager(compiler, dc)) {
       
   201             List<String> options = new ArrayList<>();
       
   202             options.add("-cp");
       
   203             options.add(tempDir.getPath());
       
   204             options.add("-d");
       
   205             options.add(tempDir.getPath());
       
   206             options.add("--should-stop=at=GENERATE");
       
   207 
       
   208             List<JavaFileObject> sources = new ArrayList<>();
       
   209             sources.add(src);
       
   210             JavacTask task = (JavacTask) compiler.getTask(writer, javaFileManager,
       
   211                                                           dc, options, null,
       
   212                                                           sources);
       
   213             SourcePositions sp = Trees.instance(task).getSourcePositions();
       
   214             boolean[] found = new boolean[1];
       
   215             new TreeScanner<Void, Void>() {
       
   216                 CompilationUnitTree cut;
       
   217                 @Override
       
   218                 public Void scan(Tree tree, Void p) {
       
   219                     if (tree == null)
       
   220                         return null;
       
   221                     if (tree.getKind() == Kind.COMPILATION_UNIT) {
       
   222                         cut = (CompilationUnitTree) tree;
       
   223                     }
       
   224                     found[0] |= (sp.getStartPosition(cut, tree) == src.startPos) &&
       
   225                                 (sp.getEndPosition(cut, tree) == src.endPos);
       
   226                     return super.scan(tree, p);
       
   227                 }
       
   228             }.scan(task.parse(), null);
       
   229 
       
   230             if (!found[0]) {
       
   231                 throw new IllegalStateException();
       
   232             }
       
   233         }
       
   234     }
   172 }
   235 }