src/share/classes/com/sun/tools/javac/model/JavacAnnoConstructs.java

Mon, 22 Apr 2013 10:24:19 +0200

author
jfranck
date
Mon, 22 Apr 2013 10:24:19 +0200
changeset 1709
bae8387d16aa
parent 1645
97f6839673d6
child 1918
a218f7befd55
permissions
-rw-r--r--

8011027: Type parameter annotations not passed through to javax.lang.model
Reviewed-by: jjg, darcy

jjg@1645 1 /*
jjg@1645 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1645 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1645 4 *
jjg@1645 5 * This code is free software; you can redistribute it and/or modify it
jjg@1645 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1645 7 * published by the Free Software Foundation. Oracle designates this
jjg@1645 8 * particular file as subject to the "Classpath" exception as provided
jjg@1645 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1645 10 *
jjg@1645 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1645 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1645 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1645 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1645 15 * accompanied this code).
jjg@1645 16 *
jjg@1645 17 * You should have received a copy of the GNU General Public License version
jjg@1645 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1645 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1645 20 *
jjg@1645 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1645 22 * or visit www.oracle.com if you need additional information or have any
jjg@1645 23 * questions.
jjg@1645 24 */
jjg@1645 25 package com.sun.tools.javac.model;
jjg@1645 26
jjg@1645 27 import java.lang.annotation.Annotation;
jjg@1645 28 import java.lang.annotation.Inherited;
jjg@1645 29 import java.lang.reflect.InvocationTargetException;
jjg@1645 30 import java.lang.reflect.Method;
jjg@1645 31
jjg@1645 32 import com.sun.tools.javac.code.Attribute;
jjg@1645 33 import com.sun.tools.javac.code.Kinds;
jjg@1645 34 import com.sun.tools.javac.code.Symbol;
jjg@1645 35 import com.sun.tools.javac.code.Symbol.ClassSymbol;
jfranck@1709 36 import com.sun.tools.javac.code.Symbol.TypeVariableSymbol;
jfranck@1709 37 import com.sun.tools.javac.code.TargetType;
jjg@1645 38 import com.sun.tools.javac.code.Type;
jjg@1645 39 import com.sun.tools.javac.code.Type.AnnotatedType;
jjg@1645 40 import com.sun.tools.javac.util.ListBuffer;
jjg@1645 41 import static com.sun.tools.javac.code.TypeTag.CLASS;
jfranck@1709 42 import com.sun.tools.javac.util.List;
jjg@1645 43
jjg@1645 44 /**
jjg@1645 45 * Utility methods for operating on annotated constructs.
jjg@1645 46 *
jjg@1645 47 * <p><b>This is NOT part of any supported API.
jjg@1645 48 * If you write code that depends on this, you do so at your own
jjg@1645 49 * risk. This code and its internal interfaces are subject to change
jjg@1645 50 * or deletion without notice.</b></p>
jjg@1645 51 */
jjg@1645 52 public class JavacAnnoConstructs {
jjg@1645 53
jjg@1645 54 // <editor-fold defaultstate="collapsed" desc="Symbols">
jjg@1645 55
jjg@1645 56 /**
jjg@1645 57 * An internal-use utility that creates a runtime view of an
jjg@1645 58 * annotation. This is the implementation of
jjg@1645 59 * Element.getAnnotation(Class).
jjg@1645 60 */
jjg@1645 61 public static <A extends Annotation> A getAnnotation(Symbol annotated,
jjg@1645 62 Class<A> annoType) {
jjg@1645 63 if (!annoType.isAnnotation())
jjg@1645 64 throw new IllegalArgumentException("Not an annotation type: "
jjg@1645 65 + annoType);
jjg@1645 66 Attribute.Compound c;
jfranck@1709 67 if (annotated.kind == Kinds.TYP &&
jfranck@1709 68 annotated instanceof ClassSymbol) {
jjg@1645 69 c = getAttributeOnClass((ClassSymbol)annotated, annoType);
jfranck@1709 70 } else if (annotated.kind == Kinds.TYP &&
jfranck@1709 71 annotated instanceof TypeVariableSymbol) {
jfranck@1709 72 c = getAttributeOnTypeVariable((TypeVariableSymbol)annotated, annoType);
jjg@1645 73 } else {
jjg@1645 74 c = getAttribute(annotated, annoType);
jjg@1645 75 }
jjg@1645 76 return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType);
jjg@1645 77 }
jjg@1645 78
jjg@1645 79 // Helper to getAnnotation[s]
jjg@1645 80 private static <A extends Annotation> Attribute.Compound getAttribute(Symbol annotated,
jjg@1645 81 Class<A> annoType) {
jjg@1645 82 String name = annoType.getName();
jjg@1645 83
jjg@1645 84 for (Attribute.Compound anno : annotated.getRawAttributes()) {
jjg@1645 85 if (name.equals(anno.type.tsym.flatName().toString()))
jjg@1645 86 return anno;
jjg@1645 87 }
jjg@1645 88
jjg@1645 89 return null;
jjg@1645 90 }
jjg@1645 91
jjg@1645 92 // Helper to getAnnotation[s]
jfranck@1709 93 private static <A extends Annotation> Attribute.Compound
jfranck@1709 94 getAttributeOnTypeVariable(TypeVariableSymbol annotated, Class<A> annoType) {
jfranck@1709 95 String name = annoType.getName();
jfranck@1709 96
jfranck@1709 97 // Declaration annotations on type variables are stored in type attributes
jfranck@1709 98 // on the owner of the TypeVariableSymbol
jfranck@1709 99 List<Attribute.Compound> res = List.nil();
jfranck@1709 100 List<Attribute.TypeCompound> candidates = annotated.owner.getRawTypeAttributes();
jfranck@1709 101 for (Attribute.TypeCompound anno : candidates)
jfranck@1709 102 if (anno.position.type == TargetType.CLASS_TYPE_PARAMETER ||
jfranck@1709 103 anno.position.type == TargetType.METHOD_TYPE_PARAMETER)
jfranck@1709 104 if (name.equals(anno.type.tsym.flatName().toString()))
jfranck@1709 105 return anno;
jfranck@1709 106
jfranck@1709 107 return null;
jfranck@1709 108 }
jfranck@1709 109
jfranck@1709 110 // Helper to getAnnotation[s]
jjg@1645 111 private static <A extends Annotation> Attribute.Compound getAttributeOnClass(ClassSymbol annotated,
jjg@1645 112 Class<A> annoType) {
jjg@1645 113 boolean inherited = annoType.isAnnotationPresent(Inherited.class);
jjg@1645 114 Attribute.Compound result = null;
jjg@1645 115 while (annotated.name != annotated.name.table.names.java_lang_Object) {
jjg@1645 116 result = getAttribute(annotated, annoType);
jjg@1645 117 if (result != null || !inherited)
jjg@1645 118 break;
jjg@1645 119 Type sup = annotated.getSuperclass();
jjg@1645 120 if (!sup.hasTag(CLASS) || sup.isErroneous())
jjg@1645 121 break;
jjg@1645 122 annotated = (ClassSymbol) sup.tsym;
jjg@1645 123 }
jjg@1645 124 return result;
jjg@1645 125 }
jjg@1645 126
jjg@1645 127 /**
jjg@1645 128 * An internal-use utility that creates a runtime view of
jjg@1645 129 * annotations. This is the implementation of
jjg@1645 130 * Element.getAnnotations(Class).
jjg@1645 131 */
jjg@1645 132 public static <A extends Annotation> A[] getAnnotations(Symbol annotated,
jjg@1645 133 Class<A> annoType) {
jjg@1645 134 if (!annoType.isAnnotation())
jjg@1645 135 throw new IllegalArgumentException("Not an annotation type: "
jjg@1645 136 + annoType);
jjg@1645 137 // If annoType does not declare a container this is equivalent to wrapping
jjg@1645 138 // getAnnotation(...) in an array.
jjg@1645 139 Class <? extends Annotation> containerType = getContainer(annoType);
jjg@1645 140 if (containerType == null) {
jjg@1645 141 A res = getAnnotation(annotated, annoType);
jjg@1645 142 int size;
jjg@1645 143 if (res == null) {
jjg@1645 144 size = 0;
jjg@1645 145 } else {
jjg@1645 146 size = 1;
jjg@1645 147 }
jjg@1645 148 @SuppressWarnings("unchecked") // annoType is the Class for A
jjg@1645 149 A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
jjg@1645 150 if (res != null)
jjg@1645 151 arr[0] = res;
jjg@1645 152 return arr;
jjg@1645 153 }
jjg@1645 154
jjg@1645 155 // So we have a containing type
jjg@1645 156 String name = annoType.getName();
jjg@1645 157 String annoTypeName = annoType.getSimpleName();
jjg@1645 158 String containerTypeName = containerType.getSimpleName();
jjg@1645 159 int directIndex = -1, containerIndex = -1;
jjg@1645 160 Attribute.Compound direct = null, container = null;
jjg@1645 161 Attribute.Compound[] rawAttributes = annotated.getRawAttributes().toArray(new Attribute.Compound[0]);
jjg@1645 162
jjg@1645 163 // Find directly present annotations
jjg@1645 164 for (int i = 0; i < rawAttributes.length; i++) {
jjg@1645 165 if (annoTypeName.equals(rawAttributes[i].type.tsym.flatName().toString())) {
jjg@1645 166 directIndex = i;
jjg@1645 167 direct = rawAttributes[i];
jjg@1645 168 } else if(containerTypeName != null &&
jjg@1645 169 containerTypeName.equals(rawAttributes[i].type.tsym.flatName().toString())) {
jjg@1645 170 containerIndex = i;
jjg@1645 171 container = rawAttributes[i];
jjg@1645 172 }
jjg@1645 173 }
jjg@1645 174
jjg@1645 175 // Deal with inherited annotations
jjg@1645 176 if (annotated.kind == Kinds.TYP &&
jjg@1645 177 (annotated instanceof ClassSymbol)) {
jjg@1645 178 ClassSymbol s = (ClassSymbol)annotated;
jjg@1645 179 if (direct == null && container == null) {
jjg@1645 180 direct = getAttributeOnClass(s, annoType);
jjg@1645 181 container = getAttributeOnClass(s, containerType);
jjg@1645 182
jjg@1645 183 // both are inherited and found, put container last
jjg@1645 184 if (direct != null && container != null) {
jjg@1645 185 directIndex = 0;
jjg@1645 186 containerIndex = 1;
jjg@1645 187 } else if (direct != null) {
jjg@1645 188 directIndex = 0;
jjg@1645 189 } else {
jjg@1645 190 containerIndex = 0;
jjg@1645 191 }
jjg@1645 192 } else if (direct == null) {
jjg@1645 193 direct = getAttributeOnClass(s, annoType);
jjg@1645 194 if (direct != null)
jjg@1645 195 directIndex = containerIndex + 1;
jjg@1645 196 } else if (container == null) {
jjg@1645 197 container = getAttributeOnClass(s, containerType);
jjg@1645 198 if (container != null)
jjg@1645 199 containerIndex = directIndex + 1;
jjg@1645 200 }
jjg@1645 201 }
jjg@1645 202
jjg@1645 203 // Pack them in an array
jjg@1645 204 Attribute[] contained0 = new Attribute[0];
jjg@1645 205 if (container != null)
jjg@1645 206 contained0 = unpackAttributes(container);
jjg@1645 207 ListBuffer<Attribute.Compound> compounds = ListBuffer.lb();
jjg@1645 208 for (Attribute a : contained0)
jjg@1645 209 if (a instanceof Attribute.Compound)
jjg@1645 210 compounds = compounds.append((Attribute.Compound)a);
jjg@1645 211 Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[0]);
jjg@1645 212
jjg@1645 213 int size = (direct == null ? 0 : 1) + contained.length;
jjg@1645 214 @SuppressWarnings("unchecked") // annoType is the Class for A
jjg@1645 215 A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
jjg@1645 216
jjg@1645 217 // if direct && container, which is first?
jjg@1645 218 int insert = -1;
jjg@1645 219 int length = arr.length;
jjg@1645 220 if (directIndex >= 0 && containerIndex >= 0) {
jjg@1645 221 if (directIndex < containerIndex) {
jjg@1645 222 arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
jjg@1645 223 insert = 1;
jjg@1645 224 } else {
jjg@1645 225 arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
jjg@1645 226 insert = 0;
jjg@1645 227 length--;
jjg@1645 228 }
jjg@1645 229 } else if (directIndex >= 0) {
jjg@1645 230 arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
jjg@1645 231 return arr;
jjg@1645 232 } else {
jjg@1645 233 // Only container
jjg@1645 234 insert = 0;
jjg@1645 235 }
jjg@1645 236
jjg@1645 237 for (int i = 0; i + insert < length; i++)
jjg@1645 238 arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType);
jjg@1645 239
jjg@1645 240 return arr;
jjg@1645 241 }
jjg@1645 242
jjg@1645 243 // </editor-fold>
jjg@1645 244
jjg@1645 245 // <editor-fold defaultstate="collapsed" desc="Types">
jjg@1645 246
jjg@1645 247 /**
jjg@1645 248 * An internal-use utility that creates a runtime view of an
jjg@1645 249 * annotation. This is the implementation of
jjg@1645 250 * TypeMirror.getAnnotation(Class).
jjg@1645 251 */
jjg@1645 252 public static <A extends Annotation> A getAnnotation(AnnotatedType annotated, Class<A> annoType) {
jjg@1645 253 if (!annoType.isAnnotation())
jjg@1645 254 throw new IllegalArgumentException("Not an annotation type: "
jjg@1645 255 + annoType);
jjg@1645 256 Attribute.Compound c = getAttribute(annotated, annoType);
jjg@1645 257 return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType);
jjg@1645 258 }
jjg@1645 259
jjg@1645 260 // Helper to getAnnotation[s]
jjg@1645 261 private static <A extends Annotation> Attribute.Compound getAttribute(Type annotated,
jjg@1645 262 Class<A> annoType) {
jjg@1645 263 String name = annoType.getName();
jjg@1645 264
jjg@1645 265 for (Attribute.Compound anno : annotated.getAnnotationMirrors()) {
jjg@1645 266 if (name.equals(anno.type.tsym.flatName().toString()))
jjg@1645 267 return anno;
jjg@1645 268 }
jjg@1645 269
jjg@1645 270 return null;
jjg@1645 271 }
jjg@1645 272
jjg@1645 273 /**
jjg@1645 274 * An internal-use utility that creates a runtime view of
jjg@1645 275 * annotations. This is the implementation of
jjg@1645 276 * TypeMirror.getAnnotationsByType(Class).
jjg@1645 277 */
jjg@1645 278 public static <A extends Annotation> A[] getAnnotationsByType(AnnotatedType annotated, Class<A> annoType) {
jjg@1645 279 if (!annoType.isAnnotation())
jjg@1645 280 throw new IllegalArgumentException("Not an annotation type: "
jjg@1645 281 + annoType);
jjg@1645 282 // If annoType does not declare a container this is equivalent to wrapping
jjg@1645 283 // getAnnotation(...) in an array.
jjg@1645 284 Class <? extends Annotation> containerType = getContainer(annoType);
jjg@1645 285 if (containerType == null) {
jjg@1645 286 A res = getAnnotation(annotated, annoType);
jjg@1645 287 int size;
jjg@1645 288 if (res == null) {
jjg@1645 289 size = 0;
jjg@1645 290 } else {
jjg@1645 291 size = 1;
jjg@1645 292 }
jjg@1645 293 @SuppressWarnings("unchecked") // annoType is the Class for A
jjg@1645 294 A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
jjg@1645 295 if (res != null)
jjg@1645 296 arr[0] = res;
jjg@1645 297 return arr;
jjg@1645 298 }
jjg@1645 299
jjg@1645 300 // So we have a containing type
jjg@1645 301 String name = annoType.getName();
jjg@1645 302 String annoTypeName = annoType.getSimpleName();
jjg@1645 303 String containerTypeName = containerType.getSimpleName();
jjg@1645 304 int directIndex = -1, containerIndex = -1;
jjg@1645 305 Attribute.Compound direct = null, container = null;
jjg@1645 306 Attribute.Compound[] rawAttributes = annotated.getAnnotationMirrors().toArray(new Attribute.Compound[0]);
jjg@1645 307
jjg@1645 308 // Find directly present annotations
jjg@1645 309 for (int i = 0; i < rawAttributes.length; i++) {
jjg@1645 310 if (annoTypeName.equals(rawAttributes[i].type.tsym.flatName().toString())) {
jjg@1645 311 directIndex = i;
jjg@1645 312 direct = rawAttributes[i];
jjg@1645 313 } else if(containerTypeName != null &&
jjg@1645 314 containerTypeName.equals(rawAttributes[i].type.tsym.flatName().toString())) {
jjg@1645 315 containerIndex = i;
jjg@1645 316 container = rawAttributes[i];
jjg@1645 317 }
jjg@1645 318 }
jjg@1645 319
jjg@1645 320 // Pack them in an array
jjg@1645 321 Attribute[] contained0 = new Attribute[0];
jjg@1645 322 if (container != null)
jjg@1645 323 contained0 = unpackAttributes(container);
jjg@1645 324 ListBuffer<Attribute.Compound> compounds = ListBuffer.lb();
jjg@1645 325 for (Attribute a : contained0) {
jjg@1645 326 if (a instanceof Attribute.Compound)
jjg@1645 327 compounds = compounds.append((Attribute.Compound)a);
jjg@1645 328 }
jjg@1645 329 Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[0]);
jjg@1645 330
jjg@1645 331 int size = (direct == null ? 0 : 1) + contained.length;
jjg@1645 332 @SuppressWarnings("unchecked") // annoType is the Class for A
jjg@1645 333 A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
jjg@1645 334
jjg@1645 335 // if direct && container, which is first?
jjg@1645 336 int insert = -1;
jjg@1645 337 int length = arr.length;
jjg@1645 338 if (directIndex >= 0 && containerIndex >= 0) {
jjg@1645 339 if (directIndex < containerIndex) {
jjg@1645 340 arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
jjg@1645 341 insert = 1;
jjg@1645 342 } else {
jjg@1645 343 arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
jjg@1645 344 insert = 0;
jjg@1645 345 length--;
jjg@1645 346 }
jjg@1645 347 } else if (directIndex >= 0) {
jjg@1645 348 arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
jjg@1645 349 return arr;
jjg@1645 350 } else {
jjg@1645 351 // Only container
jjg@1645 352 insert = 0;
jjg@1645 353 }
jjg@1645 354
jjg@1645 355 for (int i = 0; i + insert < length; i++)
jjg@1645 356 arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType);
jjg@1645 357
jjg@1645 358 return arr;
jjg@1645 359 }
jjg@1645 360
jjg@1645 361 // </editor-fold>
jjg@1645 362
jjg@1645 363 // <editor-fold defaultstate="collapsed" desc="Container support">
jjg@1645 364
jjg@1645 365 // Needed to unpack the runtime view of containing annotations
jjg@1645 366 private static final Class<? extends Annotation> REPEATABLE_CLASS = initRepeatable();
jjg@1645 367 private static final Method VALUE_ELEMENT_METHOD = initValueElementMethod();
jjg@1645 368
jjg@1645 369 private static Class<? extends Annotation> initRepeatable() {
jjg@1645 370 try {
jjg@1645 371 // Repeatable will not be available when bootstrapping on
jjg@1645 372 // JDK 7 so use a reflective lookup instead of a class
jjg@1645 373 // literal for Repeatable.class.
jjg@1645 374 return Class.forName("java.lang.annotation.Repeatable").asSubclass(Annotation.class);
jjg@1645 375 } catch (ClassNotFoundException e) {
jjg@1645 376 return null;
jjg@1645 377 } catch (SecurityException e) {
jjg@1645 378 return null;
jjg@1645 379 }
jjg@1645 380 }
jjg@1645 381
jjg@1645 382 private static Method initValueElementMethod() {
jjg@1645 383 if (REPEATABLE_CLASS == null)
jjg@1645 384 return null;
jjg@1645 385
jjg@1645 386 Method m = null;
jjg@1645 387 try {
jjg@1645 388 m = REPEATABLE_CLASS.getMethod("value");
jjg@1645 389 if (m != null)
jjg@1645 390 m.setAccessible(true);
jjg@1645 391 return m;
jjg@1645 392 } catch (NoSuchMethodException e) {
jjg@1645 393 return null;
jjg@1645 394 }
jjg@1645 395 }
jjg@1645 396
jjg@1645 397 // Helper to getAnnotations
jjg@1645 398 private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) {
jjg@1645 399 // Since we can not refer to java.lang.annotation.Repeatable until we are
jjg@1645 400 // bootstrapping with java 8 we need to get the Repeatable annotation using
jjg@1645 401 // reflective invocations instead of just using its type and element method.
jjg@1645 402 if (REPEATABLE_CLASS != null &&
jjg@1645 403 VALUE_ELEMENT_METHOD != null) {
jjg@1645 404 // Get the Repeatable instance on the annotations declaration
jjg@1645 405 Annotation repeatable = (Annotation)annoType.getAnnotation(REPEATABLE_CLASS);
jjg@1645 406 if (repeatable != null) {
jjg@1645 407 try {
jjg@1645 408 // Get the value element, it should be a class
jjg@1645 409 // indicating the containing annotation type
jjg@1645 410 @SuppressWarnings("unchecked")
jjg@1645 411 Class<? extends Annotation> containerType = (Class)VALUE_ELEMENT_METHOD.invoke(repeatable);
jjg@1645 412 if (containerType == null)
jjg@1645 413 return null;
jjg@1645 414
jjg@1645 415 return containerType;
jjg@1645 416 } catch (ClassCastException e) {
jjg@1645 417 return null;
jjg@1645 418 } catch (IllegalAccessException e) {
jjg@1645 419 return null;
jjg@1645 420 } catch (InvocationTargetException e ) {
jjg@1645 421 return null;
jjg@1645 422 }
jjg@1645 423 }
jjg@1645 424 }
jjg@1645 425 return null;
jjg@1645 426 }
jjg@1645 427
jjg@1645 428 // Helper to getAnnotations
jjg@1645 429 private static Attribute[] unpackAttributes(Attribute.Compound container) {
jjg@1645 430 // We now have an instance of the container,
jjg@1645 431 // unpack it returning an instance of the
jjg@1645 432 // contained type or null
jjg@1645 433 return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values;
jjg@1645 434 }
jjg@1645 435
jjg@1645 436 // </editor-fold>
jjg@1645 437 }

mercurial