author | mcimadamore |
Mon, 05 Dec 2016 19:00:56 +0000 | |
changeset 42416 | 1cfad0990b99 |
parent 34752 | 9c262a013456 |
permissions | -rw-r--r-- |
21007 | 1 |
/* |
28341
8f015bb3a75f
8006469: Cleanup reflective access of java.lang.annotation.Repeatable
darcy
parents:
26266
diff
changeset
|
2 |
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. |
21007 | 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 |
*/ |
|
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
28341
diff
changeset
|
25 |
|
21007 | 26 |
package com.sun.tools.javac.code; |
27 |
||
28 |
import java.lang.annotation.Annotation; |
|
21485
2759750a8cdf
8026855: AnnoConstruct.getAnnotationsByType includes inherited indirectly present annotations even when containee type is not inheritable
jfranck
parents:
21007
diff
changeset
|
29 |
import java.lang.annotation.Inherited; |
28341
8f015bb3a75f
8006469: Cleanup reflective access of java.lang.annotation.Repeatable
darcy
parents:
26266
diff
changeset
|
30 |
import java.lang.annotation.Repeatable; |
21007 | 31 |
import java.lang.reflect.InvocationTargetException; |
32 |
import java.lang.reflect.Method; |
|
33 |
||
34 |
import javax.lang.model.AnnotatedConstruct; |
|
35 |
||
36 |
import com.sun.tools.javac.model.AnnotationProxyMaker; |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
37 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
38 |
import com.sun.tools.javac.util.DefinedBy.Api; |
21007 | 39 |
import com.sun.tools.javac.util.List; |
40 |
import com.sun.tools.javac.util.ListBuffer; |
|
41 |
||
42 |
/** |
|
43 |
* Common super type for annotated constructs such as Types and Symbols. |
|
44 |
* |
|
45 |
* This class should *not* contain any fields since it would have a significant |
|
46 |
* impact on the javac memory footprint. |
|
47 |
* |
|
48 |
* <p><b>This is NOT part of any supported API. |
|
49 |
* If you write code that depends on this, you do so at your own |
|
50 |
* risk. This code and its internal interfaces are subject to change |
|
51 |
* or deletion without notice.</b></p> |
|
52 |
*/ |
|
53 |
public abstract class AnnoConstruct implements AnnotatedConstruct { |
|
54 |
||
55 |
||
56 |
// Override to enforce a narrower return type. |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
57 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
21007 | 58 |
public abstract List<? extends Attribute.Compound> getAnnotationMirrors(); |
59 |
||
60 |
||
61 |
// This method is part of the javax.lang.model API, do not use this in javac code. |
|
62 |
protected <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) { |
|
63 |
String name = annoType.getName(); |
|
64 |
||
65 |
for (Attribute.Compound anno : getAnnotationMirrors()) { |
|
66 |
if (name.equals(anno.type.tsym.flatName().toString())) |
|
67 |
return anno; |
|
68 |
} |
|
69 |
||
70 |
return null; |
|
71 |
} |
|
72 |
||
73 |
||
74 |
@SuppressWarnings("unchecked") |
|
75 |
protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) { |
|
76 |
return (A[]) java.lang.reflect.Array.newInstance(annoType, 0); // annoType is the Class for A |
|
77 |
} |
|
78 |
||
79 |
||
80 |
// This method is part of the javax.lang.model API, do not use this in javac code. |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
81 |
@DefinedBy(Api.LANGUAGE_MODEL) |
21007 | 82 |
public <A extends Annotation> A[] getAnnotationsByType(Class<A> annoType) { |
83 |
||
84 |
if (!annoType.isAnnotation()) |
|
85 |
throw new IllegalArgumentException("Not an annotation type: " |
|
86 |
+ annoType); |
|
87 |
// If annoType does not declare a container this is equivalent to wrapping |
|
88 |
// getAnnotation(...) in an array. |
|
89 |
Class <? extends Annotation> containerType = getContainer(annoType); |
|
90 |
if (containerType == null) { |
|
91 |
A res = getAnnotation(annoType); |
|
92 |
int size = res == null ? 0 : 1; |
|
93 |
||
94 |
@SuppressWarnings("unchecked") // annoType is the Class for A |
|
95 |
A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); |
|
96 |
if (res != null) |
|
97 |
arr[0] = res; |
|
98 |
return arr; |
|
99 |
} |
|
100 |
||
101 |
// So we have a containing type |
|
102 |
String annoTypeName = annoType.getName(); |
|
103 |
String containerTypeName = containerType.getName(); |
|
104 |
int directIndex = -1, containerIndex = -1; |
|
105 |
Attribute.Compound direct = null, container = null; |
|
106 |
// Find directly (explicit or implicit) present annotations |
|
107 |
int index = -1; |
|
108 |
for (Attribute.Compound attribute : getAnnotationMirrors()) { |
|
109 |
index++; |
|
110 |
if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) { |
|
111 |
directIndex = index; |
|
112 |
direct = attribute; |
|
113 |
} else if(containerTypeName != null && |
|
114 |
attribute.type.tsym.flatName().contentEquals(containerTypeName)) { |
|
115 |
containerIndex = index; |
|
116 |
container = attribute; |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
// Deal with inherited annotations |
|
21485
2759750a8cdf
8026855: AnnoConstruct.getAnnotationsByType includes inherited indirectly present annotations even when containee type is not inheritable
jfranck
parents:
21007
diff
changeset
|
121 |
if (direct == null && container == null && |
2759750a8cdf
8026855: AnnoConstruct.getAnnotationsByType includes inherited indirectly present annotations even when containee type is not inheritable
jfranck
parents:
21007
diff
changeset
|
122 |
annoType.isAnnotationPresent(Inherited.class)) |
21007 | 123 |
return getInheritedAnnotations(annoType); |
124 |
||
21487
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
125 |
Attribute.Compound[] contained = unpackContained(container); |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
126 |
|
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
127 |
// In case of an empty legacy container we might need to look for |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
128 |
// inherited annos as well |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
129 |
if (direct == null && contained.length == 0 && |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
130 |
annoType.isAnnotationPresent(Inherited.class)) |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
131 |
return getInheritedAnnotations(annoType); |
21007 | 132 |
|
133 |
int size = (direct == null ? 0 : 1) + contained.length; |
|
134 |
@SuppressWarnings("unchecked") // annoType is the Class for A |
|
135 |
A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size); |
|
136 |
||
137 |
// if direct && container, which is first? |
|
138 |
int insert = -1; |
|
139 |
int length = arr.length; |
|
140 |
if (directIndex >= 0 && containerIndex >= 0) { |
|
141 |
if (directIndex < containerIndex) { |
|
142 |
arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
|
143 |
insert = 1; |
|
144 |
} else { |
|
145 |
arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
|
146 |
insert = 0; |
|
147 |
length--; |
|
148 |
} |
|
149 |
} else if (directIndex >= 0) { |
|
150 |
arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType); |
|
151 |
return arr; |
|
152 |
} else { |
|
153 |
// Only container |
|
154 |
insert = 0; |
|
155 |
} |
|
156 |
||
157 |
for (int i = 0; i + insert < length; i++) |
|
158 |
arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType); |
|
159 |
||
160 |
return arr; |
|
161 |
} |
|
162 |
||
21487
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
163 |
private Attribute.Compound[] unpackContained(Attribute.Compound container) { |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
164 |
// Pack them in an array |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
165 |
Attribute[] contained0 = null; |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
166 |
if (container != null) |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
167 |
contained0 = unpackAttributes(container); |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
168 |
ListBuffer<Attribute.Compound> compounds = new ListBuffer<>(); |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
169 |
if (contained0 != null) { |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
170 |
for (Attribute a : contained0) |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
171 |
if (a instanceof Attribute.Compound) |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
172 |
compounds = compounds.append((Attribute.Compound)a); |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
173 |
} |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
174 |
return compounds.toArray(new Attribute.Compound[compounds.size()]); |
c00623074ad0
8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents:
21485
diff
changeset
|
175 |
} |
21007 | 176 |
|
177 |
// This method is part of the javax.lang.model API, do not use this in javac code. |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
178 |
@DefinedBy(Api.LANGUAGE_MODEL) |
21007 | 179 |
public <A extends Annotation> A getAnnotation(Class<A> annoType) { |
180 |
||
181 |
if (!annoType.isAnnotation()) |
|
182 |
throw new IllegalArgumentException("Not an annotation type: " + annoType); |
|
183 |
||
184 |
Attribute.Compound c = getAttribute(annoType); |
|
185 |
return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType); |
|
186 |
} |
|
187 |
||
188 |
// Helper to getAnnotationsByType |
|
189 |
private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) { |
|
28341
8f015bb3a75f
8006469: Cleanup reflective access of java.lang.annotation.Repeatable
darcy
parents:
26266
diff
changeset
|
190 |
Repeatable repeatable = annoType.getAnnotation(Repeatable.class); |
8f015bb3a75f
8006469: Cleanup reflective access of java.lang.annotation.Repeatable
darcy
parents:
26266
diff
changeset
|
191 |
return (repeatable == null) ? null : repeatable.value(); |
21007 | 192 |
} |
193 |
||
194 |
// Helper to getAnnotationsByType |
|
195 |
private static Attribute[] unpackAttributes(Attribute.Compound container) { |
|
196 |
// We now have an instance of the container, |
|
197 |
// unpack it returning an instance of the |
|
198 |
// contained type or null |
|
199 |
return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values; |
|
200 |
} |
|
201 |
||
202 |
} |