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