6997958: test tools/javac/api/T6412669.java fails in PIT

Tue, 09 Nov 2010 17:49:24 -0800

author
jjg
date
Tue, 09 Nov 2010 17:49:24 -0800
changeset 739
a0d9d642f65b
parent 738
9427a3c795fc
child 740
bce19889597e

6997958: test tools/javac/api/T6412669.java fails in PIT
Reviewed-by: darcy

test/tools/javac/api/T6412669.java file | annotate | diff | comparison | revisions
     1.1 --- a/test/tools/javac/api/T6412669.java	Sat Nov 06 13:53:48 2010 -0700
     1.2 +++ b/test/tools/javac/api/T6412669.java	Tue Nov 09 17:49:24 2010 -0800
     1.3 @@ -23,11 +23,12 @@
     1.4  
     1.5  /*
     1.6   * @test
     1.7 - * @bug 6412669
     1.8 + * @bug 6412669 6997958
     1.9   * @summary Should be able to get SourcePositions from 269 world
    1.10   */
    1.11  
    1.12  import java.io.*;
    1.13 +import java.net.*;
    1.14  import java.util.*;
    1.15  import javax.annotation.*;
    1.16  import javax.annotation.processing.*;
    1.17 @@ -39,28 +40,59 @@
    1.18  
    1.19  @SupportedAnnotationTypes("*")
    1.20  public class T6412669 extends AbstractProcessor {
    1.21 -    public static void main(String... args) throws IOException {
    1.22 -        String testSrc = System.getProperty("test.src", ".");
    1.23 -        String testClasses = System.getProperty("test.classes", ".");
    1.24 +    public static void main(String... args) throws Exception {
    1.25 +        File testSrc = new File(System.getProperty("test.src", "."));
    1.26 +        File testClasses = new File(System.getProperty("test.classes", "."));
    1.27 +
    1.28 +        // Determine location of necessary tools classes. Assume all in one place.
    1.29 +        // Likely candidates are typically tools.jar (when testing JDK build)
    1.30 +        // or build/classes or dist/javac.jar (when testing langtools, using -Xbootclasspath/p:)
    1.31 +        File toolsClasses;
    1.32 +        URL u = T6412669.class.getClassLoader().getResource("com/sun/source/util/JavacTask.class");
    1.33 +        switch (u.getProtocol()) {
    1.34 +            case "file":
    1.35 +                toolsClasses = new File(u.toURI());
    1.36 +                break;
    1.37 +            case "jar":
    1.38 +                String s = u.getFile(); // will be file:path!/entry
    1.39 +                int sep = s.indexOf("!");
    1.40 +                toolsClasses = new File(new URI(s.substring(0, sep)));
    1.41 +                break;
    1.42 +            default:
    1.43 +                throw new AssertionError("Cannot locate tools classes");
    1.44 +        }
    1.45 +        //System.err.println("toolsClasses: " + toolsClasses);
    1.46  
    1.47          JavacTool tool = JavacTool.create();
    1.48          StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    1.49 -        fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(new File(testClasses)));
    1.50 +        fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(testClasses, toolsClasses));
    1.51          Iterable<? extends JavaFileObject> files =
    1.52              fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6412669.class.getName()+".java")));
    1.53 -        String[] opts = { "-proc:only", "-processor", T6412669.class.getName(),
    1.54 -                          "-classpath", new File(testClasses).getPath() };
    1.55 -        JavacTask task = tool.getTask(null, fm, null, Arrays.asList(opts), null, files);
    1.56 -        if (!task.call())
    1.57 -            throw new AssertionError("test failed");
    1.58 +        String[] opts = { "-proc:only", "-processor", T6412669.class.getName()};
    1.59 +        StringWriter sw = new StringWriter();
    1.60 +        JavacTask task = tool.getTask(sw, fm, null, Arrays.asList(opts), null, files);
    1.61 +        boolean ok = task.call();
    1.62 +        String out = sw.toString();
    1.63 +        if (!out.isEmpty())
    1.64 +            System.err.println(out);
    1.65 +        if (!ok)
    1.66 +            throw new AssertionError("compilation of test program failed");
    1.67 +        // verify we found an annotated element to exercise the SourcePositions API
    1.68 +        if (!out.contains("processing element"))
    1.69 +            throw new AssertionError("expected text not found in compilation output");
    1.70      }
    1.71  
    1.72      public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.73          Trees trees = Trees.instance(processingEnv);
    1.74          SourcePositions sp = trees.getSourcePositions();
    1.75          Messager m = processingEnv.getMessager();
    1.76 +        m.printMessage(Diagnostic.Kind.NOTE, "processing annotations");
    1.77 +        int count = 0;
    1.78          for (TypeElement anno: annotations) {
    1.79 +            count++;
    1.80 +            m.printMessage(Diagnostic.Kind.NOTE, "  processing annotation " + anno);
    1.81              for (Element e: roundEnv.getElementsAnnotatedWith(anno)) {
    1.82 +                m.printMessage(Diagnostic.Kind.NOTE, "    processing element " + e);
    1.83                  TreePath p = trees.getPath(e);
    1.84                  long start = sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
    1.85                  long end = sp.getEndPosition(p.getCompilationUnit(), p.getLeaf());
    1.86 @@ -69,6 +101,8 @@
    1.87                  m.printMessage(k, "test [" + start + "," + end + "]", e);
    1.88              }
    1.89          }
    1.90 +        if (count == 0)
    1.91 +            m.printMessage(Diagnostic.Kind.NOTE, "no annotations found");
    1.92          return true;
    1.93      }
    1.94  

mercurial