1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.util.scan.annotations; |
11 | |
|
12 | |
import java.io.IOException; |
13 | |
import java.io.InputStream; |
14 | |
import java.lang.annotation.Annotation; |
15 | |
import java.net.URL; |
16 | |
|
17 | |
import org.apache.commons.logging.Log; |
18 | |
import org.apache.commons.logging.LogFactory; |
19 | |
import org.objectweb.asm.AnnotationVisitor; |
20 | |
import org.objectweb.asm.ClassReader; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class MetaAnnotationTypeFilter implements AnnotationFilter |
27 | |
{ |
28 | |
|
29 | |
|
30 | |
|
31 | 0 | protected transient final Log logger = LogFactory.getLog(MetaAnnotationTypeFilter.class); |
32 | |
|
33 | |
private Class<? extends Annotation> annotation; |
34 | |
|
35 | |
private ClassLoader classLoader; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public MetaAnnotationTypeFilter(Class<? extends Annotation> annotation, ClassLoader classLoader) |
46 | 0 | { |
47 | 0 | this.annotation = annotation; |
48 | 0 | this.classLoader = classLoader; |
49 | 0 | } |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public MetaAnnotationTypeFilter(Class<? extends Annotation> annotation) |
57 | 0 | { |
58 | 0 | this.annotation = annotation; |
59 | 0 | classLoader = Thread.currentThread().getContextClassLoader(); |
60 | 0 | } |
61 | |
|
62 | |
public boolean accept(AnnotationInfo info) |
63 | |
{ |
64 | |
try |
65 | |
{ |
66 | 0 | URL classUrl = getClassURL(info.getClassName()); |
67 | 0 | if (classUrl == null) |
68 | |
{ |
69 | 0 | logger.debug("Failed to load annotation class: " + info); |
70 | 0 | return false; |
71 | |
} |
72 | |
|
73 | 0 | InputStream classStream = classUrl.openStream(); |
74 | 0 | ClassReader r = new ClosableClassReader(classStream); |
75 | |
|
76 | 0 | MetaAnnotationScanner scanner = new MetaAnnotationScanner(new AnnotationTypeFilter(annotation)); |
77 | 0 | r.accept(scanner, 0); |
78 | |
|
79 | 0 | return scanner.getClassAnnotations().size() == 1; |
80 | |
} |
81 | 0 | catch (IOException e) |
82 | |
{ |
83 | 0 | logger.debug(e); |
84 | 0 | return false; |
85 | |
} |
86 | |
} |
87 | |
|
88 | |
private class MetaAnnotationScanner extends AnnotationsScanner |
89 | |
{ |
90 | |
public MetaAnnotationScanner(AnnotationFilter filter) |
91 | 0 | { |
92 | 0 | super(filter); |
93 | 0 | } |
94 | |
|
95 | |
@Override |
96 | |
public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) |
97 | |
{ |
98 | 0 | currentAnnotation = new AnnotationInfo(); |
99 | 0 | currentAnnotation.setClassName(getAnnotationClassName(desc)); |
100 | 0 | if (log.isDebugEnabled()) |
101 | |
{ |
102 | 0 | log.debug("Annotation: " + getAnnotationClassName(desc)); |
103 | |
} |
104 | |
|
105 | |
|
106 | 0 | if (currentlyProcessing.nextSetBit(0) < 0) |
107 | |
{ |
108 | |
|
109 | 0 | currentlyProcessing.set(PROCESSING_CLASS); |
110 | |
} |
111 | |
|
112 | 0 | return this; |
113 | |
} |
114 | |
|
115 | |
} |
116 | |
|
117 | |
public URL getClassURL(String className) |
118 | |
{ |
119 | 0 | String resource = className.replace(".", "/") + ".class"; |
120 | 0 | return classLoader.getResource(resource); |
121 | |
} |
122 | |
} |