1 10 11 package com.sun.source.util; 12 13 import java.lang.reflect.Method ; 14 import javax.annotation.processing.ProcessingEnvironment; 15 import javax.lang.model.element.AnnotationMirror; 16 import javax.lang.model.element.AnnotationValue; 17 import javax.lang.model.element.Element; 18 import javax.lang.model.element.ExecutableElement; 19 import javax.lang.model.element.TypeElement; 20 import javax.lang.model.type.DeclaredType; 21 import javax.lang.model.type.TypeMirror; 22 import javax.tools.JavaCompiler.CompilationTask; 23 24 import com.sun.source.tree.ClassTree; 25 import com.sun.source.tree.CompilationUnitTree; 26 import com.sun.source.tree.MethodTree; 27 import com.sun.source.tree.Scope; 28 import com.sun.source.tree.Tree; 29 30 35 public abstract class Trees { 36 40 public static Trees instance(CompilationTask task) { 41 if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl")) 42 throw new IllegalArgumentException (); 43 return getJavacTrees(CompilationTask.class, task); 44 } 45 46 50 public static Trees instance(ProcessingEnvironment env) { 51 if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) 52 throw new IllegalArgumentException (); 53 return getJavacTrees(ProcessingEnvironment.class, env); 54 } 55 56 private static Trees getJavacTrees(Class <?> argType, Object arg) { 57 try { 58 ClassLoader cl = arg.getClass().getClassLoader(); 59 Class <?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl); 60 argType = Class.forName(argType.getName(), false, cl); 61 Method m = c.getMethod("instance", new Class [] { argType }); 62 return (Trees) m.invoke(null, new Object [] { arg }); 63 } catch (Throwable e) { 64 throw new AssertionError (e); 65 } 66 } 67 68 71 public abstract SourcePositions getSourcePositions(); 72 73 77 public abstract Tree getTree(Element element); 78 79 83 public abstract ClassTree getTree(TypeElement element); 84 85 89 public abstract MethodTree getTree(ExecutableElement method); 90 91 95 public abstract Tree getTree(Element e, AnnotationMirror a); 96 97 101 public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v); 102 103 106 public abstract TreePath getPath(CompilationUnitTree unit, Tree node); 107 108 112 public abstract TreePath getPath(Element e); 113 114 118 public abstract TreePath getPath(Element e, AnnotationMirror a); 119 120 124 public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v); 125 126 132 public abstract Element getElement(TreePath path); 133 134 140 public abstract TypeMirror getTypeMirror(TreePath path); 141 142 146 public abstract Scope getScope(TreePath path); 147 148 154 public abstract boolean isAccessible(Scope scope, TypeElement type); 155 156 164 public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type); 165 } 166 | Popular Tags |