1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package org.apache.commons.classscan.bcel; |
15 | |
|
16 | |
import java.util.Set; |
17 | |
|
18 | |
import org.apache.bcel.classfile.Method; |
19 | |
import org.apache.bcel.classfile.ParameterAnnotationEntry; |
20 | |
import org.apache.bcel.generic.Type; |
21 | |
import org.apache.commons.classscan.model.MetaAnnotation; |
22 | |
import org.apache.commons.classscan.model.MetaParameter; |
23 | |
import org.apache.commons.classscan.model.MetaType; |
24 | |
import org.apache.commons.classscan.spi.model.SpiMetaClassLoader; |
25 | |
import org.apache.commons.classscan.spi.model.SpiMetaMethod; |
26 | |
import org.apache.commons.classscan.spi.model.SpiMetaParameter; |
27 | |
import org.apache.commons.classscan.util.ResolveSet; |
28 | |
|
29 | |
public class BcelMethod implements SpiMetaMethod { |
30 | |
|
31 | |
private final String methodName; |
32 | |
private final boolean isStatic; |
33 | |
private final AnnotationMap annotations; |
34 | |
private MetaType returnType; |
35 | |
private String returnTypeDescriptor; |
36 | |
private final ResolveSet<SpiMetaParameter> paramTypes; |
37 | |
|
38 | 1960376 | public BcelMethod(Method method) { |
39 | 1960376 | methodName = method.getName(); |
40 | 1960376 | isStatic = method.isStatic(); |
41 | 1960376 | annotations = AnnotationMap.createAnnotations(method.getAnnotationEntries()); |
42 | 1960376 | returnTypeDescriptor = method.getReturnType().getSignature(); |
43 | 1960376 | paramTypes = createParameters(method); |
44 | 1960376 | } |
45 | |
|
46 | |
@Override |
47 | |
public boolean resolve(SpiMetaClassLoader classLoader) { |
48 | 1957681 | returnType = classLoader.resolveTypeForDescriptor(returnTypeDescriptor); |
49 | 1957681 | if(returnType==null) { |
50 | 44 | return false; |
51 | |
} |
52 | 1957637 | returnTypeDescriptor = null; |
53 | |
|
54 | 1957637 | return annotations.resolve(classLoader) |
55 | |
&& paramTypes.resolve(classLoader); |
56 | |
} |
57 | |
|
58 | |
@Override |
59 | |
public String getName() { |
60 | 12146552 | return methodName; |
61 | |
} |
62 | |
|
63 | |
@Override |
64 | |
public Set<? extends MetaAnnotation> getAnnotations() { |
65 | 33 | return annotations; |
66 | |
} |
67 | |
|
68 | |
@Override |
69 | |
public MetaAnnotation getAnnotation(String annotationName) { |
70 | 0 | return annotations.getValue(annotationName); |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public boolean isStatic() { |
75 | 22 | return isStatic; |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
public MetaType getType() { |
80 | 22 | return returnType; |
81 | |
} |
82 | |
|
83 | |
@Override |
84 | |
public Set<? extends MetaParameter> getParameters() { |
85 | 11 | return paramTypes; |
86 | |
} |
87 | |
|
88 | |
private ResolveSet<SpiMetaParameter> createParameters(Method method) { |
89 | 1960376 | final Type[] types = method.getArgumentTypes(); |
90 | 1960376 | if (types.length == 0) { |
91 | 780109 | return ResolveSet.emptyResolveSet(); |
92 | |
} |
93 | |
|
94 | 1180267 | final ParameterAnnotationEntry[] parameterAnnotations = method.getParameterAnnotationEntries(); |
95 | 1180267 | SpiMetaParameter[] metaParameterArray = new SpiMetaParameter[types.length]; |
96 | 3279133 | for (int i = 0; i < metaParameterArray.length; ++i) { |
97 | |
|
98 | 2098866 | ParameterAnnotationEntry arg = i < parameterAnnotations.length ? parameterAnnotations[i] : null; |
99 | 2098866 | metaParameterArray[i] = new BcelParameter(types[i], arg); |
100 | |
} |
101 | |
|
102 | 1180267 | ResolveSet<SpiMetaParameter> parameters = new ResolveSet<SpiMetaParameter>(metaParameterArray); |
103 | 1180267 | return parameters; |
104 | |
} |
105 | |
} |