langtools/test/tools/javac/api/T6412669.java
changeset 7207 e68a36c83fd2
parent 5520 86e4b9a9da40
child 7681 1f0819a3341f
equal deleted inserted replaced
7206:02edd110358f 7207:e68a36c83fd2
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 6412669
    26  * @bug 6412669 6997958
    27  * @summary Should be able to get SourcePositions from 269 world
    27  * @summary Should be able to get SourcePositions from 269 world
    28  */
    28  */
    29 
    29 
    30 import java.io.*;
    30 import java.io.*;
       
    31 import java.net.*;
    31 import java.util.*;
    32 import java.util.*;
    32 import javax.annotation.*;
    33 import javax.annotation.*;
    33 import javax.annotation.processing.*;
    34 import javax.annotation.processing.*;
    34 import javax.lang.model.*;
    35 import javax.lang.model.*;
    35 import javax.lang.model.element.*;
    36 import javax.lang.model.element.*;
    37 import com.sun.source.util.*;
    38 import com.sun.source.util.*;
    38 import com.sun.tools.javac.api.*;
    39 import com.sun.tools.javac.api.*;
    39 
    40 
    40 @SupportedAnnotationTypes("*")
    41 @SupportedAnnotationTypes("*")
    41 public class T6412669 extends AbstractProcessor {
    42 public class T6412669 extends AbstractProcessor {
    42     public static void main(String... args) throws IOException {
    43     public static void main(String... args) throws Exception {
    43         String testSrc = System.getProperty("test.src", ".");
    44         File testSrc = new File(System.getProperty("test.src", "."));
    44         String testClasses = System.getProperty("test.classes", ".");
    45         File testClasses = new File(System.getProperty("test.classes", "."));
       
    46 
       
    47         // Determine location of necessary tools classes. Assume all in one place.
       
    48         // Likely candidates are typically tools.jar (when testing JDK build)
       
    49         // or build/classes or dist/javac.jar (when testing langtools, using -Xbootclasspath/p:)
       
    50         File toolsClasses;
       
    51         URL u = T6412669.class.getClassLoader().getResource("com/sun/source/util/JavacTask.class");
       
    52         switch (u.getProtocol()) {
       
    53             case "file":
       
    54                 toolsClasses = new File(u.toURI());
       
    55                 break;
       
    56             case "jar":
       
    57                 String s = u.getFile(); // will be file:path!/entry
       
    58                 int sep = s.indexOf("!");
       
    59                 toolsClasses = new File(new URI(s.substring(0, sep)));
       
    60                 break;
       
    61             default:
       
    62                 throw new AssertionError("Cannot locate tools classes");
       
    63         }
       
    64         //System.err.println("toolsClasses: " + toolsClasses);
    45 
    65 
    46         JavacTool tool = JavacTool.create();
    66         JavacTool tool = JavacTool.create();
    47         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    67         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    48         fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(new File(testClasses)));
    68         fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(testClasses, toolsClasses));
    49         Iterable<? extends JavaFileObject> files =
    69         Iterable<? extends JavaFileObject> files =
    50             fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6412669.class.getName()+".java")));
    70             fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6412669.class.getName()+".java")));
    51         String[] opts = { "-proc:only", "-processor", T6412669.class.getName(),
    71         String[] opts = { "-proc:only", "-processor", T6412669.class.getName()};
    52                           "-classpath", new File(testClasses).getPath() };
    72         StringWriter sw = new StringWriter();
    53         JavacTask task = tool.getTask(null, fm, null, Arrays.asList(opts), null, files);
    73         JavacTask task = tool.getTask(sw, fm, null, Arrays.asList(opts), null, files);
    54         if (!task.call())
    74         boolean ok = task.call();
    55             throw new AssertionError("test failed");
    75         String out = sw.toString();
       
    76         if (!out.isEmpty())
       
    77             System.err.println(out);
       
    78         if (!ok)
       
    79             throw new AssertionError("compilation of test program failed");
       
    80         // verify we found an annotated element to exercise the SourcePositions API
       
    81         if (!out.contains("processing element"))
       
    82             throw new AssertionError("expected text not found in compilation output");
    56     }
    83     }
    57 
    84 
    58     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    85     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    59         Trees trees = Trees.instance(processingEnv);
    86         Trees trees = Trees.instance(processingEnv);
    60         SourcePositions sp = trees.getSourcePositions();
    87         SourcePositions sp = trees.getSourcePositions();
    61         Messager m = processingEnv.getMessager();
    88         Messager m = processingEnv.getMessager();
       
    89         m.printMessage(Diagnostic.Kind.NOTE, "processing annotations");
       
    90         int count = 0;
    62         for (TypeElement anno: annotations) {
    91         for (TypeElement anno: annotations) {
       
    92             count++;
       
    93             m.printMessage(Diagnostic.Kind.NOTE, "  processing annotation " + anno);
    63             for (Element e: roundEnv.getElementsAnnotatedWith(anno)) {
    94             for (Element e: roundEnv.getElementsAnnotatedWith(anno)) {
       
    95                 m.printMessage(Diagnostic.Kind.NOTE, "    processing element " + e);
    64                 TreePath p = trees.getPath(e);
    96                 TreePath p = trees.getPath(e);
    65                 long start = sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
    97                 long start = sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
    66                 long end = sp.getEndPosition(p.getCompilationUnit(), p.getLeaf());
    98                 long end = sp.getEndPosition(p.getCompilationUnit(), p.getLeaf());
    67                 Diagnostic.Kind k = (start > 0 && end > 0 && start < end
    99                 Diagnostic.Kind k = (start > 0 && end > 0 && start < end
    68                                      ? Diagnostic.Kind.NOTE : Diagnostic.Kind.ERROR);
   100                                      ? Diagnostic.Kind.NOTE : Diagnostic.Kind.ERROR);
    69                 m.printMessage(k, "test [" + start + "," + end + "]", e);
   101                 m.printMessage(k, "test [" + start + "," + end + "]", e);
    70             }
   102             }
    71         }
   103         }
       
   104         if (count == 0)
       
   105             m.printMessage(Diagnostic.Kind.NOTE, "no annotations found");
    72         return true;
   106         return true;
    73     }
   107     }
    74 
   108 
    75     @Override
   109     @Override
    76     public SourceVersion getSupportedSourceVersion() {
   110     public SourceVersion getSupportedSourceVersion() {