duke@1: /* jfranck@1491: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.model; duke@1: duke@1: import java.lang.annotation.Annotation; duke@1: import java.lang.annotation.Inherited; jfranck@1491: import java.lang.reflect.InvocationTargetException; jfranck@1491: import java.lang.reflect.Method; duke@1: import java.util.Map; jjg@1280: duke@1: import javax.lang.model.SourceVersion; duke@1: import javax.lang.model.element.*; duke@1: import javax.lang.model.type.DeclaredType; duke@1: import javax.lang.model.util.Elements; duke@1: import javax.tools.JavaFileObject; jjg@1280: import static javax.lang.model.util.ElementFilter.methodsIn; jjg@1280: duke@1: import com.sun.tools.javac.code.*; duke@1: import com.sun.tools.javac.code.Symbol.*; jjg@1374: import com.sun.tools.javac.code.TypeTag; duke@1: import com.sun.tools.javac.comp.AttrContext; duke@1: import com.sun.tools.javac.comp.Enter; duke@1: import com.sun.tools.javac.comp.Env; duke@1: import com.sun.tools.javac.main.JavaCompiler; duke@1: import com.sun.tools.javac.processing.PrintingProcessor; duke@1: import com.sun.tools.javac.tree.JCTree; duke@1: import com.sun.tools.javac.tree.JCTree.*; duke@1: import com.sun.tools.javac.tree.TreeInfo; duke@1: import com.sun.tools.javac.tree.TreeScanner; jjg@113: import com.sun.tools.javac.util.*; duke@1: import com.sun.tools.javac.util.Name; jjg@1374: import static com.sun.tools.javac.code.TypeTag.CLASS; jjg@1127: import static com.sun.tools.javac.tree.JCTree.Tag.*; duke@1: duke@1: /** duke@1: * Utility methods for operating on program elements. duke@1: * jjg@581: *

This is NOT part of any supported API. duke@1: * If you write code that depends on this, you do so at your own duke@1: * risk. This code and its internal interfaces are subject to change duke@1: * or deletion without notice.

duke@1: */ duke@1: public class JavacElements implements Elements { duke@1: duke@1: private JavaCompiler javaCompiler; duke@1: private Symtab syms; jjg@113: private Names names; duke@1: private Types types; duke@1: private Enter enter; duke@1: duke@1: public static JavacElements instance(Context context) { jjg@706: JavacElements instance = context.get(JavacElements.class); jjg@706: if (instance == null) duke@1: instance = new JavacElements(context); duke@1: return instance; duke@1: } duke@1: duke@1: /** duke@1: * Public for use only by JavacProcessingEnvironment duke@1: */ jjg@706: protected JavacElements(Context context) { duke@1: setContext(context); duke@1: } duke@1: duke@1: /** duke@1: * Use a new context. May be called from outside to update duke@1: * internal state for a new annotation-processing round. duke@1: */ duke@1: public void setContext(Context context) { jjg@706: context.put(JavacElements.class, this); duke@1: javaCompiler = JavaCompiler.instance(context); duke@1: syms = Symtab.instance(context); jjg@113: names = Names.instance(context); duke@1: types = Types.instance(context); duke@1: enter = Enter.instance(context); duke@1: } duke@1: duke@1: /** jfranck@1491: * An internal-use utility that creates a runtime view of an jfranck@1491: * annotation. This is the implementation of jfranck@1491: * Element.getAnnotation(Class). duke@1: */ duke@1: public static A getAnnotation(Symbol annotated, duke@1: Class annoType) { duke@1: if (!annoType.isAnnotation()) duke@1: throw new IllegalArgumentException("Not an annotation type: " duke@1: + annoType); jfranck@1491: Attribute.Compound c; jfranck@1491: if (annotated.kind == Kinds.TYP && annotated instanceof ClassSymbol) { jfranck@1491: c = getAttributeOnClass((ClassSymbol)annotated, annoType); jfranck@1491: } else { jfranck@1491: c = getAttribute(annotated, annoType); jfranck@1491: } jfranck@1491: return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType); jfranck@1491: } jfranck@1491: jfranck@1491: // Helper to getAnnotation[s] jfranck@1491: private static Attribute.Compound getAttribute(Symbol annotated, jfranck@1491: Class annoType) { duke@1: String name = annoType.getName(); jfranck@1491: jfranck@1491: for (Attribute.Compound anno : annotated.getRawAttributes()) duke@1: if (name.equals(anno.type.tsym.flatName().toString())) jfranck@1491: return anno; jfranck@1491: duke@1: return null; duke@1: } jfranck@1491: // Helper to getAnnotation[s] jfranck@1491: private static Attribute.Compound getAttributeOnClass(ClassSymbol annotated, jfranck@1491: Class annoType) { duke@1: boolean inherited = annoType.isAnnotationPresent(Inherited.class); jfranck@1491: Attribute.Compound result = null; jjg@113: while (annotated.name != annotated.name.table.names.java_lang_Object) { jfranck@1491: result = getAttribute(annotated, annoType); duke@1: if (result != null || !inherited) duke@1: break; duke@1: Type sup = annotated.getSuperclass(); jjg@1374: if (!sup.hasTag(CLASS) || sup.isErroneous()) duke@1: break; duke@1: annotated = (ClassSymbol) sup.tsym; duke@1: } duke@1: return result; duke@1: } duke@1: jfranck@1491: /** jfranck@1491: * An internal-use utility that creates a runtime view of jfranck@1491: * annotations. This is the implementation of jfranck@1491: * Element.getAnnotations(Class). jfranck@1491: */ jfranck@1491: public static A[] getAnnotations(Symbol annotated, jfranck@1491: Class annoType) { jfranck@1491: if (!annoType.isAnnotation()) jfranck@1491: throw new IllegalArgumentException("Not an annotation type: " jfranck@1491: + annoType); jfranck@1491: // If annoType does not declare a container this is equivalent to wrapping jfranck@1491: // getAnnotation(...) in an array. jfranck@1491: Class containerType = getContainer(annoType); jfranck@1491: if (containerType == null) { jfranck@1491: A res = getAnnotation(annotated, annoType); jfranck@1491: int size; jfranck@1491: if (res == null) { jfranck@1491: size = 0; jfranck@1491: } else { jfranck@1491: size = 1; jfranck@1491: } jfranck@1491: @SuppressWarnings("unchecked") // annoType is the Class for A jfranck@1491: A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); jfranck@1491: if (res != null) jfranck@1491: arr[0] = res; jfranck@1491: return arr; jfranck@1491: } jfranck@1491: jfranck@1491: // So we have a containing type jfranck@1491: String name = annoType.getName(); jfranck@1491: String annoTypeName = annoType.getSimpleName(); jfranck@1491: String containerTypeName = containerType.getSimpleName(); jfranck@1491: int directIndex = -1, containerIndex = -1; jfranck@1491: Attribute.Compound direct = null, container = null; jfranck@1491: Attribute.Compound[] rawAttributes = annotated.getRawAttributes().toArray(new Attribute.Compound[0]); jfranck@1491: jfranck@1491: // Find directly present annotations jfranck@1491: for (int i = 0; i < rawAttributes.length; i++) { jfranck@1491: if (annoTypeName.equals(rawAttributes[i].type.tsym.flatName().toString())) { jfranck@1491: directIndex = i; jfranck@1491: direct = rawAttributes[i]; jfranck@1491: } else if(containerTypeName != null && jfranck@1491: containerTypeName.equals(rawAttributes[i].type.tsym.flatName().toString())) { jfranck@1491: containerIndex = i; jfranck@1491: container = rawAttributes[i]; jfranck@1491: } jfranck@1491: } jfranck@1491: // Deal with inherited annotations jfranck@1491: if (annotated.kind == Kinds.TYP && jfranck@1491: (annotated instanceof ClassSymbol)) { jfranck@1491: ClassSymbol s = (ClassSymbol)annotated; jfranck@1491: if (direct == null && container == null) { jfranck@1491: direct = getAttributeOnClass(s, annoType); jfranck@1491: container = getAttributeOnClass(s, containerType); jfranck@1491: jfranck@1491: // both are inherited and found, put container last jfranck@1491: if (direct != null && container != null) { jfranck@1491: directIndex = 0; jfranck@1491: containerIndex = 1; jfranck@1491: } else if (direct != null) { jfranck@1491: directIndex = 0; jfranck@1491: } else { jfranck@1491: containerIndex = 0; jfranck@1491: } jfranck@1491: } else if (direct == null) { jfranck@1491: direct = getAttributeOnClass(s, annoType); jfranck@1491: if (direct != null) jfranck@1491: directIndex = containerIndex + 1; jfranck@1491: } else if (container == null) { jfranck@1491: container = getAttributeOnClass(s, containerType); jfranck@1491: if (container != null) jfranck@1491: containerIndex = directIndex + 1; jfranck@1491: } jfranck@1491: } jfranck@1491: jfranck@1491: // Pack them in an array jfranck@1491: Attribute[] contained0 = new Attribute[0]; jfranck@1491: if (container != null) jfranck@1491: contained0 = unpackAttributes(container); jfranck@1491: ListBuffer compounds = ListBuffer.lb(); jfranck@1491: for (Attribute a : contained0) jfranck@1491: if (a instanceof Attribute.Compound) jfranck@1491: compounds = compounds.append((Attribute.Compound)a); jfranck@1491: Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[0]); jfranck@1491: jfranck@1491: int size = (direct == null ? 0 : 1) + contained.length; jfranck@1491: @SuppressWarnings("unchecked") // annoType is the Class for A jfranck@1491: A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); jfranck@1491: jfranck@1491: // if direct && container, which is first? jfranck@1491: int insert = -1; jfranck@1491: int length = arr.length; jfranck@1491: if (directIndex >= 0 && containerIndex >= 0) { jfranck@1491: if (directIndex < containerIndex) { jfranck@1491: arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); jfranck@1491: insert = 1; jfranck@1491: } else { jfranck@1491: arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType); jfranck@1491: insert = 0; jfranck@1491: length--; jfranck@1491: } jfranck@1491: } else if (directIndex >= 0) { jfranck@1491: arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); jfranck@1491: return arr; jfranck@1491: } else { jfranck@1491: // Only container jfranck@1491: insert = 0; jfranck@1491: } jfranck@1491: jfranck@1491: for (int i = 0; i + insert < length; i++) jfranck@1491: arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType); jfranck@1491: jfranck@1491: return arr; jfranck@1491: } jfranck@1491: jfranck@1491: // Needed to unpack the runtime view of containing annotations jjg@1492: private static final Class REPEATABLE_CLASS = initRepeatable(); jfranck@1491: private static final Method VALUE_ELEMENT_METHOD = initValueElementMethod(); jfranck@1491: jjg@1492: private static Class initRepeatable() { jfranck@1491: try { jjg@1492: @SuppressWarnings("unchecked") // java.lang.annotation.Repeatable extends Annotation by being an annotation type jjg@1492: Class c = (Class)Class.forName("java.lang.annotation.Repeatable"); jfranck@1491: return c; jfranck@1491: } catch (ClassNotFoundException e) { jfranck@1491: return null; jfranck@1491: } catch (SecurityException e) { jfranck@1491: return null; jfranck@1491: } jfranck@1491: } jfranck@1491: private static Method initValueElementMethod() { jjg@1492: if (REPEATABLE_CLASS == null) jfranck@1491: return null; jfranck@1491: jfranck@1491: Method m = null; jfranck@1491: try { jjg@1492: m = REPEATABLE_CLASS.getMethod("value"); jfranck@1491: if (m != null) jfranck@1491: m.setAccessible(true); jfranck@1491: return m; jfranck@1491: } catch (NoSuchMethodException e) { jfranck@1491: return null; jfranck@1491: } jfranck@1491: } jfranck@1491: jfranck@1491: // Helper to getAnnotations jfranck@1491: private static Class getContainer(Class annoType) { jjg@1492: // Since we can not refer to java.lang.annotation.Repeatable until we are jjg@1492: // bootstrapping with java 8 we need to get the Repeatable annotation using jfranck@1491: // reflective invocations instead of just using its type and element method. jjg@1492: if (REPEATABLE_CLASS != null && jfranck@1491: VALUE_ELEMENT_METHOD != null) { jjg@1492: // Get the Repeatable instance on the annotations declaration jjg@1492: Annotation repeatable = (Annotation)annoType.getAnnotation(REPEATABLE_CLASS); jjg@1492: if (repeatable != null) { jfranck@1491: try { jfranck@1491: // Get the value element, it should be a class jfranck@1491: // indicating the containing annotation type jfranck@1491: @SuppressWarnings("unchecked") jjg@1492: Class containerType = (Class)VALUE_ELEMENT_METHOD.invoke(repeatable); jfranck@1491: if (containerType == null) jfranck@1491: return null; jfranck@1491: jfranck@1491: return containerType; jfranck@1491: } catch (ClassCastException e) { jfranck@1491: return null; jfranck@1491: } catch (IllegalAccessException e) { jfranck@1491: return null; jfranck@1491: } catch (InvocationTargetException e ) { jfranck@1491: return null; jfranck@1491: } jfranck@1491: } jfranck@1491: } jfranck@1491: return null; jfranck@1491: } jfranck@1491: // Helper to getAnnotations jfranck@1491: private static Attribute[] unpackAttributes(Attribute.Compound container) { jfranck@1491: // We now have an instance of the container, jfranck@1491: // unpack it returning an instance of the jfranck@1491: // contained type or null jfranck@1491: return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values; jfranck@1491: } duke@1: duke@1: public PackageSymbol getPackageElement(CharSequence name) { duke@1: String strName = name.toString(); duke@1: if (strName.equals("")) duke@1: return syms.unnamedPackage; duke@1: return SourceVersion.isName(strName) duke@1: ? nameToSymbol(strName, PackageSymbol.class) duke@1: : null; duke@1: } duke@1: duke@1: public ClassSymbol getTypeElement(CharSequence name) { duke@1: String strName = name.toString(); duke@1: return SourceVersion.isName(strName) duke@1: ? nameToSymbol(strName, ClassSymbol.class) duke@1: : null; duke@1: } duke@1: duke@1: /** duke@1: * Returns a symbol given the type's or packages's canonical name, duke@1: * or null if the name isn't found. duke@1: */ duke@1: private S nameToSymbol(String nameStr, Class clazz) { duke@1: Name name = names.fromString(nameStr); duke@1: // First check cache. duke@1: Symbol sym = (clazz == ClassSymbol.class) duke@1: ? syms.classes.get(name) duke@1: : syms.packages.get(name); duke@1: duke@1: try { duke@1: if (sym == null) duke@1: sym = javaCompiler.resolveIdent(nameStr); duke@1: duke@1: sym.complete(); duke@1: duke@1: return (sym.kind != Kinds.ERR && duke@1: sym.exists() && duke@1: clazz.isInstance(sym) && duke@1: name.equals(sym.getQualifiedName())) duke@1: ? clazz.cast(sym) duke@1: : null; duke@1: } catch (CompletionFailure e) { duke@1: return null; duke@1: } duke@1: } duke@1: duke@1: public JavacSourcePosition getSourcePosition(Element e) { duke@1: Pair treeTop = getTreeAndTopLevel(e); duke@1: if (treeTop == null) duke@1: return null; duke@1: JCTree tree = treeTop.fst; duke@1: JCCompilationUnit toplevel = treeTop.snd; duke@1: JavaFileObject sourcefile = toplevel.sourcefile; duke@1: if (sourcefile == null) duke@1: return null; duke@1: return new JavacSourcePosition(sourcefile, tree.pos, toplevel.lineMap); duke@1: } duke@1: duke@1: public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a) { duke@1: Pair treeTop = getTreeAndTopLevel(e); duke@1: if (treeTop == null) duke@1: return null; duke@1: JCTree tree = treeTop.fst; duke@1: JCCompilationUnit toplevel = treeTop.snd; duke@1: JavaFileObject sourcefile = toplevel.sourcefile; duke@1: if (sourcefile == null) duke@1: return null; duke@1: duke@1: JCTree annoTree = matchAnnoToTree(a, e, tree); duke@1: if (annoTree == null) duke@1: return null; duke@1: return new JavacSourcePosition(sourcefile, annoTree.pos, duke@1: toplevel.lineMap); duke@1: } duke@1: duke@1: public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a, duke@1: AnnotationValue v) { duke@1: // TODO: better accuracy in getSourcePosition(... AnnotationValue) duke@1: return getSourcePosition(e, a); duke@1: } duke@1: duke@1: /** duke@1: * Returns the tree for an annotation given the annotated element duke@1: * and the element's own tree. Returns null if the tree cannot be found. duke@1: */ duke@1: private JCTree matchAnnoToTree(AnnotationMirror findme, duke@1: Element e, JCTree tree) { duke@1: Symbol sym = cast(Symbol.class, e); duke@1: class Vis extends JCTree.Visitor { duke@1: List result = null; duke@1: public void visitTopLevel(JCCompilationUnit tree) { duke@1: result = tree.packageAnnotations; duke@1: } duke@1: public void visitClassDef(JCClassDecl tree) { duke@1: result = tree.mods.annotations; duke@1: } duke@1: public void visitMethodDef(JCMethodDecl tree) { duke@1: result = tree.mods.annotations; duke@1: } duke@1: public void visitVarDef(JCVariableDecl tree) { duke@1: result = tree.mods.annotations; duke@1: } duke@1: } duke@1: Vis vis = new Vis(); duke@1: tree.accept(vis); duke@1: if (vis.result == null) duke@1: return null; jfranck@1491: jfranck@1491: List annos = sym.getRawAttributes(); duke@1: return matchAnnoToTree(cast(Attribute.Compound.class, findme), jfranck@1491: annos, duke@1: vis.result); duke@1: } duke@1: duke@1: /** duke@1: * Returns the tree for an annotation given a list of annotations duke@1: * in which to search (recursively) and their corresponding trees. duke@1: * Returns null if the tree cannot be found. duke@1: */ duke@1: private JCTree matchAnnoToTree(Attribute.Compound findme, duke@1: List annos, duke@1: List trees) { duke@1: for (Attribute.Compound anno : annos) { duke@1: for (JCAnnotation tree : trees) { duke@1: JCTree match = matchAnnoToTree(findme, anno, tree); duke@1: if (match != null) duke@1: return match; duke@1: } duke@1: } duke@1: return null; duke@1: } duke@1: duke@1: /** duke@1: * Returns the tree for an annotation given an Attribute to duke@1: * search (recursively) and its corresponding tree. duke@1: * Returns null if the tree cannot be found. duke@1: */ duke@1: private JCTree matchAnnoToTree(final Attribute.Compound findme, duke@1: final Attribute attr, duke@1: final JCTree tree) { duke@1: if (attr == findme) duke@1: return (tree.type.tsym == findme.type.tsym) ? tree : null; duke@1: duke@1: class Vis implements Attribute.Visitor { duke@1: JCTree result = null; duke@1: public void visitConstant(Attribute.Constant value) { duke@1: } duke@1: public void visitClass(Attribute.Class clazz) { duke@1: } duke@1: public void visitCompound(Attribute.Compound anno) { duke@1: for (Pair pair : anno.values) { duke@1: JCExpression expr = scanForAssign(pair.fst, tree); duke@1: if (expr != null) { duke@1: JCTree match = matchAnnoToTree(findme, pair.snd, expr); duke@1: if (match != null) { duke@1: result = match; duke@1: return; duke@1: } duke@1: } duke@1: } duke@1: } duke@1: public void visitArray(Attribute.Array array) { jjg@1127: if (tree.hasTag(NEWARRAY) && duke@1: types.elemtype(array.type).tsym == findme.type.tsym) { duke@1: List elems = ((JCNewArray) tree).elems; duke@1: for (Attribute value : array.values) { duke@1: if (value == findme) { duke@1: result = elems.head; duke@1: return; duke@1: } duke@1: elems = elems.tail; duke@1: } duke@1: } duke@1: } duke@1: public void visitEnum(Attribute.Enum e) { duke@1: } duke@1: public void visitError(Attribute.Error e) { duke@1: } duke@1: } duke@1: Vis vis = new Vis(); duke@1: attr.accept(vis); duke@1: return vis.result; duke@1: } duke@1: duke@1: /** duke@1: * Scans for a JCAssign node with a LHS matching a given duke@1: * symbol, and returns its RHS. Does not scan nested JCAnnotations. duke@1: */ duke@1: private JCExpression scanForAssign(final MethodSymbol sym, duke@1: final JCTree tree) { duke@1: class TS extends TreeScanner { duke@1: JCExpression result = null; duke@1: public void scan(JCTree t) { duke@1: if (t != null && result == null) duke@1: t.accept(this); duke@1: } duke@1: public void visitAnnotation(JCAnnotation t) { duke@1: if (t == tree) duke@1: scan(t.args); duke@1: } duke@1: public void visitAssign(JCAssign t) { jjg@1127: if (t.lhs.hasTag(IDENT)) { duke@1: JCIdent ident = (JCIdent) t.lhs; duke@1: if (ident.sym == sym) duke@1: result = t.rhs; duke@1: } duke@1: } duke@1: } duke@1: TS scanner = new TS(); duke@1: tree.accept(scanner); duke@1: return scanner.result; duke@1: } duke@1: duke@1: /** duke@1: * Returns the tree node corresponding to this element, or null duke@1: * if none can be found. duke@1: */ duke@1: public JCTree getTree(Element e) { duke@1: Pair treeTop = getTreeAndTopLevel(e); duke@1: return (treeTop != null) ? treeTop.fst : null; duke@1: } duke@1: duke@1: public String getDocComment(Element e) { duke@1: // Our doc comment is contained in a map in our toplevel, duke@1: // indexed by our tree. Find our enter environment, which gives duke@1: // us our toplevel. It also gives us a tree that contains our duke@1: // tree: walk it to find our tree. This is painful. duke@1: Pair treeTop = getTreeAndTopLevel(e); duke@1: if (treeTop == null) duke@1: return null; duke@1: JCTree tree = treeTop.fst; duke@1: JCCompilationUnit toplevel = treeTop.snd; duke@1: if (toplevel.docComments == null) duke@1: return null; jjg@1280: return toplevel.docComments.getCommentText(tree); duke@1: } duke@1: duke@1: public PackageElement getPackageOf(Element e) { duke@1: return cast(Symbol.class, e).packge(); duke@1: } duke@1: duke@1: public boolean isDeprecated(Element e) { duke@1: Symbol sym = cast(Symbol.class, e); duke@1: return (sym.flags() & Flags.DEPRECATED) != 0; duke@1: } duke@1: duke@1: public Name getBinaryName(TypeElement type) { duke@1: return cast(TypeSymbol.class, type).flatName(); duke@1: } duke@1: duke@1: public Map getElementValuesWithDefaults( duke@1: AnnotationMirror a) { duke@1: Attribute.Compound anno = cast(Attribute.Compound.class, a); duke@1: DeclaredType annotype = a.getAnnotationType(); duke@1: Map valmap = anno.getElementValues(); duke@1: duke@1: for (ExecutableElement ex : duke@1: methodsIn(annotype.asElement().getEnclosedElements())) { duke@1: MethodSymbol meth = (MethodSymbol) ex; duke@1: Attribute defaultValue = meth.getDefaultValue(); duke@1: if (defaultValue != null && !valmap.containsKey(meth)) { duke@1: valmap.put(meth, defaultValue); duke@1: } duke@1: } duke@1: return valmap; duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public FilteredMemberList getAllMembers(TypeElement element) { duke@1: Symbol sym = cast(Symbol.class, element); duke@1: Scope scope = sym.members().dupUnshared(); duke@1: List closure = types.closure(sym.asType()); duke@1: for (Type t : closure) duke@1: addMembers(scope, t); duke@1: return new FilteredMemberList(scope); duke@1: } duke@1: // where duke@1: private void addMembers(Scope scope, Type type) { duke@1: members: duke@1: for (Scope.Entry e = type.asElement().members().elems; e != null; e = e.sibling) { duke@1: Scope.Entry overrider = scope.lookup(e.sym.getSimpleName()); duke@1: while (overrider.scope != null) { duke@1: if (overrider.sym.kind == e.sym.kind duke@1: && (overrider.sym.flags() & Flags.SYNTHETIC) == 0) duke@1: { duke@1: if (overrider.sym.getKind() == ElementKind.METHOD duke@1: && overrides((ExecutableElement)overrider.sym, (ExecutableElement)e.sym, (TypeElement)type.asElement())) { duke@1: continue members; duke@1: } duke@1: } duke@1: overrider = overrider.next(); duke@1: } duke@1: boolean derived = e.sym.getEnclosingElement() != scope.owner; duke@1: ElementKind kind = e.sym.getKind(); duke@1: boolean initializer = kind == ElementKind.CONSTRUCTOR duke@1: || kind == ElementKind.INSTANCE_INIT duke@1: || kind == ElementKind.STATIC_INIT; duke@1: if (!derived || (!initializer && e.sym.isInheritedIn(scope.owner, types))) duke@1: scope.enter(e.sym); duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Returns all annotations of an element, whether duke@1: * inherited or directly present. duke@1: * duke@1: * @param e the element being examined duke@1: * @return all annotations of the element duke@1: */ duke@1: public List getAllAnnotationMirrors(Element e) { duke@1: Symbol sym = cast(Symbol.class, e); jfranck@1491: List annos = sym.getRawAttributes(); duke@1: while (sym.getKind() == ElementKind.CLASS) { duke@1: Type sup = ((ClassSymbol) sym).getSuperclass(); jjg@1374: if (!sup.hasTag(CLASS) || sup.isErroneous() || duke@1: sup.tsym == syms.objectType.tsym) { duke@1: break; duke@1: } duke@1: sym = sup.tsym; duke@1: List oldAnnos = annos; jfranck@1491: List newAnnos = sym.getRawAttributes(); jfranck@1491: for (Attribute.Compound anno : newAnnos) { duke@1: if (isInherited(anno.type) && duke@1: !containsAnnoOfType(oldAnnos, anno.type)) { duke@1: annos = annos.prepend(anno); duke@1: } duke@1: } duke@1: } duke@1: return annos; duke@1: } duke@1: duke@1: /** duke@1: * Tests whether an annotation type is @Inherited. duke@1: */ duke@1: private boolean isInherited(Type annotype) { jfranck@1491: return annotype.tsym.attribute(syms.inheritedType.tsym) != null; duke@1: } duke@1: duke@1: /** duke@1: * Tests whether a list of annotations contains an annotation duke@1: * of a given type. duke@1: */ duke@1: private static boolean containsAnnoOfType(List annos, duke@1: Type type) { duke@1: for (Attribute.Compound anno : annos) { duke@1: if (anno.type.tsym == type.tsym) duke@1: return true; duke@1: } duke@1: return false; duke@1: } duke@1: duke@1: public boolean hides(Element hiderEl, Element hideeEl) { duke@1: Symbol hider = cast(Symbol.class, hiderEl); duke@1: Symbol hidee = cast(Symbol.class, hideeEl); duke@1: duke@1: // Fields only hide fields; methods only methods; types only types. duke@1: // Names must match. Nothing hides itself (just try it). duke@1: if (hider == hidee || duke@1: hider.kind != hidee.kind || duke@1: hider.name != hidee.name) { duke@1: return false; duke@1: } duke@1: duke@1: // Only static methods can hide other methods. duke@1: // Methods only hide methods with matching signatures. duke@1: if (hider.kind == Kinds.MTH) { duke@1: if (!hider.isStatic() || duke@1: !types.isSubSignature(hider.type, hidee.type)) { duke@1: return false; duke@1: } duke@1: } duke@1: duke@1: // Hider must be in a subclass of hidee's class. duke@1: // Note that if M1 hides M2, and M2 hides M3, and M3 is accessible duke@1: // in M1's class, then M1 and M2 both hide M3. duke@1: ClassSymbol hiderClass = hider.owner.enclClass(); duke@1: ClassSymbol hideeClass = hidee.owner.enclClass(); duke@1: if (hiderClass == null || hideeClass == null || duke@1: !hiderClass.isSubClass(hideeClass, types)) { duke@1: return false; duke@1: } duke@1: duke@1: // Hidee must be accessible in hider's class. duke@1: // The method isInheritedIn is poorly named: it checks only access. duke@1: return hidee.isInheritedIn(hiderClass, types); duke@1: } duke@1: duke@1: public boolean overrides(ExecutableElement riderEl, duke@1: ExecutableElement rideeEl, TypeElement typeEl) { duke@1: MethodSymbol rider = cast(MethodSymbol.class, riderEl); duke@1: MethodSymbol ridee = cast(MethodSymbol.class, rideeEl); duke@1: ClassSymbol origin = cast(ClassSymbol.class, typeEl); duke@1: duke@1: return rider.name == ridee.name && duke@1: duke@1: // not reflexive as per JLS duke@1: rider != ridee && duke@1: duke@1: // we don't care if ridee is static, though that wouldn't duke@1: // compile duke@1: !rider.isStatic() && duke@1: duke@1: // Symbol.overrides assumes the following duke@1: ridee.isMemberOf(origin, types) && duke@1: duke@1: // check access and signatures; don't check return types duke@1: rider.overrides(ridee, origin, types, false); duke@1: } duke@1: duke@1: public String getConstantExpression(Object value) { duke@1: return Constants.format(value); duke@1: } duke@1: duke@1: /** duke@1: * Print a representation of the elements to the given writer in duke@1: * the specified order. The main purpose of this method is for duke@1: * diagnostics. The exact format of the output is not duke@1: * specified and is subject to change. duke@1: * duke@1: * @param w the writer to print the output to duke@1: * @param elements the elements to print duke@1: */ duke@1: public void printElements(java.io.Writer w, Element... elements) { duke@1: for (Element element : elements) duke@1: (new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush(); duke@1: } duke@1: duke@1: public Name getName(CharSequence cs) { jjg@113: return names.fromString(cs.toString()); duke@1: } duke@1: duke@1: /** duke@1: * Returns the tree node and compilation unit corresponding to this duke@1: * element, or null if they can't be found. duke@1: */ duke@1: private Pair getTreeAndTopLevel(Element e) { duke@1: Symbol sym = cast(Symbol.class, e); duke@1: Env enterEnv = getEnterEnv(sym); duke@1: if (enterEnv == null) duke@1: return null; duke@1: JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree); duke@1: if (tree == null || enterEnv.toplevel == null) duke@1: return null; duke@1: return new Pair(tree, enterEnv.toplevel); duke@1: } duke@1: duke@1: /** duke@1: * Returns the best approximation for the tree node and compilation unit duke@1: * corresponding to the given element, annotation and value. duke@1: * If the element is null, null is returned. duke@1: * If the annotation is null or cannot be found, the tree node and duke@1: * compilation unit for the element is returned. duke@1: * If the annotation value is null or cannot be found, the tree node and duke@1: * compilation unit for the annotation is returned. duke@1: */ duke@1: public Pair getTreeAndTopLevel( duke@1: Element e, AnnotationMirror a, AnnotationValue v) { duke@1: if (e == null) duke@1: return null; duke@1: duke@1: Pair elemTreeTop = getTreeAndTopLevel(e); duke@1: if (elemTreeTop == null) duke@1: return null; duke@1: duke@1: if (a == null) duke@1: return elemTreeTop; duke@1: duke@1: JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst); duke@1: if (annoTree == null) duke@1: return elemTreeTop; duke@1: duke@1: // 6388543: if v != null, we should search within annoTree to find duke@1: // the tree matching v. For now, we ignore v and return the tree of duke@1: // the annotation. duke@1: return new Pair(annoTree, elemTreeTop.snd); duke@1: } duke@1: duke@1: /** duke@1: * Returns a symbol's enter environment, or null if it has none. duke@1: */ duke@1: private Env getEnterEnv(Symbol sym) { duke@1: // Get enclosing class of sym, or sym itself if it is a class duke@1: // or package. duke@1: TypeSymbol ts = (sym.kind != Kinds.PCK) duke@1: ? sym.enclClass() duke@1: : (PackageSymbol) sym; duke@1: return (ts != null) duke@1: ? enter.getEnv(ts) duke@1: : null; duke@1: } duke@1: duke@1: /** duke@1: * Returns an object cast to the specified type. duke@1: * @throws NullPointerException if the object is {@code null} duke@1: * @throws IllegalArgumentException if the object is of the wrong type duke@1: */ duke@1: private static T cast(Class clazz, Object o) { duke@1: if (! clazz.isInstance(o)) duke@1: throw new IllegalArgumentException(o.toString()); duke@1: return clazz.cast(o); duke@1: } duke@1: }