-
Notifications
You must be signed in to change notification settings - Fork 53
/
AnnotationMirrorAltAnnotationImpl.java
executable file
·347 lines (283 loc) · 14.2 KB
/
AnnotationMirrorAltAnnotationImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.hk2.xml.internal.alt.papi;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.glassfish.hk2.xml.internal.Utilities;
import org.glassfish.hk2.xml.internal.alt.AltAnnotation;
import org.glassfish.hk2.xml.internal.alt.AltClass;
import org.glassfish.hk2.xml.internal.alt.AltEnum;
/**
* @author jwells
*
*/
public class AnnotationMirrorAltAnnotationImpl implements AltAnnotation {
private final ReentrantLock lock = new ReentrantLock();
private final AnnotationMirror annotation;
private final ProcessingEnvironment processingEnv;
private String type;
private Map<String, Object> values;
public AnnotationMirrorAltAnnotationImpl(AnnotationMirror annotation, ProcessingEnvironment processingEnv) {
this.annotation = annotation;
this.processingEnv = processingEnv;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltAnnotation#annotationType()
*/
@Override
public String annotationType() {
lock.lock();
try {
if (type != null) return type;
DeclaredType dt = annotation.getAnnotationType();
TypeElement clazzType = (TypeElement) dt.asElement();
type = Utilities.convertNameToString(clazzType.getQualifiedName());
return type;
} finally {
lock.unlock();
}
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltAnnotation#getStringValue(java.lang.String)
*/
@Override
public String getStringValue(String methodName) {
getAnnotationValues();
return (String) values.get(methodName);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltAnnotation#getBooleanValue(java.lang.String)
*/
@Override
public boolean getBooleanValue(String methodName) {
getAnnotationValues();
return (Boolean) values.get(methodName);
}
@Override
public String[] getStringArrayValue(String methodName) {
lock.lock();
try {
getAnnotationValues();
Object retVal = values.get(methodName);
if (retVal == null || !(retVal instanceof String[])) {
return null;
}
return (String[]) values.get(methodName);
} finally {
lock.unlock();
}
}
@Override
public AltAnnotation[] getAnnotationArrayValue(String methodName) {
getAnnotationValues();
return (AltAnnotation[]) values.get(methodName);
}
@Override
public AltClass getClassValue(String methodName) {
getAnnotationValues();
return (AltClass) values.get(methodName);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltAnnotation#getAnnotationValues()
*/
@Override
public Map<String, Object> getAnnotationValues() {
lock.lock();
try {
if (values != null) return values;
Map<? extends ExecutableElement, ? extends AnnotationValue> rawValues =
processingEnv.getElementUtils().getElementValuesWithDefaults(annotation);
Map<String, Object> retVal = new TreeMap<String, Object>();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : rawValues.entrySet()) {
ExecutableElement annoMethod = entry.getKey();
AnnotationValue annoValue = entry.getValue();
String key = Utilities.convertNameToString(annoMethod.getSimpleName());
Object value = annoValue.getValue();
if (value instanceof TypeMirror) {
// The annotation method is a java.lang.Class
value = Utilities.convertTypeMirror((TypeMirror) value, processingEnv);
}
else if (value instanceof VariableElement) {
// The annotation method is an Enum
VariableElement variable = (VariableElement) value;
TypeElement enclosing = (TypeElement) variable.getEnclosingElement();
String annoClassName = Utilities.convertNameToString(enclosing.getQualifiedName());
String annoVal = Utilities.convertNameToString(variable.getSimpleName());
value = new StringAltEnumImpl(annoClassName, annoVal);
}
else if (value instanceof AnnotationMirror) {
throw new AssertionError("The annotation " + annotation + " key " + key + " has unimplemented type AnnotationMirror");
}
else if (value instanceof List) {
// The annotation method returns an array of something
ArrayType returnType = (ArrayType) annoMethod.getReturnType();
TypeMirror arrayTypeMirror = returnType.getComponentType();
TypeKind arrayTypeKind = arrayTypeMirror.getKind();
@SuppressWarnings("unchecked")
List<? extends AnnotationValue> array = ((List<? extends AnnotationValue>) value);
if (TypeKind.INT.equals(arrayTypeMirror.getKind())) {
int[] iValue = new int[array.size()];
int lcv = 0;
for (AnnotationValue item : array) {
iValue[lcv++] = (Integer) item.getValue();
}
value = iValue;
}
else if (TypeKind.DECLARED.equals(arrayTypeMirror.getKind())) {
AltClass[] cValue = new AltClass[array.size()];
AltEnum[] eValue = new AltEnum[array.size()];
String[] sValue = new String[array.size()];
AltAnnotation[] aValue = new AltAnnotation[array.size()];
boolean isClass = true;
boolean isEnum = true;
boolean isAnnos = false;
int lcv = 0;
for (AnnotationValue item : array) {
Object itemValue = item.getValue();
if (itemValue instanceof TypeMirror) {
isClass = true;
isEnum = false;
isAnnos = false;
cValue[lcv++] = Utilities.convertTypeMirror((TypeMirror) itemValue, processingEnv);
}
else if (itemValue instanceof VariableElement) {
isClass = false;
isEnum = true;
isAnnos = false;
VariableElement variable = (VariableElement) itemValue;
TypeElement enclosing = (TypeElement) variable.getEnclosingElement();
String annoClassName = Utilities.convertNameToString(enclosing.getQualifiedName());
String annoVal = Utilities.convertNameToString(variable.getSimpleName());
eValue[lcv++] = new StringAltEnumImpl(annoClassName, annoVal);
}
else if (itemValue instanceof String) {
isClass = false;
isEnum = false;
isAnnos = false;
sValue[lcv++] = (String) itemValue;
}
else if (itemValue instanceof List) {
throw new AssertionError("Unimplemented declared List type in " + this);
}
else if (itemValue instanceof AnnotationMirror) {
isClass = false;
isEnum = false;
isAnnos = true;
aValue[lcv++] = new AnnotationMirrorAltAnnotationImpl((AnnotationMirror) itemValue, processingEnv);
}
else {
throw new AssertionError("Unknown declared type: " + itemValue.getClass().getName());
}
}
if (isClass) {
value = cValue;
}
else if (isEnum) {
value = eValue;
}
else if (isAnnos) {
value = aValue;
}
else {
value = sValue;
}
}
else if (TypeKind.LONG.equals(arrayTypeMirror.getKind())) {
long[] iValue = new long[array.size()];
int lcv = 0;
for (AnnotationValue item : array) {
iValue[lcv++] = (Long) item.getValue();
}
value = iValue;
}
else if (TypeKind.SHORT.equals(arrayTypeMirror.getKind())) {
short[] iValue = new short[array.size()];
int lcv = 0;
for (AnnotationValue item : array) {
iValue[lcv++] = (Short) item.getValue();
}
value = iValue;
}
else if (TypeKind.CHAR.equals(arrayTypeMirror.getKind())) {
char[] iValue = new char[array.size()];
int lcv = 0;
for (AnnotationValue item : array) {
iValue[lcv++] = (Character) item.getValue();
}
value = iValue;
}
else if (TypeKind.FLOAT.equals(arrayTypeMirror.getKind())) {
float[] iValue = new float[array.size()];
int lcv = 0;
for (AnnotationValue item : array) {
iValue[lcv++] = (Float) item.getValue();
}
value = iValue;
}
else if (TypeKind.DOUBLE.equals(arrayTypeMirror.getKind())) {
double[] iValue = new double[array.size()];
int lcv = 0;
for (AnnotationValue item : array) {
iValue[lcv++] = (Double) item.getValue();
}
value = iValue;
}
else if (TypeKind.BOOLEAN.equals(arrayTypeMirror.getKind())) {
boolean[] iValue = new boolean[array.size()];
int lcv = 0;
for (AnnotationValue item : array) {
iValue[lcv++] = (Boolean) item.getValue();
}
value = iValue;
}
else if (TypeKind.BYTE.equals(arrayTypeMirror.getKind())) {
byte[] iValue = new byte[array.size()];
int lcv = 0;
for (AnnotationValue item : array) {
iValue[lcv++] = (Byte) item.getValue();
}
value = iValue;
}
else {
throw new AssertionError("Array type " + arrayTypeKind + " is not implemented");
}
}
retVal.put(key, value);
}
values = Collections.unmodifiableMap(retVal);
return values;
} finally {
lock.unlock();
}
}
@Override
public String toString() {
return "AnnotationMirrorAltAnnotationImpl(" + annotationType() + ")";
}
}