src/share/classes/com/sun/tools/javac/code/AnnoConstruct.java

Tue, 22 Oct 2013 03:36:44 +0200

author
jfranck
date
Tue, 22 Oct 2013 03:36:44 +0200
changeset 2154
ac839d6f4953
parent 2100
1e7ad879f15e
child 2156
f003f09144ff
permissions
-rw-r--r--

8026855: AnnoConstruct.getAnnotationsByType includes inherited indirectly present annotations even when containee type is not inheritable
Summary: In AnnoConstruct.getAnnotationByType() check that the annotation sought after is inherited before looking on supertypes.
Reviewed-by: jjg

alundblad@2100 1 /*
alundblad@2100 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
alundblad@2100 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
alundblad@2100 4 *
alundblad@2100 5 * This code is free software; you can redistribute it and/or modify it
alundblad@2100 6 * under the terms of the GNU General Public License version 2 only, as
alundblad@2100 7 * published by the Free Software Foundation. Oracle designates this
alundblad@2100 8 * particular file as subject to the "Classpath" exception as provided
alundblad@2100 9 * by Oracle in the LICENSE file that accompanied this code.
alundblad@2100 10 *
alundblad@2100 11 * This code is distributed in the hope that it will be useful, but WITHOUT
alundblad@2100 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
alundblad@2100 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
alundblad@2100 14 * version 2 for more details (a copy is included in the LICENSE file that
alundblad@2100 15 * accompanied this code).
alundblad@2100 16 *
alundblad@2100 17 * You should have received a copy of the GNU General Public License version
alundblad@2100 18 * 2 along with this work; if not, write to the Free Software Foundation,
alundblad@2100 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
alundblad@2100 20 *
alundblad@2100 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
alundblad@2100 22 * or visit www.oracle.com if you need additional information or have any
alundblad@2100 23 * questions.
alundblad@2100 24 */
alundblad@2100 25 package com.sun.tools.javac.code;
alundblad@2100 26
alundblad@2100 27 import java.lang.annotation.Annotation;
jfranck@2154 28 import java.lang.annotation.Inherited;
alundblad@2100 29 import java.lang.reflect.InvocationTargetException;
alundblad@2100 30 import java.lang.reflect.Method;
alundblad@2100 31
alundblad@2100 32 import javax.lang.model.AnnotatedConstruct;
alundblad@2100 33
alundblad@2100 34 import com.sun.tools.javac.model.AnnotationProxyMaker;
alundblad@2100 35 import com.sun.tools.javac.util.List;
alundblad@2100 36 import com.sun.tools.javac.util.ListBuffer;
alundblad@2100 37
alundblad@2100 38 /**
alundblad@2100 39 * Common super type for annotated constructs such as Types and Symbols.
alundblad@2100 40 *
alundblad@2100 41 * This class should *not* contain any fields since it would have a significant
alundblad@2100 42 * impact on the javac memory footprint.
alundblad@2100 43 *
alundblad@2100 44 * <p><b>This is NOT part of any supported API.
alundblad@2100 45 * If you write code that depends on this, you do so at your own
alundblad@2100 46 * risk. This code and its internal interfaces are subject to change
alundblad@2100 47 * or deletion without notice.</b></p>
alundblad@2100 48 */
alundblad@2100 49 public abstract class AnnoConstruct implements AnnotatedConstruct {
alundblad@2100 50
alundblad@2100 51
alundblad@2100 52 // Override to enforce a narrower return type.
alundblad@2100 53 @Override
alundblad@2100 54 public abstract List<? extends Attribute.Compound> getAnnotationMirrors();
alundblad@2100 55
alundblad@2100 56
alundblad@2100 57 // This method is part of the javax.lang.model API, do not use this in javac code.
alundblad@2100 58 protected <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
alundblad@2100 59 String name = annoType.getName();
alundblad@2100 60
alundblad@2100 61 for (Attribute.Compound anno : getAnnotationMirrors()) {
alundblad@2100 62 if (name.equals(anno.type.tsym.flatName().toString()))
alundblad@2100 63 return anno;
alundblad@2100 64 }
alundblad@2100 65
alundblad@2100 66 return null;
alundblad@2100 67 }
alundblad@2100 68
alundblad@2100 69
alundblad@2100 70 @SuppressWarnings("unchecked")
alundblad@2100 71 protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
alundblad@2100 72 return (A[]) java.lang.reflect.Array.newInstance(annoType, 0); // annoType is the Class for A
alundblad@2100 73 }
alundblad@2100 74
alundblad@2100 75
alundblad@2100 76 // This method is part of the javax.lang.model API, do not use this in javac code.
alundblad@2100 77 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annoType) {
alundblad@2100 78
alundblad@2100 79 if (!annoType.isAnnotation())
alundblad@2100 80 throw new IllegalArgumentException("Not an annotation type: "
alundblad@2100 81 + annoType);
alundblad@2100 82 // If annoType does not declare a container this is equivalent to wrapping
alundblad@2100 83 // getAnnotation(...) in an array.
alundblad@2100 84 Class <? extends Annotation> containerType = getContainer(annoType);
alundblad@2100 85 if (containerType == null) {
alundblad@2100 86 A res = getAnnotation(annoType);
alundblad@2100 87 int size = res == null ? 0 : 1;
alundblad@2100 88
alundblad@2100 89 @SuppressWarnings("unchecked") // annoType is the Class for A
alundblad@2100 90 A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
alundblad@2100 91 if (res != null)
alundblad@2100 92 arr[0] = res;
alundblad@2100 93 return arr;
alundblad@2100 94 }
alundblad@2100 95
alundblad@2100 96 // So we have a containing type
alundblad@2100 97 String annoTypeName = annoType.getName();
alundblad@2100 98 String containerTypeName = containerType.getName();
alundblad@2100 99 int directIndex = -1, containerIndex = -1;
alundblad@2100 100 Attribute.Compound direct = null, container = null;
alundblad@2100 101 // Find directly (explicit or implicit) present annotations
alundblad@2100 102 int index = -1;
alundblad@2100 103 for (Attribute.Compound attribute : getAnnotationMirrors()) {
alundblad@2100 104 index++;
alundblad@2100 105 if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) {
alundblad@2100 106 directIndex = index;
alundblad@2100 107 direct = attribute;
alundblad@2100 108 } else if(containerTypeName != null &&
alundblad@2100 109 attribute.type.tsym.flatName().contentEquals(containerTypeName)) {
alundblad@2100 110 containerIndex = index;
alundblad@2100 111 container = attribute;
alundblad@2100 112 }
alundblad@2100 113 }
alundblad@2100 114
alundblad@2100 115 // Deal with inherited annotations
jfranck@2154 116 if (direct == null && container == null &&
jfranck@2154 117 annoType.isAnnotationPresent(Inherited.class))
alundblad@2100 118 return getInheritedAnnotations(annoType);
alundblad@2100 119
alundblad@2100 120 // Pack them in an array
alundblad@2100 121 Attribute[] contained0 = null;
alundblad@2100 122 if (container != null)
alundblad@2100 123 contained0 = unpackAttributes(container);
alundblad@2100 124 ListBuffer<Attribute.Compound> compounds = new ListBuffer<>();
alundblad@2100 125 if (contained0 != null) {
alundblad@2100 126 for (Attribute a : contained0)
alundblad@2100 127 if (a instanceof Attribute.Compound)
alundblad@2100 128 compounds = compounds.append((Attribute.Compound)a);
alundblad@2100 129 }
alundblad@2100 130 Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[compounds.size()]);
alundblad@2100 131
alundblad@2100 132 int size = (direct == null ? 0 : 1) + contained.length;
alundblad@2100 133 @SuppressWarnings("unchecked") // annoType is the Class for A
alundblad@2100 134 A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
alundblad@2100 135
alundblad@2100 136 // if direct && container, which is first?
alundblad@2100 137 int insert = -1;
alundblad@2100 138 int length = arr.length;
alundblad@2100 139 if (directIndex >= 0 && containerIndex >= 0) {
alundblad@2100 140 if (directIndex < containerIndex) {
alundblad@2100 141 arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
alundblad@2100 142 insert = 1;
alundblad@2100 143 } else {
alundblad@2100 144 arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
alundblad@2100 145 insert = 0;
alundblad@2100 146 length--;
alundblad@2100 147 }
alundblad@2100 148 } else if (directIndex >= 0) {
alundblad@2100 149 arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
alundblad@2100 150 return arr;
alundblad@2100 151 } else {
alundblad@2100 152 // Only container
alundblad@2100 153 insert = 0;
alundblad@2100 154 }
alundblad@2100 155
alundblad@2100 156 for (int i = 0; i + insert < length; i++)
alundblad@2100 157 arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType);
alundblad@2100 158
alundblad@2100 159 return arr;
alundblad@2100 160 }
alundblad@2100 161
alundblad@2100 162
alundblad@2100 163 // This method is part of the javax.lang.model API, do not use this in javac code.
alundblad@2100 164 public <A extends Annotation> A getAnnotation(Class<A> annoType) {
alundblad@2100 165
alundblad@2100 166 if (!annoType.isAnnotation())
alundblad@2100 167 throw new IllegalArgumentException("Not an annotation type: " + annoType);
alundblad@2100 168
alundblad@2100 169 Attribute.Compound c = getAttribute(annoType);
alundblad@2100 170 return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType);
alundblad@2100 171 }
alundblad@2100 172
alundblad@2100 173 // Needed to unpack the runtime view of containing annotations
alundblad@2100 174 private static final Class<? extends Annotation> REPEATABLE_CLASS = initRepeatable();
alundblad@2100 175 private static final Method VALUE_ELEMENT_METHOD = initValueElementMethod();
alundblad@2100 176
alundblad@2100 177 private static Class<? extends Annotation> initRepeatable() {
alundblad@2100 178 try {
alundblad@2100 179 // Repeatable will not be available when bootstrapping on
alundblad@2100 180 // JDK 7 so use a reflective lookup instead of a class
alundblad@2100 181 // literal for Repeatable.class.
alundblad@2100 182 return Class.forName("java.lang.annotation.Repeatable").asSubclass(Annotation.class);
alundblad@2100 183 } catch (ClassNotFoundException | SecurityException e) {
alundblad@2100 184 return null;
alundblad@2100 185 }
alundblad@2100 186 }
alundblad@2100 187
alundblad@2100 188 private static Method initValueElementMethod() {
alundblad@2100 189 if (REPEATABLE_CLASS == null)
alundblad@2100 190 return null;
alundblad@2100 191
alundblad@2100 192 Method m = null;
alundblad@2100 193 try {
alundblad@2100 194 m = REPEATABLE_CLASS.getMethod("value");
alundblad@2100 195 if (m != null)
alundblad@2100 196 m.setAccessible(true);
alundblad@2100 197 return m;
alundblad@2100 198 } catch (NoSuchMethodException e) {
alundblad@2100 199 return null;
alundblad@2100 200 }
alundblad@2100 201 }
alundblad@2100 202
alundblad@2100 203
alundblad@2100 204 // Helper to getAnnotationsByType
alundblad@2100 205 private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) {
alundblad@2100 206 // Since we can not refer to java.lang.annotation.Repeatable until we are
alundblad@2100 207 // bootstrapping with java 8 we need to get the Repeatable annotation using
alundblad@2100 208 // reflective invocations instead of just using its type and element method.
alundblad@2100 209 if (REPEATABLE_CLASS != null &&
alundblad@2100 210 VALUE_ELEMENT_METHOD != null) {
alundblad@2100 211 // Get the Repeatable instance on the annotations declaration
alundblad@2100 212 Annotation repeatable = (Annotation)annoType.getAnnotation(REPEATABLE_CLASS);
alundblad@2100 213 if (repeatable != null) {
alundblad@2100 214 try {
alundblad@2100 215 // Get the value element, it should be a class
alundblad@2100 216 // indicating the containing annotation type
alundblad@2100 217 @SuppressWarnings("unchecked")
alundblad@2100 218 Class<? extends Annotation> containerType = (Class)VALUE_ELEMENT_METHOD.invoke(repeatable);
alundblad@2100 219 if (containerType == null)
alundblad@2100 220 return null;
alundblad@2100 221
alundblad@2100 222 return containerType;
alundblad@2100 223 } catch (ClassCastException | IllegalAccessException | InvocationTargetException e) {
alundblad@2100 224 return null;
alundblad@2100 225 }
alundblad@2100 226 }
alundblad@2100 227 }
alundblad@2100 228 return null;
alundblad@2100 229 }
alundblad@2100 230
alundblad@2100 231
alundblad@2100 232 // Helper to getAnnotationsByType
alundblad@2100 233 private static Attribute[] unpackAttributes(Attribute.Compound container) {
alundblad@2100 234 // We now have an instance of the container,
alundblad@2100 235 // unpack it returning an instance of the
alundblad@2100 236 // contained type or null
alundblad@2100 237 return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values;
alundblad@2100 238 }
alundblad@2100 239
alundblad@2100 240 }

mercurial