-
Notifications
You must be signed in to change notification settings - Fork 53
/
TypeElementAltClassImpl.java
executable file
·321 lines (249 loc) · 11.8 KB
/
TypeElementAltClassImpl.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
/*
* 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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeMirror;
import org.glassfish.hk2.utilities.general.GeneralUtilities;
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.AltMethod;
import org.glassfish.hk2.xml.internal.alt.clazz.ClassAltClassImpl;
/**
* @author jwells
*
*/
public class TypeElementAltClassImpl implements AltClass {
private final ReentrantLock lock = new ReentrantLock();
private final TypeElement clazz;
private final ProcessingEnvironment processingEnv;
private List<AltMethod> methods;
private List<AltAnnotation> annotations;
public TypeElementAltClassImpl(TypeElement clazz, ProcessingEnvironment processingEnv) {
this.clazz = clazz;
this.processingEnv = processingEnv;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltClass#getName()
*/
@Override
public String getName() {
return Utilities.convertNameToString(processingEnv.getElementUtils().getBinaryName(clazz));
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltClass#getSimpleName()
*/
@Override
public String getSimpleName() {
return Utilities.convertNameToString(clazz.getSimpleName());
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltClass#getAnnotations()
*/
@Override
public List<AltAnnotation> getAnnotations() {
lock.lock();
try {
if (annotations != null) return annotations;
List<? extends AnnotationMirror> annoMirrors = processingEnv.getElementUtils().getAllAnnotationMirrors(clazz);
ArrayList<AltAnnotation> retVal = new ArrayList<AltAnnotation>(annoMirrors.size());
for (AnnotationMirror annoMirror : annoMirrors) {
AnnotationMirrorAltAnnotationImpl anno = new AnnotationMirrorAltAnnotationImpl(annoMirror, processingEnv);
retVal.add(anno);
}
annotations = Collections.unmodifiableList(new ArrayList<AltAnnotation>(retVal));
return annotations;
} finally {
lock.unlock();
}
}
private static final Set<String> POSSIBLE_NO_HANDLE = new HashSet<String>(Arrays.asList(new String[] {
"getClass",
"hashCode",
"equals",
"toString",
"notify",
"notifyAll",
"wait"
}));
private boolean isMethodToGenerate(Element element) {
if (!ElementKind.METHOD.equals(element.getKind())) return false;
ExecutableElement executable = (ExecutableElement) element;
String methodName = executable.getSimpleName().toString();
if (!POSSIBLE_NO_HANDLE.contains(methodName)) return true;
List<? extends TypeMirror> parameters = ((ExecutableType) executable.asType()).getParameterTypes();
if ("getClass".equals(methodName) ||
"hashCode".equals(methodName) ||
"toString".equals(methodName) ||
"notify".equals(methodName) ||
"notifyAll".equals(methodName) ||
"wait".equals(methodName)) {
// If any of the above have zero arguments they should not be handled
if (parameters.size() == 0) return false;
}
if ("equals".equals(methodName) && (parameters.size() == 1)) {
// If input is an Object.class...
TypeMirror param0 = parameters.get(0);
AltClass ac = Utilities.convertTypeMirror(param0, processingEnv);
if (Object.class.getName().equals(ac.getName())) return false;
}
if ("wait".equals(methodName) && (parameters.size() == 1)) {
// If input is a long
TypeMirror param0 = parameters.get(0);
AltClass ac = Utilities.convertTypeMirror(param0, processingEnv);
if (ClassAltClassImpl.LONG.equals(ac)) return false;
}
if ("wait".equals(methodName) && (parameters.size() == 2)) {
// If input is a long and an int
TypeMirror param0 = parameters.get(0);
TypeMirror param1 = parameters.get(1);
AltClass ac0 = Utilities.convertTypeMirror(param0, processingEnv);
AltClass ac1 = Utilities.convertTypeMirror(param1, processingEnv);
if (ClassAltClassImpl.LONG.equals(ac0) && ClassAltClassImpl.INT.equals(ac1)) return false;
}
return true;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltClass#getMethods()
*/
@Override
public List<AltMethod> getMethods() {
lock.lock();
try {
if (methods != null) return methods;
List<? extends Element> innerElements = processingEnv.getElementUtils().getAllMembers(clazz);
TreeMap<String, List<Element>> reorderByEnclosingClass = new TreeMap<String, List<Element>>();
String clazzName = getName();
for (Element innerElementElement : innerElements) {
if (isMethodToGenerate(innerElementElement)) {
TypeElement enclosingElement = (TypeElement) innerElementElement.getEnclosingElement();
String enclosingName = Utilities.convertNameToString(processingEnv.getElementUtils().getBinaryName(enclosingElement));
List<Element> addedList = reorderByEnclosingClass.get(enclosingName);
if (addedList == null) {
addedList = new LinkedList<Element>();
reorderByEnclosingClass.put(enclosingName, addedList);
}
addedList.add(innerElementElement);
}
}
List<Element> innerElementsReordered = new ArrayList<Element>(innerElements.size());
for (Map.Entry<String, List<Element>> listByEnclosing : reorderByEnclosingClass.entrySet()) {
String enclosingClass = listByEnclosing.getKey();
if (clazzName.equals(enclosingClass)) continue;
innerElementsReordered.addAll(listByEnclosing.getValue());
}
List<Element> topClass = reorderByEnclosingClass.get(clazzName);
if (topClass != null) {
innerElementsReordered.addAll(topClass);
}
ArrayList<AltMethod> retVal = new ArrayList<AltMethod>(innerElementsReordered.size());
for (Element innerElementElement : innerElementsReordered) {
retVal.add(new ElementAltMethodImpl(innerElementElement, processingEnv));
}
methods = Collections.unmodifiableList(retVal);
return methods;
} finally {
lock.unlock();
}
}
@Override
public AltClass getSuperParameterizedType(AltClass superclass,
int paramIndex) {
if (paramIndex < 0) return null;
String stopName = superclass.getName();
TypeElement currentClass = clazz;
while (currentClass != null) {
String currentName = Utilities.convertNameToString(processingEnv.getElementUtils().getBinaryName(currentClass));
TypeMirror superMirror = currentClass.getSuperclass();
if (superMirror == null) return null;
if (!(superMirror instanceof DeclaredType)) return null;
DeclaredType superDeclared = (DeclaredType) superMirror;
TypeElement nextClass = (TypeElement) superDeclared.asElement();
String superName = Utilities.convertNameToString(processingEnv.getElementUtils().getBinaryName(nextClass));
if (GeneralUtilities.safeEquals(superName, stopName)) {
List<? extends TypeMirror> genericParams = superDeclared.getTypeArguments();
if (genericParams == null || genericParams.isEmpty()) {
throw new IllegalStateException("Class " + currentName + " which is a superclass of " + stopName +
" does is not a parameterized type");
}
if (paramIndex >= genericParams.size()) {
throw new IllegalStateException("Class " + currentName + " which is a superclass of " + stopName +
" does not have " + paramIndex + " types. It only has " + genericParams.size());
}
TypeMirror tpe = genericParams.get(paramIndex);
if (!(tpe instanceof DeclaredType)) return null;
DeclaredType retValDecl = (DeclaredType) tpe;
TypeElement retValElement = (TypeElement) retValDecl.asElement();
return new TypeElementAltClassImpl(retValElement, processingEnv);
}
currentClass = nextClass;
}
return null;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltClass#isInterface()
*/
@Override
public boolean isInterface() {
return ElementKind.INTERFACE.equals(clazz.getKind());
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltClass#isArray()
*/
@Override
public boolean isArray() {
return false;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltClass#getComponentType()
*/
@Override
public AltClass getComponentType() {
return null;
}
@Override
public int hashCode() {
return getName().hashCode();
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof AltClass)) return false;
AltClass oac = (AltClass) o;
return GeneralUtilities.safeEquals(oac.getName(), getName());
}
@Override
public String toString() {
return "TypeElementAltClassImpl(" + clazz.getQualifiedName() + "," + System.identityHashCode(this) + ")";
}
}