-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
WeldClassFileInfo.java
370 lines (322 loc) · 13.2 KB
/
WeldClassFileInfo.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.weld.discovery;
import org.jboss.as.server.deployment.annotation.CompositeIndex;
import org.jboss.as.weld.logging.WeldLogger;
import org.jboss.as.weld.util.Reflections;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;
import org.jboss.weld.resources.spi.ClassFileInfo;
import org.jboss.weld.util.cache.ComputingCache;
import javax.enterprise.inject.Vetoed;
import javax.inject.Inject;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Set;
/**
*
* @author Martin Kouba
*/
public class WeldClassFileInfo implements ClassFileInfo {
private static final DotName DOT_NAME_INJECT = DotName.createSimple(Inject.class.getName());
private static final DotName DOT_NAME_VETOED = DotName.createSimple(Vetoed.class.getName());
private static final DotName OBJECT_NAME = DotName.createSimple(Object.class.getName());
private static final String CONSTRUCTOR_METHOD_NAME = "<init>";
private static final String PACKAGE_INFO_NAME = "package-info";
private final ClassInfo classInfo;
private final CompositeIndex index;
private final boolean isVetoed;
private final boolean hasCdiConstructor;
private final ComputingCache<DotName, Set<String>> annotationClassAnnotationsCache;
private final ClassLoader classLoader;
/**
*
* @param className
* @param index
* @param annotationClassAnnotationsCache
*/
public WeldClassFileInfo(String className, CompositeIndex index, ComputingCache<DotName, Set<String>> annotationClassAnnotationsCache, ClassLoader classLoader) {
this.index = index;
this.annotationClassAnnotationsCache = annotationClassAnnotationsCache;
this.classInfo = index.getClassByName(DotName.createSimple(className));
if (this.classInfo == null) {
throw WeldLogger.ROOT_LOGGER.nameNotFoundInIndex(className);
}
this.isVetoed = isVetoedTypeOrPackage();
this.hasCdiConstructor = this.classInfo.hasNoArgsConstructor() || hasInjectConstructor();
this.classLoader = classLoader;
}
@Override
public String getClassName() {
return classInfo.name().toString();
}
@Override
public boolean isAnnotationDeclared(Class<? extends Annotation> annotation) {
return isAnnotationDeclared(classInfo, annotation);
}
@Override
public boolean containsAnnotation(Class<? extends Annotation> annotation) {
return containsAnnotation(classInfo, DotName.createSimple(annotation.getName()), annotation);
}
@Override
public int getModifiers() {
return classInfo.flags();
}
@Override
public boolean hasCdiConstructor() {
return hasCdiConstructor;
}
@Override
public boolean isAssignableFrom(Class<?> fromClass) {
return isAssignableFrom(getClassName(), fromClass);
}
@Override
public boolean isAssignableTo(Class<?> toClass) {
return isAssignableTo(classInfo.name(), toClass);
}
@Override
public boolean isVetoed() {
return isVetoed;
}
// @Override unused except in a test and removed in Weld 5 so don't annotate it
// TODO to be removed once Weld 5 is a direct dependency of non-preview WFLY
public boolean isTopLevelClass() {
return classInfo.nestingType() == ClassInfo.NestingType.TOP_LEVEL;
}
@Override
public ClassFileInfo.NestingType getNestingType() {
NestingType result = null;
switch (classInfo.nestingType()) {
case ANONYMOUS:
result = NestingType.NESTED_ANONYMOUS;
break;
case TOP_LEVEL:
result = NestingType.TOP_LEVEL;
break;
case LOCAL:
result = NestingType.NESTED_LOCAL;
break;
case INNER:
if (Modifier.isStatic(classInfo.flags())) {
result = NestingType.NESTED_STATIC;
} else {
result = NestingType.NESTED_INNER;
}
break;
default:
// should never happer
break;
}
return result;
}
@Override
public String getSuperclassName() {
return classInfo.superName().toString();
}
private boolean isVetoedTypeOrPackage() {
if (isAnnotationDeclared(classInfo, DOT_NAME_VETOED)) {
return true;
}
final DotName packageInfoName = DotName.createComponentized(getPackageName(classInfo.name()), PACKAGE_INFO_NAME);
ClassInfo packageInfo = index.getClassByName(packageInfoName);
if (packageInfo != null && isAnnotationDeclared(packageInfo, DOT_NAME_VETOED)) {
return true;
}
return false;
}
private boolean isAnnotationDeclared(ClassInfo classInfo, Class<? extends Annotation> annotation) {
return isAnnotationDeclared(classInfo, DotName.createSimple(annotation.getName()));
}
private boolean isAnnotationDeclared(ClassInfo classInfo, DotName requiredAnnotationName) {
List<AnnotationInstance> annotations = classInfo.annotationsMap().get(requiredAnnotationName);
if (annotations != null) {
for (AnnotationInstance annotationInstance : annotations) {
if (annotationInstance.target().equals(classInfo)) {
return true;
}
}
}
return false;
}
private boolean hasInjectConstructor() {
List<AnnotationInstance> annotationInstances = classInfo.annotationsMap().get(DOT_NAME_INJECT);
if (annotationInstances != null) {
for (AnnotationInstance instance : annotationInstances) {
AnnotationTarget target = instance.target();
if (target instanceof MethodInfo) {
MethodInfo methodInfo = (MethodInfo) target;
if (methodInfo.name().equals(CONSTRUCTOR_METHOD_NAME)) {
return true;
}
}
}
}
return false;
}
private DotName getPackageName(DotName name) {
if (name.isComponentized()) {
while (name.isInner()) {
name = name.prefix();
if (name == null) {
throw new IllegalStateException("Could not determine package from corrupted class name");
}
}
return name.prefix();
} else {
final int lastIndex = name.local().lastIndexOf(".");
if (lastIndex == -1) {
return name;
}
return DotName.createSimple(name.local().substring(0, name.local().lastIndexOf(".")));
}
}
/**
* @param className
* @param fromClass
* @return
*/
private boolean isAssignableFrom(String className, Class<?> fromClass) {
if (className.equals(fromClass.getName())) {
return true;
}
if (Object.class.equals(fromClass)) {
return false; // there's nothing assignable from Object.class except for Object.class
}
Class<?> superClass = fromClass.getSuperclass();
if (superClass != null && isAssignableFrom(className, superClass)) {
return true;
}
for (Class<?> interfaceClass : fromClass.getInterfaces()) {
if (isAssignableFrom(className, interfaceClass)) {
return true;
}
}
return false;
}
/**
* @param to
* @param name
* @return <code>true</code> if the name is equal to the fromName, or if the name represents a superclass or superinterface of the fromName,
* <code>false</code> otherwise
*/
private boolean isAssignableTo(DotName name, Class<?> to) {
if (to.getName().equals(name.toString())) {
return true;
}
if (OBJECT_NAME.equals(name)) {
return false; // there's nothing assignable from Object.class except for Object.class
}
ClassInfo fromClassInfo = index.getClassByName(name);
if (fromClassInfo == null) {
// We reached a class that is not in the index. Let's use reflection.
final Class<?> clazz = loadClass(name.toString());
return to.isAssignableFrom(clazz);
}
DotName superName = fromClassInfo.superName();
if (superName != null && isAssignableTo(superName, to)) {
return true;
}
if (fromClassInfo.interfaces() != null) {
for (DotName interfaceName : fromClassInfo.interfaces()) {
if (isAssignableTo(interfaceName, to)) {
return true;
}
}
}
return false;
}
private boolean containsAnnotation(ClassInfo classInfo, DotName requiredAnnotationName, Class<? extends Annotation> requiredAnnotation) {
// Type and members
if (classInfo.annotationsMap().containsKey(requiredAnnotationName)) {
return true;
}
// Meta-annotations
for (DotName annotation : classInfo.annotationsMap().keySet()) {
if (annotationClassAnnotationsCache.getValue(annotation).contains(requiredAnnotationName.toString())) {
return true;
}
}
// Superclass
final DotName superName = classInfo.superName();
if (superName != null && !OBJECT_NAME.equals(superName)) {
final ClassInfo superClassInfo = index.getClassByName(superName);
if (superClassInfo == null) {
// we are accessing a class that is outside of the jandex index
// fallback to using reflection
return Reflections.containsAnnotation(loadClass(superName.toString()), requiredAnnotation);
}
if (containsAnnotation(superClassInfo, requiredAnnotationName, requiredAnnotation)) {
return true;
}
}
// Also check default methods on interfaces
for (DotName interfaceName : classInfo.interfaceNames()) {
final ClassInfo interfaceInfo = index.getClassByName(interfaceName);
if (interfaceInfo == null) {
// we are accessing a class that is outside of the jandex index
// fallback to using reflection
Class<?> interfaceClass = loadClass(interfaceName.toString());
for (Method method : interfaceClass.getDeclaredMethods()) {
if (method.isDefault() && Reflections.containsAnnotations(method.getAnnotations(), requiredAnnotation)) {
return true;
}
}
continue;
}
for (MethodInfo method : interfaceInfo.methods()) {
// Default methods are public non-abstract instance methods declared in an interface
if (isNonAbstractPublicInstanceMethod(method)) {
if (method.hasAnnotation(requiredAnnotationName)) {
return true;
}
// Meta-annotations
for (AnnotationInstance annotation : method.annotations()) {
if (annotationClassAnnotationsCache.getValue(annotation.name()).contains(requiredAnnotationName.toString())) {
return true;
}
}
}
}
}
return false;
}
private boolean isNonAbstractPublicInstanceMethod(MethodInfo method) {
return (method.flags() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) == Modifier.PUBLIC;
}
private Class<?> loadClass(String className) {
WeldLogger.DEPLOYMENT_LOGGER.tracef("Falling back to reflection for %s", className);
try {
return classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
throw WeldLogger.ROOT_LOGGER.cannotLoadClass(className, e);
}
}
@Override
public String toString() {
return classInfo.toString();
}
}