Thu, 10 Oct 2013 08:51:55 +0200
8021237: clean up JavacAnnotatedConstruct
Summary: Refactored the static helper methods in JavacAnnoConstructs into ordinary methods and put them in a common superclass (AnnoConstruct) of Symbol and Type.
Reviewed-by: jjg, vromero, jfranck
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; |
alundblad@2100 | 28 | import java.lang.reflect.InvocationTargetException; |
alundblad@2100 | 29 | import java.lang.reflect.Method; |
alundblad@2100 | 30 | |
alundblad@2100 | 31 | import javax.lang.model.AnnotatedConstruct; |
alundblad@2100 | 32 | |
alundblad@2100 | 33 | import com.sun.tools.javac.model.AnnotationProxyMaker; |
alundblad@2100 | 34 | import com.sun.tools.javac.util.List; |
alundblad@2100 | 35 | import com.sun.tools.javac.util.ListBuffer; |
alundblad@2100 | 36 | |
alundblad@2100 | 37 | /** |
alundblad@2100 | 38 | * Common super type for annotated constructs such as Types and Symbols. |
alundblad@2100 | 39 | * |
alundblad@2100 | 40 | * This class should *not* contain any fields since it would have a significant |
alundblad@2100 | 41 | * impact on the javac memory footprint. |
alundblad@2100 | 42 | * |
alundblad@2100 | 43 | * <p><b>This is NOT part of any supported API. |
alundblad@2100 | 44 | * If you write code that depends on this, you do so at your own |
alundblad@2100 | 45 | * risk. This code and its internal interfaces are subject to change |
alundblad@2100 | 46 | * or deletion without notice.</b></p> |
alundblad@2100 | 47 | */ |
alundblad@2100 | 48 | public abstract class AnnoConstruct implements AnnotatedConstruct { |
alundblad@2100 | 49 | |
alundblad@2100 | 50 | |
alundblad@2100 | 51 | // Override to enforce a narrower return type. |
alundblad@2100 | 52 | @Override |
alundblad@2100 | 53 | public abstract List<? extends Attribute.Compound> getAnnotationMirrors(); |
alundblad@2100 | 54 | |
alundblad@2100 | 55 | |
alundblad@2100 | 56 | // This method is part of the javax.lang.model API, do not use this in javac code. |
alundblad@2100 | 57 | protected <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) { |
alundblad@2100 | 58 | String name = annoType.getName(); |
alundblad@2100 | 59 | |
alundblad@2100 | 60 | for (Attribute.Compound anno : getAnnotationMirrors()) { |
alundblad@2100 | 61 | if (name.equals(anno.type.tsym.flatName().toString())) |
alundblad@2100 | 62 | return anno; |
alundblad@2100 | 63 | } |
alundblad@2100 | 64 | |
alundblad@2100 | 65 | return null; |
alundblad@2100 | 66 | } |
alundblad@2100 | 67 | |
alundblad@2100 | 68 | |
alundblad@2100 | 69 | @SuppressWarnings("unchecked") |
alundblad@2100 | 70 | protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) { |
alundblad@2100 | 71 | return (A[]) java.lang.reflect.Array.newInstance(annoType, 0); // annoType is the Class for A |
alundblad@2100 | 72 | } |
alundblad@2100 | 73 | |
alundblad@2100 | 74 | |
alundblad@2100 | 75 | // This method is part of the javax.lang.model API, do not use this in javac code. |
alundblad@2100 | 76 | public <A extends Annotation> A[] getAnnotationsByType(Class<A> annoType) { |
alundblad@2100 | 77 | |
alundblad@2100 | 78 | if (!annoType.isAnnotation()) |
alundblad@2100 | 79 | throw new IllegalArgumentException("Not an annotation type: " |
alundblad@2100 | 80 | + annoType); |
alundblad@2100 | 81 | // If annoType does not declare a container this is equivalent to wrapping |
alundblad@2100 | 82 | // getAnnotation(...) in an array. |
alundblad@2100 | 83 | Class <? extends Annotation> containerType = getContainer(annoType); |
alundblad@2100 | 84 | if (containerType == null) { |
alundblad@2100 | 85 | A res = getAnnotation(annoType); |
alundblad@2100 | 86 | int size = res == null ? 0 : 1; |
alundblad@2100 | 87 | |
alundblad@2100 | 88 | @SuppressWarnings("unchecked") // annoType is the Class for A |
alundblad@2100 | 89 | A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); |
alundblad@2100 | 90 | if (res != null) |
alundblad@2100 | 91 | arr[0] = res; |
alundblad@2100 | 92 | return arr; |
alundblad@2100 | 93 | } |
alundblad@2100 | 94 | |
alundblad@2100 | 95 | // So we have a containing type |
alundblad@2100 | 96 | String annoTypeName = annoType.getName(); |
alundblad@2100 | 97 | String containerTypeName = containerType.getName(); |
alundblad@2100 | 98 | int directIndex = -1, containerIndex = -1; |
alundblad@2100 | 99 | Attribute.Compound direct = null, container = null; |
alundblad@2100 | 100 | // Find directly (explicit or implicit) present annotations |
alundblad@2100 | 101 | int index = -1; |
alundblad@2100 | 102 | for (Attribute.Compound attribute : getAnnotationMirrors()) { |
alundblad@2100 | 103 | index++; |
alundblad@2100 | 104 | if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) { |
alundblad@2100 | 105 | directIndex = index; |
alundblad@2100 | 106 | direct = attribute; |
alundblad@2100 | 107 | } else if(containerTypeName != null && |
alundblad@2100 | 108 | attribute.type.tsym.flatName().contentEquals(containerTypeName)) { |
alundblad@2100 | 109 | containerIndex = index; |
alundblad@2100 | 110 | container = attribute; |
alundblad@2100 | 111 | } |
alundblad@2100 | 112 | } |
alundblad@2100 | 113 | |
alundblad@2100 | 114 | // Deal with inherited annotations |
alundblad@2100 | 115 | if (direct == null && container == null) |
alundblad@2100 | 116 | return getInheritedAnnotations(annoType); |
alundblad@2100 | 117 | |
alundblad@2100 | 118 | // Pack them in an array |
alundblad@2100 | 119 | Attribute[] contained0 = null; |
alundblad@2100 | 120 | if (container != null) |
alundblad@2100 | 121 | contained0 = unpackAttributes(container); |
alundblad@2100 | 122 | ListBuffer<Attribute.Compound> compounds = new ListBuffer<>(); |
alundblad@2100 | 123 | if (contained0 != null) { |
alundblad@2100 | 124 | for (Attribute a : contained0) |
alundblad@2100 | 125 | if (a instanceof Attribute.Compound) |
alundblad@2100 | 126 | compounds = compounds.append((Attribute.Compound)a); |
alundblad@2100 | 127 | } |
alundblad@2100 | 128 | Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[compounds.size()]); |
alundblad@2100 | 129 | |
alundblad@2100 | 130 | int size = (direct == null ? 0 : 1) + contained.length; |
alundblad@2100 | 131 | @SuppressWarnings("unchecked") // annoType is the Class for A |
alundblad@2100 | 132 | A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); |
alundblad@2100 | 133 | |
alundblad@2100 | 134 | // if direct && container, which is first? |
alundblad@2100 | 135 | int insert = -1; |
alundblad@2100 | 136 | int length = arr.length; |
alundblad@2100 | 137 | if (directIndex >= 0 && containerIndex >= 0) { |
alundblad@2100 | 138 | if (directIndex < containerIndex) { |
alundblad@2100 | 139 | arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
alundblad@2100 | 140 | insert = 1; |
alundblad@2100 | 141 | } else { |
alundblad@2100 | 142 | arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
alundblad@2100 | 143 | insert = 0; |
alundblad@2100 | 144 | length--; |
alundblad@2100 | 145 | } |
alundblad@2100 | 146 | } else if (directIndex >= 0) { |
alundblad@2100 | 147 | arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
alundblad@2100 | 148 | return arr; |
alundblad@2100 | 149 | } else { |
alundblad@2100 | 150 | // Only container |
alundblad@2100 | 151 | insert = 0; |
alundblad@2100 | 152 | } |
alundblad@2100 | 153 | |
alundblad@2100 | 154 | for (int i = 0; i + insert < length; i++) |
alundblad@2100 | 155 | arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType); |
alundblad@2100 | 156 | |
alundblad@2100 | 157 | return arr; |
alundblad@2100 | 158 | } |
alundblad@2100 | 159 | |
alundblad@2100 | 160 | |
alundblad@2100 | 161 | // This method is part of the javax.lang.model API, do not use this in javac code. |
alundblad@2100 | 162 | public <A extends Annotation> A getAnnotation(Class<A> annoType) { |
alundblad@2100 | 163 | |
alundblad@2100 | 164 | if (!annoType.isAnnotation()) |
alundblad@2100 | 165 | throw new IllegalArgumentException("Not an annotation type: " + annoType); |
alundblad@2100 | 166 | |
alundblad@2100 | 167 | Attribute.Compound c = getAttribute(annoType); |
alundblad@2100 | 168 | return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType); |
alundblad@2100 | 169 | } |
alundblad@2100 | 170 | |
alundblad@2100 | 171 | // Needed to unpack the runtime view of containing annotations |
alundblad@2100 | 172 | private static final Class<? extends Annotation> REPEATABLE_CLASS = initRepeatable(); |
alundblad@2100 | 173 | private static final Method VALUE_ELEMENT_METHOD = initValueElementMethod(); |
alundblad@2100 | 174 | |
alundblad@2100 | 175 | private static Class<? extends Annotation> initRepeatable() { |
alundblad@2100 | 176 | try { |
alundblad@2100 | 177 | // Repeatable will not be available when bootstrapping on |
alundblad@2100 | 178 | // JDK 7 so use a reflective lookup instead of a class |
alundblad@2100 | 179 | // literal for Repeatable.class. |
alundblad@2100 | 180 | return Class.forName("java.lang.annotation.Repeatable").asSubclass(Annotation.class); |
alundblad@2100 | 181 | } catch (ClassNotFoundException | SecurityException e) { |
alundblad@2100 | 182 | return null; |
alundblad@2100 | 183 | } |
alundblad@2100 | 184 | } |
alundblad@2100 | 185 | |
alundblad@2100 | 186 | private static Method initValueElementMethod() { |
alundblad@2100 | 187 | if (REPEATABLE_CLASS == null) |
alundblad@2100 | 188 | return null; |
alundblad@2100 | 189 | |
alundblad@2100 | 190 | Method m = null; |
alundblad@2100 | 191 | try { |
alundblad@2100 | 192 | m = REPEATABLE_CLASS.getMethod("value"); |
alundblad@2100 | 193 | if (m != null) |
alundblad@2100 | 194 | m.setAccessible(true); |
alundblad@2100 | 195 | return m; |
alundblad@2100 | 196 | } catch (NoSuchMethodException e) { |
alundblad@2100 | 197 | return null; |
alundblad@2100 | 198 | } |
alundblad@2100 | 199 | } |
alundblad@2100 | 200 | |
alundblad@2100 | 201 | |
alundblad@2100 | 202 | // Helper to getAnnotationsByType |
alundblad@2100 | 203 | private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) { |
alundblad@2100 | 204 | // Since we can not refer to java.lang.annotation.Repeatable until we are |
alundblad@2100 | 205 | // bootstrapping with java 8 we need to get the Repeatable annotation using |
alundblad@2100 | 206 | // reflective invocations instead of just using its type and element method. |
alundblad@2100 | 207 | if (REPEATABLE_CLASS != null && |
alundblad@2100 | 208 | VALUE_ELEMENT_METHOD != null) { |
alundblad@2100 | 209 | // Get the Repeatable instance on the annotations declaration |
alundblad@2100 | 210 | Annotation repeatable = (Annotation)annoType.getAnnotation(REPEATABLE_CLASS); |
alundblad@2100 | 211 | if (repeatable != null) { |
alundblad@2100 | 212 | try { |
alundblad@2100 | 213 | // Get the value element, it should be a class |
alundblad@2100 | 214 | // indicating the containing annotation type |
alundblad@2100 | 215 | @SuppressWarnings("unchecked") |
alundblad@2100 | 216 | Class<? extends Annotation> containerType = (Class)VALUE_ELEMENT_METHOD.invoke(repeatable); |
alundblad@2100 | 217 | if (containerType == null) |
alundblad@2100 | 218 | return null; |
alundblad@2100 | 219 | |
alundblad@2100 | 220 | return containerType; |
alundblad@2100 | 221 | } catch (ClassCastException | IllegalAccessException | InvocationTargetException e) { |
alundblad@2100 | 222 | return null; |
alundblad@2100 | 223 | } |
alundblad@2100 | 224 | } |
alundblad@2100 | 225 | } |
alundblad@2100 | 226 | return null; |
alundblad@2100 | 227 | } |
alundblad@2100 | 228 | |
alundblad@2100 | 229 | |
alundblad@2100 | 230 | // Helper to getAnnotationsByType |
alundblad@2100 | 231 | private static Attribute[] unpackAttributes(Attribute.Compound container) { |
alundblad@2100 | 232 | // We now have an instance of the container, |
alundblad@2100 | 233 | // unpack it returning an instance of the |
alundblad@2100 | 234 | // contained type or null |
alundblad@2100 | 235 | return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values; |
alundblad@2100 | 236 | } |
alundblad@2100 | 237 | |
alundblad@2100 | 238 | } |