test/tools/javac/api/TestGetElementReference.java

Print this page




  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.net.URI;
  38 import java.util.Arrays;
  39 import java.util.regex.Matcher;
  40 import java.util.regex.Pattern;
  41 import javax.lang.model.element.Element;
  42 import javax.tools.Diagnostic;
  43 import javax.tools.DiagnosticCollector;
  44 import javax.tools.JavaFileObject;
  45 import javax.tools.SimpleJavaFileObject;
  46 import javax.tools.StandardJavaFileManager;
  47 import javax.tools.ToolProvider;
  48 
  49 public class TestGetElementReference {
  50 
  51     public static void main(String... args) throws IOException {
  52         File source = new File(System.getProperty("test.src", "."), "TestGetElementReferenceData.java").getAbsoluteFile();
  53         StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
  54         DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
  55         JavacTask ct = (JavacTask) ToolProvider.getSystemJavaCompiler().getTask(null, null, diagnostics, Arrays.asList("-Xjcov", "-source", "1.8"), null, fm.getJavaFileObjects(source));
  56         Trees trees = Trees.instance(ct);
  57         CompilationUnitTree cut = ct.parse().iterator().next();
  58 
  59         ct.analyze();
  60 
  61         for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
  62             if (d.getKind() == Diagnostic.Kind.ERROR) {
  63                 throw new IllegalStateException("Should have been attributed without errors: " + diagnostics.getDiagnostics());
  64             }
  65         }
  66 
  67         Pattern p = Pattern.compile("/\\*getElement:(.*?)\\*/");
  68         Matcher m = p.matcher(cut.getSourceFile().getCharContent(false));
  69 
  70         while (m.find()) {
  71             TreePath tp = pathFor(trees, cut, m.start() - 1);
  72             Element found = trees.getElement(tp);
  73             String expected = m.group(1);
  74             String actual = found != null ? found.getKind() + ":" + symbolToString(found) : "<null>";
  75 




  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.net.URI;
  38 import java.util.Arrays;
  39 import java.util.regex.Matcher;
  40 import java.util.regex.Pattern;
  41 import javax.lang.model.element.Element;
  42 import javax.tools.Diagnostic;
  43 import javax.tools.DiagnosticCollector;
  44 import javax.tools.JavaFileObject;
  45 import javax.tools.SimpleJavaFileObject;
  46 import javax.tools.StandardJavaFileManager;
  47 import javax.tools.ToolProvider;
  48 
  49 public class TestGetElementReference {
  50 
  51     public static void main(String... args) throws IOException {
  52         File source = new File(System.getProperty("test.src", "."), "TestGetElementReferenceData.java").getAbsoluteFile();
  53         StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
  54         DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
  55         JavacTask ct = (JavacTask) ToolProvider.getSystemJavaCompiler().getTask(null, null, diagnostics, Arrays.asList("-Xjcov"), null, fm.getJavaFileObjects(source));
  56         Trees trees = Trees.instance(ct);
  57         CompilationUnitTree cut = ct.parse().iterator().next();
  58 
  59         ct.analyze();
  60 
  61         for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
  62             if (d.getKind() == Diagnostic.Kind.ERROR) {
  63                 throw new IllegalStateException("Should have been attributed without errors: " + diagnostics.getDiagnostics());
  64             }
  65         }
  66 
  67         Pattern p = Pattern.compile("/\\*getElement:(.*?)\\*/");
  68         Matcher m = p.matcher(cut.getSourceFile().getCharContent(false));
  69 
  70         while (m.find()) {
  71             TreePath tp = pathFor(trees, cut, m.start() - 1);
  72             Element found = trees.getElement(tp);
  73             String expected = m.group(1);
  74             String actual = found != null ? found.getKind() + ":" + symbolToString(found) : "<null>";
  75