Thu, 25 Jul 2013 11:02:27 +0200
8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
Reviewed-by: jjg
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] |
jfranck@1918 | 111 | private static <A extends Annotation> Attribute.Compound getAttributeOnClass( |
jfranck@1918 | 112 | ClassSymbol annotated, |
jfranck@1918 | 113 | final Class<A> annoType) |
jfranck@1918 | 114 | { |
jjg@1645 | 115 | boolean inherited = annoType.isAnnotationPresent(Inherited.class); |
jjg@1645 | 116 | Attribute.Compound result = null; |
jfranck@1918 | 117 | |
jfranck@1918 | 118 | result = getAttribute(annotated, annoType); |
jfranck@1918 | 119 | if (result != null || !inherited) |
jfranck@1918 | 120 | return result; |
jfranck@1918 | 121 | |
jfranck@1918 | 122 | while ((annotated = nextSupertypeToSearch(annotated)) != null) { |
jjg@1645 | 123 | result = getAttribute(annotated, annoType); |
jfranck@1918 | 124 | if (result != null) |
jfranck@1918 | 125 | return result; |
jjg@1645 | 126 | } |
jfranck@1918 | 127 | return null; // no more supertypes to search |
jfranck@1918 | 128 | } |
jfranck@1918 | 129 | |
jfranck@1918 | 130 | /** |
jfranck@1918 | 131 | * Returns the next type to search for inherited annotations or {@code null} |
jfranck@1918 | 132 | * if the next type can't be found. |
jfranck@1918 | 133 | */ |
jfranck@1918 | 134 | private static ClassSymbol nextSupertypeToSearch(ClassSymbol annotated) { |
jfranck@1918 | 135 | if (annotated.name == annotated.name.table.names.java_lang_Object) |
jfranck@1918 | 136 | return null; |
jfranck@1918 | 137 | |
jfranck@1918 | 138 | Type sup = annotated.getSuperclass(); |
jfranck@1918 | 139 | if (!sup.hasTag(CLASS) || sup.isErroneous()) |
jfranck@1918 | 140 | return null; |
jfranck@1918 | 141 | |
jfranck@1918 | 142 | return (ClassSymbol) sup.tsym; |
jjg@1645 | 143 | } |
jjg@1645 | 144 | |
jjg@1645 | 145 | /** |
jjg@1645 | 146 | * An internal-use utility that creates a runtime view of |
jjg@1645 | 147 | * annotations. This is the implementation of |
jjg@1645 | 148 | * Element.getAnnotations(Class). |
jjg@1645 | 149 | */ |
jfranck@1918 | 150 | public static <A extends Annotation> A[] getAnnotationsByType(Symbol annotated, |
jfranck@1918 | 151 | Class<A> annoType) |
jfranck@1918 | 152 | { |
jjg@1645 | 153 | if (!annoType.isAnnotation()) |
jjg@1645 | 154 | throw new IllegalArgumentException("Not an annotation type: " |
jjg@1645 | 155 | + annoType); |
jjg@1645 | 156 | // If annoType does not declare a container this is equivalent to wrapping |
jjg@1645 | 157 | // getAnnotation(...) in an array. |
jjg@1645 | 158 | Class <? extends Annotation> containerType = getContainer(annoType); |
jjg@1645 | 159 | if (containerType == null) { |
jjg@1645 | 160 | A res = getAnnotation(annotated, annoType); |
jjg@1645 | 161 | int size; |
jjg@1645 | 162 | if (res == null) { |
jjg@1645 | 163 | size = 0; |
jjg@1645 | 164 | } else { |
jjg@1645 | 165 | size = 1; |
jjg@1645 | 166 | } |
jjg@1645 | 167 | @SuppressWarnings("unchecked") // annoType is the Class for A |
jjg@1645 | 168 | A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); |
jjg@1645 | 169 | if (res != null) |
jjg@1645 | 170 | arr[0] = res; |
jjg@1645 | 171 | return arr; |
jjg@1645 | 172 | } |
jjg@1645 | 173 | |
jjg@1645 | 174 | // So we have a containing type |
jjg@1645 | 175 | String annoTypeName = annoType.getSimpleName(); |
jjg@1645 | 176 | String containerTypeName = containerType.getSimpleName(); |
jjg@1645 | 177 | int directIndex = -1, containerIndex = -1; |
jjg@1645 | 178 | Attribute.Compound direct = null, container = null; |
jfranck@1918 | 179 | // Find directly (explicit or implicit) present annotations |
jfranck@1918 | 180 | int index = -1; |
jfranck@1918 | 181 | for (List<Attribute.Compound> list = annotated.getAnnotationMirrors(); |
jfranck@1918 | 182 | !list.isEmpty(); |
jfranck@1918 | 183 | list = list.tail) { |
jfranck@1918 | 184 | Attribute.Compound attribute = list.head; |
jfranck@1918 | 185 | index++; |
jfranck@1918 | 186 | if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) { |
jfranck@1918 | 187 | directIndex = index; |
jfranck@1918 | 188 | direct = attribute; |
jjg@1645 | 189 | } else if(containerTypeName != null && |
jfranck@1918 | 190 | attribute.type.tsym.flatName().contentEquals(containerTypeName)) { |
jfranck@1918 | 191 | containerIndex = index; |
jfranck@1918 | 192 | container = attribute; |
jjg@1645 | 193 | } |
jjg@1645 | 194 | } |
jjg@1645 | 195 | |
jjg@1645 | 196 | // Deal with inherited annotations |
jfranck@1918 | 197 | if (direct == null && container == null) { |
jfranck@1918 | 198 | if (annotated.kind == Kinds.TYP && |
jfranck@1918 | 199 | (annotated instanceof ClassSymbol)) { |
jfranck@1918 | 200 | ClassSymbol s = nextSupertypeToSearch((ClassSymbol)annotated); |
jfranck@1918 | 201 | if (s != null) |
jfranck@1918 | 202 | return getAnnotationsByType(s, annoType); |
jjg@1645 | 203 | } |
jjg@1645 | 204 | } |
jjg@1645 | 205 | |
jjg@1645 | 206 | // Pack them in an array |
jfranck@1918 | 207 | Attribute[] contained0 = null; |
jjg@1645 | 208 | if (container != null) |
jjg@1645 | 209 | contained0 = unpackAttributes(container); |
jjg@1645 | 210 | ListBuffer<Attribute.Compound> compounds = ListBuffer.lb(); |
jfranck@1918 | 211 | if (contained0 != null) { |
jfranck@1918 | 212 | for (Attribute a : contained0) |
jfranck@1918 | 213 | if (a instanceof Attribute.Compound) |
jfranck@1918 | 214 | compounds = compounds.append((Attribute.Compound)a); |
jfranck@1918 | 215 | } |
jfranck@1918 | 216 | Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[compounds.size()]); |
jjg@1645 | 217 | |
jjg@1645 | 218 | int size = (direct == null ? 0 : 1) + contained.length; |
jjg@1645 | 219 | @SuppressWarnings("unchecked") // annoType is the Class for A |
jjg@1645 | 220 | A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); |
jjg@1645 | 221 | |
jjg@1645 | 222 | // if direct && container, which is first? |
jjg@1645 | 223 | int insert = -1; |
jjg@1645 | 224 | int length = arr.length; |
jjg@1645 | 225 | if (directIndex >= 0 && containerIndex >= 0) { |
jjg@1645 | 226 | if (directIndex < containerIndex) { |
jjg@1645 | 227 | arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
jjg@1645 | 228 | insert = 1; |
jjg@1645 | 229 | } else { |
jjg@1645 | 230 | arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
jjg@1645 | 231 | insert = 0; |
jjg@1645 | 232 | length--; |
jjg@1645 | 233 | } |
jjg@1645 | 234 | } else if (directIndex >= 0) { |
jjg@1645 | 235 | arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
jjg@1645 | 236 | return arr; |
jjg@1645 | 237 | } else { |
jjg@1645 | 238 | // Only container |
jjg@1645 | 239 | insert = 0; |
jjg@1645 | 240 | } |
jjg@1645 | 241 | |
jjg@1645 | 242 | for (int i = 0; i + insert < length; i++) |
jjg@1645 | 243 | arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType); |
jjg@1645 | 244 | |
jjg@1645 | 245 | return arr; |
jjg@1645 | 246 | } |
jjg@1645 | 247 | |
jjg@1645 | 248 | // </editor-fold> |
jjg@1645 | 249 | |
jjg@1645 | 250 | // <editor-fold defaultstate="collapsed" desc="Types"> |
jjg@1645 | 251 | |
jjg@1645 | 252 | /** |
jjg@1645 | 253 | * An internal-use utility that creates a runtime view of an |
jjg@1645 | 254 | * annotation. This is the implementation of |
jjg@1645 | 255 | * TypeMirror.getAnnotation(Class). |
jjg@1645 | 256 | */ |
jjg@1645 | 257 | public static <A extends Annotation> A getAnnotation(AnnotatedType annotated, Class<A> annoType) { |
jjg@1645 | 258 | if (!annoType.isAnnotation()) |
jjg@1645 | 259 | throw new IllegalArgumentException("Not an annotation type: " |
jjg@1645 | 260 | + annoType); |
jjg@1645 | 261 | Attribute.Compound c = getAttribute(annotated, annoType); |
jjg@1645 | 262 | return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType); |
jjg@1645 | 263 | } |
jjg@1645 | 264 | |
jjg@1645 | 265 | // Helper to getAnnotation[s] |
jjg@1645 | 266 | private static <A extends Annotation> Attribute.Compound getAttribute(Type annotated, |
jjg@1645 | 267 | Class<A> annoType) { |
jjg@1645 | 268 | String name = annoType.getName(); |
jjg@1645 | 269 | |
jjg@1645 | 270 | for (Attribute.Compound anno : annotated.getAnnotationMirrors()) { |
jjg@1645 | 271 | if (name.equals(anno.type.tsym.flatName().toString())) |
jjg@1645 | 272 | return anno; |
jjg@1645 | 273 | } |
jjg@1645 | 274 | |
jjg@1645 | 275 | return null; |
jjg@1645 | 276 | } |
jjg@1645 | 277 | |
jjg@1645 | 278 | /** |
jjg@1645 | 279 | * An internal-use utility that creates a runtime view of |
jjg@1645 | 280 | * annotations. This is the implementation of |
jjg@1645 | 281 | * TypeMirror.getAnnotationsByType(Class). |
jjg@1645 | 282 | */ |
jjg@1645 | 283 | public static <A extends Annotation> A[] getAnnotationsByType(AnnotatedType annotated, Class<A> annoType) { |
jjg@1645 | 284 | if (!annoType.isAnnotation()) |
jjg@1645 | 285 | throw new IllegalArgumentException("Not an annotation type: " |
jjg@1645 | 286 | + annoType); |
jjg@1645 | 287 | // If annoType does not declare a container this is equivalent to wrapping |
jjg@1645 | 288 | // getAnnotation(...) in an array. |
jjg@1645 | 289 | Class <? extends Annotation> containerType = getContainer(annoType); |
jjg@1645 | 290 | if (containerType == null) { |
jjg@1645 | 291 | A res = getAnnotation(annotated, annoType); |
jjg@1645 | 292 | int size; |
jjg@1645 | 293 | if (res == null) { |
jjg@1645 | 294 | size = 0; |
jjg@1645 | 295 | } else { |
jjg@1645 | 296 | size = 1; |
jjg@1645 | 297 | } |
jjg@1645 | 298 | @SuppressWarnings("unchecked") // annoType is the Class for A |
jjg@1645 | 299 | A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); |
jjg@1645 | 300 | if (res != null) |
jjg@1645 | 301 | arr[0] = res; |
jjg@1645 | 302 | return arr; |
jjg@1645 | 303 | } |
jjg@1645 | 304 | |
jjg@1645 | 305 | // So we have a containing type |
jjg@1645 | 306 | String annoTypeName = annoType.getSimpleName(); |
jjg@1645 | 307 | String containerTypeName = containerType.getSimpleName(); |
jjg@1645 | 308 | int directIndex = -1, containerIndex = -1; |
jjg@1645 | 309 | Attribute.Compound direct = null, container = null; |
jfranck@1918 | 310 | // Find directly (explicit or implicit) present annotations |
jfranck@1918 | 311 | int index = -1; |
jfranck@1918 | 312 | for (List<? extends Attribute.Compound> list = annotated.getAnnotationMirrors(); |
jfranck@1918 | 313 | !list.isEmpty(); |
jfranck@1918 | 314 | list = list.tail) { |
jfranck@1918 | 315 | Attribute.Compound attribute = list.head; |
jfranck@1918 | 316 | index++; |
jfranck@1918 | 317 | if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) { |
jfranck@1918 | 318 | directIndex = index; |
jfranck@1918 | 319 | direct = attribute; |
jjg@1645 | 320 | } else if(containerTypeName != null && |
jfranck@1918 | 321 | attribute.type.tsym.flatName().contentEquals(containerTypeName)) { |
jfranck@1918 | 322 | containerIndex = index; |
jfranck@1918 | 323 | container = attribute; |
jjg@1645 | 324 | } |
jjg@1645 | 325 | } |
jjg@1645 | 326 | |
jjg@1645 | 327 | // Pack them in an array |
jfranck@1918 | 328 | Attribute[] contained0 = null; |
jjg@1645 | 329 | if (container != null) |
jjg@1645 | 330 | contained0 = unpackAttributes(container); |
jjg@1645 | 331 | ListBuffer<Attribute.Compound> compounds = ListBuffer.lb(); |
jfranck@1918 | 332 | if (contained0 != null) { |
jfranck@1918 | 333 | for (Attribute a : contained0) |
jfranck@1918 | 334 | if (a instanceof Attribute.Compound) |
jfranck@1918 | 335 | compounds = compounds.append((Attribute.Compound)a); |
jjg@1645 | 336 | } |
jfranck@1918 | 337 | Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[compounds.size()]); |
jjg@1645 | 338 | |
jjg@1645 | 339 | int size = (direct == null ? 0 : 1) + contained.length; |
jjg@1645 | 340 | @SuppressWarnings("unchecked") // annoType is the Class for A |
jjg@1645 | 341 | A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); |
jjg@1645 | 342 | |
jjg@1645 | 343 | // if direct && container, which is first? |
jjg@1645 | 344 | int insert = -1; |
jjg@1645 | 345 | int length = arr.length; |
jjg@1645 | 346 | if (directIndex >= 0 && containerIndex >= 0) { |
jjg@1645 | 347 | if (directIndex < containerIndex) { |
jjg@1645 | 348 | arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
jjg@1645 | 349 | insert = 1; |
jjg@1645 | 350 | } else { |
jjg@1645 | 351 | arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
jjg@1645 | 352 | insert = 0; |
jjg@1645 | 353 | length--; |
jjg@1645 | 354 | } |
jjg@1645 | 355 | } else if (directIndex >= 0) { |
jjg@1645 | 356 | arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
jjg@1645 | 357 | return arr; |
jjg@1645 | 358 | } else { |
jjg@1645 | 359 | // Only container |
jjg@1645 | 360 | insert = 0; |
jjg@1645 | 361 | } |
jjg@1645 | 362 | |
jjg@1645 | 363 | for (int i = 0; i + insert < length; i++) |
jjg@1645 | 364 | arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType); |
jjg@1645 | 365 | |
jjg@1645 | 366 | return arr; |
jjg@1645 | 367 | } |
jjg@1645 | 368 | |
jjg@1645 | 369 | // </editor-fold> |
jjg@1645 | 370 | |
jjg@1645 | 371 | // <editor-fold defaultstate="collapsed" desc="Container support"> |
jjg@1645 | 372 | |
jjg@1645 | 373 | // Needed to unpack the runtime view of containing annotations |
jjg@1645 | 374 | private static final Class<? extends Annotation> REPEATABLE_CLASS = initRepeatable(); |
jjg@1645 | 375 | private static final Method VALUE_ELEMENT_METHOD = initValueElementMethod(); |
jjg@1645 | 376 | |
jjg@1645 | 377 | private static Class<? extends Annotation> initRepeatable() { |
jjg@1645 | 378 | try { |
jjg@1645 | 379 | // Repeatable will not be available when bootstrapping on |
jjg@1645 | 380 | // JDK 7 so use a reflective lookup instead of a class |
jjg@1645 | 381 | // literal for Repeatable.class. |
jjg@1645 | 382 | return Class.forName("java.lang.annotation.Repeatable").asSubclass(Annotation.class); |
jjg@1645 | 383 | } catch (ClassNotFoundException e) { |
jjg@1645 | 384 | return null; |
jjg@1645 | 385 | } catch (SecurityException e) { |
jjg@1645 | 386 | return null; |
jjg@1645 | 387 | } |
jjg@1645 | 388 | } |
jjg@1645 | 389 | |
jjg@1645 | 390 | private static Method initValueElementMethod() { |
jjg@1645 | 391 | if (REPEATABLE_CLASS == null) |
jjg@1645 | 392 | return null; |
jjg@1645 | 393 | |
jjg@1645 | 394 | Method m = null; |
jjg@1645 | 395 | try { |
jjg@1645 | 396 | m = REPEATABLE_CLASS.getMethod("value"); |
jjg@1645 | 397 | if (m != null) |
jjg@1645 | 398 | m.setAccessible(true); |
jjg@1645 | 399 | return m; |
jjg@1645 | 400 | } catch (NoSuchMethodException e) { |
jjg@1645 | 401 | return null; |
jjg@1645 | 402 | } |
jjg@1645 | 403 | } |
jjg@1645 | 404 | |
jjg@1645 | 405 | // Helper to getAnnotations |
jjg@1645 | 406 | private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) { |
jjg@1645 | 407 | // Since we can not refer to java.lang.annotation.Repeatable until we are |
jjg@1645 | 408 | // bootstrapping with java 8 we need to get the Repeatable annotation using |
jjg@1645 | 409 | // reflective invocations instead of just using its type and element method. |
jjg@1645 | 410 | if (REPEATABLE_CLASS != null && |
jjg@1645 | 411 | VALUE_ELEMENT_METHOD != null) { |
jjg@1645 | 412 | // Get the Repeatable instance on the annotations declaration |
jjg@1645 | 413 | Annotation repeatable = (Annotation)annoType.getAnnotation(REPEATABLE_CLASS); |
jjg@1645 | 414 | if (repeatable != null) { |
jjg@1645 | 415 | try { |
jjg@1645 | 416 | // Get the value element, it should be a class |
jjg@1645 | 417 | // indicating the containing annotation type |
jjg@1645 | 418 | @SuppressWarnings("unchecked") |
jjg@1645 | 419 | Class<? extends Annotation> containerType = (Class)VALUE_ELEMENT_METHOD.invoke(repeatable); |
jjg@1645 | 420 | if (containerType == null) |
jjg@1645 | 421 | return null; |
jjg@1645 | 422 | |
jjg@1645 | 423 | return containerType; |
jjg@1645 | 424 | } catch (ClassCastException e) { |
jjg@1645 | 425 | return null; |
jjg@1645 | 426 | } catch (IllegalAccessException e) { |
jjg@1645 | 427 | return null; |
jjg@1645 | 428 | } catch (InvocationTargetException e ) { |
jjg@1645 | 429 | return null; |
jjg@1645 | 430 | } |
jjg@1645 | 431 | } |
jjg@1645 | 432 | } |
jjg@1645 | 433 | return null; |
jjg@1645 | 434 | } |
jjg@1645 | 435 | |
jjg@1645 | 436 | // Helper to getAnnotations |
jjg@1645 | 437 | private static Attribute[] unpackAttributes(Attribute.Compound container) { |
jjg@1645 | 438 | // We now have an instance of the container, |
jjg@1645 | 439 | // unpack it returning an instance of the |
jjg@1645 | 440 | // contained type or null |
jjg@1645 | 441 | return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values; |
jjg@1645 | 442 | } |
jjg@1645 | 443 | |
jjg@1645 | 444 | // </editor-fold> |
jjg@1645 | 445 | } |