--- a/langtools/test/tools/javac/api/T6412669.java Sat Nov 06 13:53:48 2010 -0700
+++ b/langtools/test/tools/javac/api/T6412669.java Tue Nov 09 17:49:24 2010 -0800
@@ -23,11 +23,12 @@
/*
* @test
- * @bug 6412669
+ * @bug 6412669 6997958
* @summary Should be able to get SourcePositions from 269 world
*/
import java.io.*;
+import java.net.*;
import java.util.*;
import javax.annotation.*;
import javax.annotation.processing.*;
@@ -39,28 +40,59 @@
@SupportedAnnotationTypes("*")
public class T6412669 extends AbstractProcessor {
- public static void main(String... args) throws IOException {
- String testSrc = System.getProperty("test.src", ".");
- String testClasses = System.getProperty("test.classes", ".");
+ public static void main(String... args) throws Exception {
+ File testSrc = new File(System.getProperty("test.src", "."));
+ File testClasses = new File(System.getProperty("test.classes", "."));
+
+ // Determine location of necessary tools classes. Assume all in one place.
+ // Likely candidates are typically tools.jar (when testing JDK build)
+ // or build/classes or dist/javac.jar (when testing langtools, using -Xbootclasspath/p:)
+ File toolsClasses;
+ URL u = T6412669.class.getClassLoader().getResource("com/sun/source/util/JavacTask.class");
+ switch (u.getProtocol()) {
+ case "file":
+ toolsClasses = new File(u.toURI());
+ break;
+ case "jar":
+ String s = u.getFile(); // will be file:path!/entry
+ int sep = s.indexOf("!");
+ toolsClasses = new File(new URI(s.substring(0, sep)));
+ break;
+ default:
+ throw new AssertionError("Cannot locate tools classes");
+ }
+ //System.err.println("toolsClasses: " + toolsClasses);
JavacTool tool = JavacTool.create();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
- fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(new File(testClasses)));
+ fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(testClasses, toolsClasses));
Iterable<? extends JavaFileObject> files =
fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6412669.class.getName()+".java")));
- String[] opts = { "-proc:only", "-processor", T6412669.class.getName(),
- "-classpath", new File(testClasses).getPath() };
- JavacTask task = tool.getTask(null, fm, null, Arrays.asList(opts), null, files);
- if (!task.call())
- throw new AssertionError("test failed");
+ String[] opts = { "-proc:only", "-processor", T6412669.class.getName()};
+ StringWriter sw = new StringWriter();
+ JavacTask task = tool.getTask(sw, fm, null, Arrays.asList(opts), null, files);
+ boolean ok = task.call();
+ String out = sw.toString();
+ if (!out.isEmpty())
+ System.err.println(out);
+ if (!ok)
+ throw new AssertionError("compilation of test program failed");
+ // verify we found an annotated element to exercise the SourcePositions API
+ if (!out.contains("processing element"))
+ throw new AssertionError("expected text not found in compilation output");
}
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Trees trees = Trees.instance(processingEnv);
SourcePositions sp = trees.getSourcePositions();
Messager m = processingEnv.getMessager();
+ m.printMessage(Diagnostic.Kind.NOTE, "processing annotations");
+ int count = 0;
for (TypeElement anno: annotations) {
+ count++;
+ m.printMessage(Diagnostic.Kind.NOTE, " processing annotation " + anno);
for (Element e: roundEnv.getElementsAnnotatedWith(anno)) {
+ m.printMessage(Diagnostic.Kind.NOTE, " processing element " + e);
TreePath p = trees.getPath(e);
long start = sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
long end = sp.getEndPosition(p.getCompilationUnit(), p.getLeaf());
@@ -69,6 +101,8 @@
m.printMessage(k, "test [" + start + "," + end + "]", e);
}
}
+ if (count == 0)
+ m.printMessage(Diagnostic.Kind.NOTE, "no annotations found");
return true;
}