Method from com.sun.tools.javac.model.JavacElements Detail: |
public List<Compound> getAllAnnotationMirrors(Element e) {
Symbol sym = cast(Symbol.class, e);
List< Attribute.Compound > annos = sym.getAnnotationMirrors();
while (sym.getKind() == ElementKind.CLASS) {
Type sup = ((ClassSymbol) sym).getSuperclass();
if (sup.tag != TypeTags.CLASS || sup.isErroneous() ||
sup.tsym == syms.objectType.tsym) {
break;
}
sym = sup.tsym;
List< Attribute.Compound > oldAnnos = annos;
for (Attribute.Compound anno : sym.getAnnotationMirrors()) {
if (isInherited(anno.type) &&
!containsAnnoOfType(oldAnnos, anno.type)) {
annos = annos.prepend(anno);
}
}
}
return annos;
}
Returns all annotations of an element, whether
inherited or directly present. |
public FilteredMemberList getAllMembers(TypeElement element) {
Symbol sym = cast(Symbol.class, element);
Scope scope = sym.members().dupUnshared();
List< Type > closure = types.closure(sym.asType());
for (Type t : closure)
addMembers(scope, t);
return new FilteredMemberList(scope);
}
|
public static A getAnnotation(Symbol annotated,
Class<A> annoType) {
if (!annoType.isAnnotation())
throw new IllegalArgumentException("Not an annotation type: "
+ annoType);
String name = annoType.getName();
for (Attribute.Compound anno : annotated.getAnnotationMirrors())
if (name.equals(anno.type.tsym.flatName().toString()))
return AnnotationProxyMaker.generateAnnotation(anno, annoType);
return null;
}
An internal-use utility that creates a reified annotation. |
public static A getAnnotation(ClassSymbol annotated,
Class<A> annoType) {
boolean inherited = annoType.isAnnotationPresent(Inherited.class);
A result = null;
while (annotated.name != annotated.name.table.names.java_lang_Object) {
result = getAnnotation((Symbol)annotated, annoType);
if (result != null || !inherited)
break;
Type sup = annotated.getSuperclass();
if (sup.tag != TypeTags.CLASS || sup.isErroneous())
break;
annotated = (ClassSymbol) sup.tsym;
}
return result;
}
An internal-use utility that creates a reified annotation.
This overloaded version take annotation inheritance into account. |
public Name getBinaryName(TypeElement type) {
return cast(TypeSymbol.class, type).flatName();
}
|
public String getConstantExpression(Object value) {
return Constants.format(value);
}
|
public String getDocComment(Element e) {
// Our doc comment is contained in a map in our toplevel,
// indexed by our tree. Find our enter environment, which gives
// us our toplevel. It also gives us a tree that contains our
// tree: walk it to find our tree. This is painful.
Pair< JCTree, JCCompilationUnit > treeTop = getTreeAndTopLevel(e);
if (treeTop == null)
return null;
JCTree tree = treeTop.fst;
JCCompilationUnit toplevel = treeTop.snd;
if (toplevel.docComments == null)
return null;
return toplevel.docComments.get(tree);
}
|
public Map<MethodSymbol, Attribute> getElementValuesWithDefaults(AnnotationMirror a) {
Attribute.Compound anno = cast(Attribute.Compound.class, a);
DeclaredType annotype = a.getAnnotationType();
Map< MethodSymbol, Attribute > valmap = anno.getElementValues();
for (ExecutableElement ex :
methodsIn(annotype.asElement().getEnclosedElements())) {
MethodSymbol meth = (MethodSymbol) ex;
Attribute defaultValue = meth.getDefaultValue();
if (defaultValue != null && !valmap.containsKey(meth)) {
valmap.put(meth, defaultValue);
}
}
return valmap;
}
|
public Name getName(CharSequence cs) {
return names.fromString(cs.toString());
}
|
public PackageSymbol getPackageElement(CharSequence name) {
String strName = name.toString();
if (strName.equals(""))
return syms.unnamedPackage;
return SourceVersion.isName(strName)
? nameToSymbol(strName, PackageSymbol.class)
: null;
}
|
public PackageElement getPackageOf(Element e) {
return cast(Symbol.class, e).packge();
}
|
public JavacSourcePosition getSourcePosition(Element e) {
Pair< JCTree, JCCompilationUnit > treeTop = getTreeAndTopLevel(e);
if (treeTop == null)
return null;
JCTree tree = treeTop.fst;
JCCompilationUnit toplevel = treeTop.snd;
JavaFileObject sourcefile = toplevel.sourcefile;
if (sourcefile == null)
return null;
return new JavacSourcePosition(sourcefile, tree.pos, toplevel.lineMap);
}
|
public JavacSourcePosition getSourcePosition(Element e,
AnnotationMirror a) {
Pair< JCTree, JCCompilationUnit > treeTop = getTreeAndTopLevel(e);
if (treeTop == null)
return null;
JCTree tree = treeTop.fst;
JCCompilationUnit toplevel = treeTop.snd;
JavaFileObject sourcefile = toplevel.sourcefile;
if (sourcefile == null)
return null;
JCTree annoTree = matchAnnoToTree(a, e, tree);
if (annoTree == null)
return null;
return new JavacSourcePosition(sourcefile, annoTree.pos,
toplevel.lineMap);
}
|
public JavacSourcePosition getSourcePosition(Element e,
AnnotationMirror a,
AnnotationValue v) {
// TODO: better accuracy in getSourcePosition(... AnnotationValue)
return getSourcePosition(e, a);
}
|
public JCTree getTree(Element e) {
Pair< JCTree, ? > treeTop = getTreeAndTopLevel(e);
return (treeTop != null) ? treeTop.fst : null;
}
Returns the tree node corresponding to this element, or null
if none can be found. |
public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e,
AnnotationMirror a,
AnnotationValue v) {
if (e == null)
return null;
Pair< JCTree, JCCompilationUnit > elemTreeTop = getTreeAndTopLevel(e);
if (elemTreeTop == null)
return null;
if (a == null)
return elemTreeTop;
JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst);
if (annoTree == null)
return elemTreeTop;
// 6388543: if v != null, we should search within annoTree to find
// the tree matching v. For now, we ignore v and return the tree of
// the annotation.
return new Pair< JCTree, JCCompilationUnit >(annoTree, elemTreeTop.snd);
}
Returns the best approximation for the tree node and compilation unit
corresponding to the given element, annotation and value.
If the element is null, null is returned.
If the annotation is null or cannot be found, the tree node and
compilation unit for the element is returned.
If the annotation value is null or cannot be found, the tree node and
compilation unit for the annotation is returned. |
public ClassSymbol getTypeElement(CharSequence name) {
String strName = name.toString();
return SourceVersion.isName(strName)
? nameToSymbol(strName, ClassSymbol.class)
: null;
}
|
public boolean hides(Element hiderEl,
Element hideeEl) {
Symbol hider = cast(Symbol.class, hiderEl);
Symbol hidee = cast(Symbol.class, hideeEl);
// Fields only hide fields; methods only methods; types only types.
// Names must match. Nothing hides itself (just try it).
if (hider == hidee ||
hider.kind != hidee.kind ||
hider.name != hidee.name) {
return false;
}
// Only static methods can hide other methods.
// Methods only hide methods with matching signatures.
if (hider.kind == Kinds.MTH) {
if (!hider.isStatic() ||
!types.isSubSignature(hider.type, hidee.type)) {
return false;
}
}
// Hider must be in a subclass of hidee's class.
// Note that if M1 hides M2, and M2 hides M3, and M3 is accessible
// in M1's class, then M1 and M2 both hide M3.
ClassSymbol hiderClass = hider.owner.enclClass();
ClassSymbol hideeClass = hidee.owner.enclClass();
if (hiderClass == null || hideeClass == null ||
!hiderClass.isSubClass(hideeClass, types)) {
return false;
}
// Hidee must be accessible in hider's class.
// The method isInheritedIn is poorly named: it checks only access.
return hidee.isInheritedIn(hiderClass, types);
}
|
public static JavacElements instance(Context context) {
JavacElements instance = context.get(JavacElements.class);
if (instance == null)
instance = new JavacElements(context);
return instance;
}
|
public boolean isDeprecated(Element e) {
Symbol sym = cast(Symbol.class, e);
return (sym.flags() & Flags.DEPRECATED) != 0;
}
|
public boolean overrides(ExecutableElement riderEl,
ExecutableElement rideeEl,
TypeElement typeEl) {
MethodSymbol rider = cast(MethodSymbol.class, riderEl);
MethodSymbol ridee = cast(MethodSymbol.class, rideeEl);
ClassSymbol origin = cast(ClassSymbol.class, typeEl);
return rider.name == ridee.name &&
// not reflexive as per JLS
rider != ridee &&
// we don't care if ridee is static, though that wouldn't
// compile
!rider.isStatic() &&
// Symbol.overrides assumes the following
ridee.isMemberOf(origin, types) &&
// check access and signatures; don't check return types
rider.overrides(ridee, origin, types, false);
}
|
public void printElements(Writer w,
Element elements) {
for (Element element : elements)
(new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush();
}
Print a representation of the elements to the given writer in
the specified order. The main purpose of this method is for
diagnostics. The exact format of the output is not
specified and is subject to change. |
public void setContext(Context context) {
context.put(JavacElements.class, this);
javaCompiler = JavaCompiler.instance(context);
syms = Symtab.instance(context);
names = Names.instance(context);
types = Types.instance(context);
enter = Enter.instance(context);
}
Use a new context. May be called from outside to update
internal state for a new annotation-processing round. |