1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.util.scan; |
11 | |
|
12 | |
import org.mule.util.scan.annotations.ClosableClassReader; |
13 | |
|
14 | |
import java.io.IOException; |
15 | |
import java.io.InputStream; |
16 | |
import java.net.URL; |
17 | |
|
18 | |
import org.objectweb.asm.ClassReader; |
19 | |
import org.objectweb.asm.commons.EmptyVisitor; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class InterfaceClassScanner extends EmptyVisitor implements ClassScanner |
25 | |
{ |
26 | |
private Class interfaceClass; |
27 | |
|
28 | |
private boolean match; |
29 | |
|
30 | |
private String className; |
31 | |
|
32 | |
private ClassLoader classLoader; |
33 | |
|
34 | |
public InterfaceClassScanner(Class interfaceClass) |
35 | |
{ |
36 | 0 | this(interfaceClass, Thread.currentThread().getContextClassLoader()); |
37 | 0 | } |
38 | |
|
39 | |
public InterfaceClassScanner(Class interfaceClass, ClassLoader classLoader) |
40 | 0 | { |
41 | 0 | if(!interfaceClass.isInterface()) |
42 | |
{ |
43 | 0 | throw new IllegalArgumentException("The class need to be an interface"); |
44 | |
} |
45 | 0 | this.interfaceClass = interfaceClass; |
46 | |
|
47 | 0 | this.classLoader = classLoader; |
48 | 0 | } |
49 | |
|
50 | |
public void visit(int i, int i1, String s, String s1, String superName, String[] interfaces) |
51 | |
{ |
52 | 0 | if (interfaces != null && interfaces.length > 0) |
53 | |
{ |
54 | 0 | for (int j = 0; j < interfaces.length; j++) |
55 | |
{ |
56 | 0 | String anInterface = interfaces[j].replace("/", "."); |
57 | 0 | if (interfaceClass.getName().equals(anInterface)) |
58 | |
{ |
59 | 0 | match = true; |
60 | 0 | className = s; |
61 | 0 | break; |
62 | |
} |
63 | |
else |
64 | |
{ |
65 | |
|
66 | 0 | ClassScanner scanner = scan(anInterface); |
67 | 0 | match = scanner.isMatch(); |
68 | 0 | className = s; |
69 | |
} |
70 | |
|
71 | |
} |
72 | |
} |
73 | |
|
74 | 0 | else if (superName == null) |
75 | |
{ |
76 | 0 | return; |
77 | |
} |
78 | |
else |
79 | |
{ |
80 | |
|
81 | 0 | ClassScanner scanner = scan(superName); |
82 | 0 | match = scanner.isMatch(); |
83 | 0 | className = scanner.getClassName(); |
84 | |
|
85 | 0 | if(match) |
86 | |
{ |
87 | 0 | className = s; |
88 | |
} |
89 | |
} |
90 | 0 | } |
91 | |
|
92 | |
protected ClassScanner scan(String name) |
93 | |
{ |
94 | |
try |
95 | |
{ |
96 | 0 | InterfaceClassScanner scanner = new InterfaceClassScanner(interfaceClass, classLoader); |
97 | 0 | URL classURL = getClassURL(name); |
98 | 0 | if(classURL==null) |
99 | |
{ |
100 | 0 | throw new RuntimeException("Failed to read class URL for name: " + name); |
101 | |
} |
102 | 0 | InputStream classStream = classURL.openStream(); |
103 | 0 | ClassReader r = new ClosableClassReader(classStream); |
104 | |
|
105 | 0 | r.accept(scanner, 0); |
106 | 0 | return scanner; |
107 | |
} |
108 | 0 | catch (IOException e) |
109 | |
{ |
110 | 0 | throw new RuntimeException(name, e); |
111 | |
} |
112 | |
} |
113 | |
|
114 | |
public boolean isMatch() |
115 | |
{ |
116 | 0 | return match; |
117 | |
} |
118 | |
|
119 | |
public String getClassName() |
120 | |
{ |
121 | 0 | return className; |
122 | |
} |
123 | |
|
124 | |
public URL getClassURL(String className) |
125 | |
{ |
126 | 0 | String resource = className.replace(".", "/") + ".class"; |
127 | 0 | return classLoader.getResource(resource); |
128 | |
} |
129 | |
} |