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