jjg@1645: /* jjg@1645: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. jjg@1645: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1645: * jjg@1645: * This code is free software; you can redistribute it and/or modify it jjg@1645: * under the terms of the GNU General Public License version 2 only, as jjg@1645: * published by the Free Software Foundation. Oracle designates this jjg@1645: * particular file as subject to the "Classpath" exception as provided jjg@1645: * by Oracle in the LICENSE file that accompanied this code. jjg@1645: * jjg@1645: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1645: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1645: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1645: * version 2 for more details (a copy is included in the LICENSE file that jjg@1645: * accompanied this code). jjg@1645: * jjg@1645: * You should have received a copy of the GNU General Public License version jjg@1645: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1645: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1645: * jjg@1645: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1645: * or visit www.oracle.com if you need additional information or have any jjg@1645: * questions. jjg@1645: */ jjg@1645: package com.sun.tools.javac.model; jjg@1645: jjg@1645: import java.lang.annotation.Annotation; jjg@1645: import java.lang.annotation.Inherited; jjg@1645: import java.lang.reflect.InvocationTargetException; jjg@1645: import java.lang.reflect.Method; jjg@1645: jjg@1645: import com.sun.tools.javac.code.Attribute; jjg@1645: import com.sun.tools.javac.code.Kinds; jjg@1645: import com.sun.tools.javac.code.Symbol; jjg@1645: import com.sun.tools.javac.code.Symbol.ClassSymbol; jfranck@1709: import com.sun.tools.javac.code.Symbol.TypeVariableSymbol; jfranck@1709: import com.sun.tools.javac.code.TargetType; jjg@1645: import com.sun.tools.javac.code.Type; jjg@1645: import com.sun.tools.javac.code.Type.AnnotatedType; jjg@1645: import com.sun.tools.javac.util.ListBuffer; jjg@1645: import static com.sun.tools.javac.code.TypeTag.CLASS; jfranck@1709: import com.sun.tools.javac.util.List; jjg@1645: jjg@1645: /** jjg@1645: * Utility methods for operating on annotated constructs. jjg@1645: * jjg@1645: *

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

jjg@1645: */ jjg@1645: public class JavacAnnoConstructs { jjg@1645: jjg@1645: // jjg@1645: jjg@1645: /** jjg@1645: * An internal-use utility that creates a runtime view of an jjg@1645: * annotation. This is the implementation of jjg@1645: * Element.getAnnotation(Class). jjg@1645: */ jjg@1645: public static A getAnnotation(Symbol annotated, jjg@1645: Class annoType) { jjg@1645: if (!annoType.isAnnotation()) jjg@1645: throw new IllegalArgumentException("Not an annotation type: " jjg@1645: + annoType); jjg@1645: Attribute.Compound c; jfranck@1709: if (annotated.kind == Kinds.TYP && jfranck@1709: annotated instanceof ClassSymbol) { jjg@1645: c = getAttributeOnClass((ClassSymbol)annotated, annoType); jfranck@1709: } else if (annotated.kind == Kinds.TYP && jfranck@1709: annotated instanceof TypeVariableSymbol) { jfranck@1709: c = getAttributeOnTypeVariable((TypeVariableSymbol)annotated, annoType); jjg@1645: } else { jjg@1645: c = getAttribute(annotated, annoType); jjg@1645: } jjg@1645: return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType); jjg@1645: } jjg@1645: jjg@1645: // Helper to getAnnotation[s] jjg@1645: private static Attribute.Compound getAttribute(Symbol annotated, jjg@1645: Class annoType) { jjg@1645: String name = annoType.getName(); jjg@1645: jjg@1645: for (Attribute.Compound anno : annotated.getRawAttributes()) { jjg@1645: if (name.equals(anno.type.tsym.flatName().toString())) jjg@1645: return anno; jjg@1645: } jjg@1645: jjg@1645: return null; jjg@1645: } jjg@1645: jjg@1645: // Helper to getAnnotation[s] jfranck@1709: private static Attribute.Compound jfranck@1709: getAttributeOnTypeVariable(TypeVariableSymbol annotated, Class annoType) { jfranck@1709: String name = annoType.getName(); jfranck@1709: jfranck@1709: // Declaration annotations on type variables are stored in type attributes jfranck@1709: // on the owner of the TypeVariableSymbol jfranck@1709: List res = List.nil(); jfranck@1709: List candidates = annotated.owner.getRawTypeAttributes(); jfranck@1709: for (Attribute.TypeCompound anno : candidates) jfranck@1709: if (anno.position.type == TargetType.CLASS_TYPE_PARAMETER || jfranck@1709: anno.position.type == TargetType.METHOD_TYPE_PARAMETER) jfranck@1709: if (name.equals(anno.type.tsym.flatName().toString())) jfranck@1709: return anno; jfranck@1709: jfranck@1709: return null; jfranck@1709: } jfranck@1709: jfranck@1709: // Helper to getAnnotation[s] jfranck@1918: private static Attribute.Compound getAttributeOnClass( jfranck@1918: ClassSymbol annotated, jfranck@1918: final Class annoType) jfranck@1918: { jjg@1645: boolean inherited = annoType.isAnnotationPresent(Inherited.class); jjg@1645: Attribute.Compound result = null; jfranck@1918: jfranck@1918: result = getAttribute(annotated, annoType); jfranck@1918: if (result != null || !inherited) jfranck@1918: return result; jfranck@1918: jfranck@1918: while ((annotated = nextSupertypeToSearch(annotated)) != null) { jjg@1645: result = getAttribute(annotated, annoType); jfranck@1918: if (result != null) jfranck@1918: return result; jjg@1645: } jfranck@1918: return null; // no more supertypes to search jfranck@1918: } jfranck@1918: jfranck@1918: /** jfranck@1918: * Returns the next type to search for inherited annotations or {@code null} jfranck@1918: * if the next type can't be found. jfranck@1918: */ jfranck@1918: private static ClassSymbol nextSupertypeToSearch(ClassSymbol annotated) { jfranck@1918: if (annotated.name == annotated.name.table.names.java_lang_Object) jfranck@1918: return null; jfranck@1918: jfranck@1918: Type sup = annotated.getSuperclass(); jfranck@1918: if (!sup.hasTag(CLASS) || sup.isErroneous()) jfranck@1918: return null; jfranck@1918: jfranck@1918: return (ClassSymbol) sup.tsym; jjg@1645: } jjg@1645: jjg@1645: /** jjg@1645: * An internal-use utility that creates a runtime view of jjg@1645: * annotations. This is the implementation of jjg@1645: * Element.getAnnotations(Class). jjg@1645: */ jfranck@1918: public static A[] getAnnotationsByType(Symbol annotated, jfranck@1918: Class annoType) jfranck@1918: { jjg@1645: if (!annoType.isAnnotation()) jjg@1645: throw new IllegalArgumentException("Not an annotation type: " jjg@1645: + annoType); jjg@1645: // If annoType does not declare a container this is equivalent to wrapping jjg@1645: // getAnnotation(...) in an array. jjg@1645: Class containerType = getContainer(annoType); jjg@1645: if (containerType == null) { jjg@1645: A res = getAnnotation(annotated, annoType); jjg@1645: int size; jjg@1645: if (res == null) { jjg@1645: size = 0; jjg@1645: } else { jjg@1645: size = 1; jjg@1645: } jjg@1645: @SuppressWarnings("unchecked") // annoType is the Class for A jjg@1645: A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); jjg@1645: if (res != null) jjg@1645: arr[0] = res; jjg@1645: return arr; jjg@1645: } jjg@1645: jjg@1645: // So we have a containing type jjg@1645: String annoTypeName = annoType.getSimpleName(); jjg@1645: String containerTypeName = containerType.getSimpleName(); jjg@1645: int directIndex = -1, containerIndex = -1; jjg@1645: Attribute.Compound direct = null, container = null; jfranck@1918: // Find directly (explicit or implicit) present annotations jfranck@1918: int index = -1; jfranck@1918: for (List list = annotated.getAnnotationMirrors(); jfranck@1918: !list.isEmpty(); jfranck@1918: list = list.tail) { jfranck@1918: Attribute.Compound attribute = list.head; jfranck@1918: index++; jfranck@1918: if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) { jfranck@1918: directIndex = index; jfranck@1918: direct = attribute; jjg@1645: } else if(containerTypeName != null && jfranck@1918: attribute.type.tsym.flatName().contentEquals(containerTypeName)) { jfranck@1918: containerIndex = index; jfranck@1918: container = attribute; jjg@1645: } jjg@1645: } jjg@1645: jjg@1645: // Deal with inherited annotations jfranck@1918: if (direct == null && container == null) { jfranck@1918: if (annotated.kind == Kinds.TYP && jfranck@1918: (annotated instanceof ClassSymbol)) { jfranck@1918: ClassSymbol s = nextSupertypeToSearch((ClassSymbol)annotated); jfranck@1918: if (s != null) jfranck@1918: return getAnnotationsByType(s, annoType); jjg@1645: } jjg@1645: } jjg@1645: jjg@1645: // Pack them in an array jfranck@1918: Attribute[] contained0 = null; jjg@1645: if (container != null) jjg@1645: contained0 = unpackAttributes(container); jjg@1645: ListBuffer compounds = ListBuffer.lb(); jfranck@1918: if (contained0 != null) { jfranck@1918: for (Attribute a : contained0) jfranck@1918: if (a instanceof Attribute.Compound) jfranck@1918: compounds = compounds.append((Attribute.Compound)a); jfranck@1918: } jfranck@1918: Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[compounds.size()]); jjg@1645: jjg@1645: int size = (direct == null ? 0 : 1) + contained.length; jjg@1645: @SuppressWarnings("unchecked") // annoType is the Class for A jjg@1645: A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); jjg@1645: jjg@1645: // if direct && container, which is first? jjg@1645: int insert = -1; jjg@1645: int length = arr.length; jjg@1645: if (directIndex >= 0 && containerIndex >= 0) { jjg@1645: if (directIndex < containerIndex) { jjg@1645: arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); jjg@1645: insert = 1; jjg@1645: } else { jjg@1645: arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType); jjg@1645: insert = 0; jjg@1645: length--; jjg@1645: } jjg@1645: } else if (directIndex >= 0) { jjg@1645: arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); jjg@1645: return arr; jjg@1645: } else { jjg@1645: // Only container jjg@1645: insert = 0; jjg@1645: } jjg@1645: jjg@1645: for (int i = 0; i + insert < length; i++) jjg@1645: arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType); jjg@1645: jjg@1645: return arr; jjg@1645: } jjg@1645: jjg@1645: // jjg@1645: jjg@1645: // jjg@1645: jjg@1645: /** jjg@1645: * An internal-use utility that creates a runtime view of an jjg@1645: * annotation. This is the implementation of jjg@1645: * TypeMirror.getAnnotation(Class). jjg@1645: */ jjg@1645: public static A getAnnotation(AnnotatedType annotated, Class annoType) { jjg@1645: if (!annoType.isAnnotation()) jjg@1645: throw new IllegalArgumentException("Not an annotation type: " jjg@1645: + annoType); jjg@1645: Attribute.Compound c = getAttribute(annotated, annoType); jjg@1645: return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType); jjg@1645: } jjg@1645: jjg@1645: // Helper to getAnnotation[s] jjg@1645: private static Attribute.Compound getAttribute(Type annotated, jjg@1645: Class annoType) { jjg@1645: String name = annoType.getName(); jjg@1645: jjg@1645: for (Attribute.Compound anno : annotated.getAnnotationMirrors()) { jjg@1645: if (name.equals(anno.type.tsym.flatName().toString())) jjg@1645: return anno; jjg@1645: } jjg@1645: jjg@1645: return null; jjg@1645: } jjg@1645: jjg@1645: /** jjg@1645: * An internal-use utility that creates a runtime view of jjg@1645: * annotations. This is the implementation of jjg@1645: * TypeMirror.getAnnotationsByType(Class). jjg@1645: */ jjg@1645: public static A[] getAnnotationsByType(AnnotatedType annotated, Class annoType) { jjg@1645: if (!annoType.isAnnotation()) jjg@1645: throw new IllegalArgumentException("Not an annotation type: " jjg@1645: + annoType); jjg@1645: // If annoType does not declare a container this is equivalent to wrapping jjg@1645: // getAnnotation(...) in an array. jjg@1645: Class containerType = getContainer(annoType); jjg@1645: if (containerType == null) { jjg@1645: A res = getAnnotation(annotated, annoType); jjg@1645: int size; jjg@1645: if (res == null) { jjg@1645: size = 0; jjg@1645: } else { jjg@1645: size = 1; jjg@1645: } jjg@1645: @SuppressWarnings("unchecked") // annoType is the Class for A jjg@1645: A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); jjg@1645: if (res != null) jjg@1645: arr[0] = res; jjg@1645: return arr; jjg@1645: } jjg@1645: jjg@1645: // So we have a containing type jjg@1645: String annoTypeName = annoType.getSimpleName(); jjg@1645: String containerTypeName = containerType.getSimpleName(); jjg@1645: int directIndex = -1, containerIndex = -1; jjg@1645: Attribute.Compound direct = null, container = null; jfranck@1918: // Find directly (explicit or implicit) present annotations jfranck@1918: int index = -1; jfranck@1918: for (List list = annotated.getAnnotationMirrors(); jfranck@1918: !list.isEmpty(); jfranck@1918: list = list.tail) { jfranck@1918: Attribute.Compound attribute = list.head; jfranck@1918: index++; jfranck@1918: if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) { jfranck@1918: directIndex = index; jfranck@1918: direct = attribute; jjg@1645: } else if(containerTypeName != null && jfranck@1918: attribute.type.tsym.flatName().contentEquals(containerTypeName)) { jfranck@1918: containerIndex = index; jfranck@1918: container = attribute; jjg@1645: } jjg@1645: } jjg@1645: jjg@1645: // Pack them in an array jfranck@1918: Attribute[] contained0 = null; jjg@1645: if (container != null) jjg@1645: contained0 = unpackAttributes(container); jjg@1645: ListBuffer compounds = ListBuffer.lb(); jfranck@1918: if (contained0 != null) { jfranck@1918: for (Attribute a : contained0) jfranck@1918: if (a instanceof Attribute.Compound) jfranck@1918: compounds = compounds.append((Attribute.Compound)a); jjg@1645: } jfranck@1918: Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[compounds.size()]); jjg@1645: jjg@1645: int size = (direct == null ? 0 : 1) + contained.length; jjg@1645: @SuppressWarnings("unchecked") // annoType is the Class for A jjg@1645: A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); jjg@1645: jjg@1645: // if direct && container, which is first? jjg@1645: int insert = -1; jjg@1645: int length = arr.length; jjg@1645: if (directIndex >= 0 && containerIndex >= 0) { jjg@1645: if (directIndex < containerIndex) { jjg@1645: arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); jjg@1645: insert = 1; jjg@1645: } else { jjg@1645: arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType); jjg@1645: insert = 0; jjg@1645: length--; jjg@1645: } jjg@1645: } else if (directIndex >= 0) { jjg@1645: arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); jjg@1645: return arr; jjg@1645: } else { jjg@1645: // Only container jjg@1645: insert = 0; jjg@1645: } jjg@1645: jjg@1645: for (int i = 0; i + insert < length; i++) jjg@1645: arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType); jjg@1645: jjg@1645: return arr; jjg@1645: } jjg@1645: jjg@1645: // jjg@1645: jjg@1645: // jjg@1645: jjg@1645: // Needed to unpack the runtime view of containing annotations jjg@1645: private static final Class REPEATABLE_CLASS = initRepeatable(); jjg@1645: private static final Method VALUE_ELEMENT_METHOD = initValueElementMethod(); jjg@1645: jjg@1645: private static Class initRepeatable() { jjg@1645: try { jjg@1645: // Repeatable will not be available when bootstrapping on jjg@1645: // JDK 7 so use a reflective lookup instead of a class jjg@1645: // literal for Repeatable.class. jjg@1645: return Class.forName("java.lang.annotation.Repeatable").asSubclass(Annotation.class); jjg@1645: } catch (ClassNotFoundException e) { jjg@1645: return null; jjg@1645: } catch (SecurityException e) { jjg@1645: return null; jjg@1645: } jjg@1645: } jjg@1645: jjg@1645: private static Method initValueElementMethod() { jjg@1645: if (REPEATABLE_CLASS == null) jjg@1645: return null; jjg@1645: jjg@1645: Method m = null; jjg@1645: try { jjg@1645: m = REPEATABLE_CLASS.getMethod("value"); jjg@1645: if (m != null) jjg@1645: m.setAccessible(true); jjg@1645: return m; jjg@1645: } catch (NoSuchMethodException e) { jjg@1645: return null; jjg@1645: } jjg@1645: } jjg@1645: jjg@1645: // Helper to getAnnotations jjg@1645: private static Class getContainer(Class annoType) { jjg@1645: // Since we can not refer to java.lang.annotation.Repeatable until we are jjg@1645: // bootstrapping with java 8 we need to get the Repeatable annotation using jjg@1645: // reflective invocations instead of just using its type and element method. jjg@1645: if (REPEATABLE_CLASS != null && jjg@1645: VALUE_ELEMENT_METHOD != null) { jjg@1645: // Get the Repeatable instance on the annotations declaration jjg@1645: Annotation repeatable = (Annotation)annoType.getAnnotation(REPEATABLE_CLASS); jjg@1645: if (repeatable != null) { jjg@1645: try { jjg@1645: // Get the value element, it should be a class jjg@1645: // indicating the containing annotation type jjg@1645: @SuppressWarnings("unchecked") jjg@1645: Class containerType = (Class)VALUE_ELEMENT_METHOD.invoke(repeatable); jjg@1645: if (containerType == null) jjg@1645: return null; jjg@1645: jjg@1645: return containerType; jjg@1645: } catch (ClassCastException e) { jjg@1645: return null; jjg@1645: } catch (IllegalAccessException e) { jjg@1645: return null; jjg@1645: } catch (InvocationTargetException e ) { jjg@1645: return null; jjg@1645: } jjg@1645: } jjg@1645: } jjg@1645: return null; jjg@1645: } jjg@1645: jjg@1645: // Helper to getAnnotations jjg@1645: private static Attribute[] unpackAttributes(Attribute.Compound container) { jjg@1645: // We now have an instance of the container, jjg@1645: // unpack it returning an instance of the jjg@1645: // contained type or null jjg@1645: return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values; jjg@1645: } jjg@1645: jjg@1645: // jjg@1645: }