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 | |
package javafx.reflect; |
27 | |
import java.util.*; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class LocalReflectionContext extends ReflectionContext { |
37 | 1 | static LocalReflectionContext instance = new LocalReflectionContext(); |
38 | |
|
39 | 1 | private LocalReflectionContext () { |
40 | 1 | } |
41 | |
|
42 | |
|
43 | 1 | public static LocalReflectionContext getInstance() { return instance; } |
44 | |
|
45 | |
|
46 | |
public ObjectRef makeObjectRef(Object obj) { |
47 | 1 | return new LocalObjectRef(obj, this); |
48 | |
|
49 | |
|
50 | |
} |
51 | |
|
52 | |
|
53 | |
public ClassRef findClass(String cname) { |
54 | |
ClassLoader loader; |
55 | |
try { |
56 | 3 | loader = Thread.currentThread().getContextClassLoader(); |
57 | |
} |
58 | 0 | catch (java.lang.SecurityException ex) { |
59 | 0 | loader = getClass().getClassLoader(); |
60 | 3 | } |
61 | 3 | return findClass(cname, loader); |
62 | |
} |
63 | |
|
64 | |
|
65 | |
public ClassRef findClass(String cname, ClassLoader loader) { |
66 | 3 | String n = cname; |
67 | 3 | Exception ex0 = null; |
68 | |
for (;;) { |
69 | |
try { |
70 | 4 | Class cl = Class.forName(n, false, loader); |
71 | |
|
72 | 3 | return makeClassRef(cl); |
73 | 1 | } catch (Exception ex) { |
74 | 1 | if (ex0 == null) |
75 | 1 | ex0 = ex; |
76 | 1 | int dot = n.lastIndexOf('.'); |
77 | 1 | if (dot < 0) |
78 | 0 | break; |
79 | 1 | n = n.substring(0, dot)+'$'+n.substring(dot+1); |
80 | 1 | } |
81 | |
} |
82 | 0 | throw new RuntimeException(ex0); |
83 | |
} |
84 | |
|
85 | |
|
86 | |
public ClassRef makeClassRef(Class cls) { |
87 | 22 | int modifiers = 0; |
88 | |
try { |
89 | 22 | String cname = cls.getName(); |
90 | 22 | Class clsInterface = null; |
91 | 22 | if (cname.endsWith(INTERFACE_SUFFIX)) { |
92 | 12 | clsInterface = cls; |
93 | 12 | cname = cname.substring(0, cname.length()-INTERFACE_SUFFIX.length()); |
94 | 12 | cls = Class.forName(cname, false, cls.getClassLoader()); |
95 | 12 | modifiers = ClassRef.COMPOUND_CLASS|ClassRef.FX_CLASS; |
96 | 12 | return new LocalClassRef(this, modifiers, cls, clsInterface); |
97 | |
} |
98 | 10 | Class[] interfaces = cls.getInterfaces(); |
99 | 10 | String intfName = cname + INTERFACE_SUFFIX; |
100 | 17 | for (int i = 0; i < interfaces.length; i++ ) { |
101 | 7 | String iname = interfaces[i].getName(); |
102 | 7 | if (iname.equals(FXOBJECT_NAME)) |
103 | 2 | modifiers |= ClassRef.FX_CLASS; |
104 | 5 | else if (iname.equals(intfName)) { |
105 | 1 | clsInterface = interfaces[i]; |
106 | 1 | modifiers |= ClassRef.COMPOUND_CLASS; |
107 | |
} |
108 | |
} |
109 | 10 | return new LocalClassRef(this, modifiers, cls, clsInterface); |
110 | |
} |
111 | 0 | catch (Exception ex) { |
112 | 0 | throw new RuntimeException(ex); |
113 | |
} |
114 | |
} |
115 | |
|
116 | |
public SequenceBuilder makeSequenceBuilder (TypeRef elementType) { |
117 | 0 | return new SequenceBuilder() { |
118 | 0 | public void append(ValueRef value) { throw new Error(); } |
119 | |
|
120 | 0 | public ValueRef getSequence() { throw new Error(); } |
121 | |
}; |
122 | |
} |
123 | |
|
124 | 0 | public TypeRef getIntegerType() { throw new Error(); } |
125 | |
|
126 | 0 | public TypeRef getNumberType() { throw new Error(); } |
127 | |
} |
128 | |
|
129 | 0 | class LocalClassRef extends ClassRef { |
130 | |
Class refClass; |
131 | |
Class refInterface; |
132 | |
|
133 | |
public LocalClassRef(LocalReflectionContext context, int modifiers, |
134 | |
Class refClass, Class refInterface) { |
135 | 22 | super(context, modifiers); |
136 | 22 | this.refClass = refClass; |
137 | 22 | this.refInterface = refInterface; |
138 | 22 | this.name = refClass.getCanonicalName(); |
139 | 22 | } |
140 | |
|
141 | |
@Override |
142 | |
public LocalReflectionContext getReflectionContect() { |
143 | 18 | return (LocalReflectionContext) super.getReflectionContect(); |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public int hashCode() { |
150 | 0 | return name.hashCode(); |
151 | |
} |
152 | |
|
153 | |
public boolean equals (Object obj) { |
154 | 0 | return obj instanceof LocalClassRef |
155 | |
&& refClass == ((LocalClassRef) obj).refClass; |
156 | |
} |
157 | |
|
158 | 23 | static class SortedClassArray extends AbstractList<ClassRef> { |
159 | 4 | LocalClassRef[] buffer = new LocalClassRef[4]; |
160 | |
int sz; |
161 | |
public ClassRef get(int index) { |
162 | 19 | if (index >= sz) |
163 | 0 | throw new IndexOutOfBoundsException(); |
164 | 19 | return buffer[index]; |
165 | |
} |
166 | 23 | public int size() { return sz; } |
167 | |
|
168 | |
boolean insert(LocalClassRef cl) { |
169 | 20 | String clname = cl.getName(); |
170 | |
|
171 | |
|
172 | 20 | int i = 0; |
173 | 108 | for (; i < sz; i++) { |
174 | 56 | LocalClassRef c = buffer[i]; |
175 | 56 | int cmp = c.getName().compareTo(clname); |
176 | 56 | if (cmp > 0) |
177 | 11 | break; |
178 | 45 | if (cmp == 0) { |
179 | 1 | if (c.refClass == cl.refClass) |
180 | 1 | return false; |
181 | |
|
182 | |
break; |
183 | |
} |
184 | |
} |
185 | 19 | if (sz == buffer.length) { |
186 | 3 | LocalClassRef[] tmp = new LocalClassRef[2*sz]; |
187 | 3 | System.arraycopy(buffer, 0, tmp, 0, sz); |
188 | 3 | buffer = tmp; |
189 | |
} |
190 | 19 | System.arraycopy(buffer, i, buffer, i+1, sz-i); |
191 | 19 | buffer[i] = cl; |
192 | 19 | sz++; |
193 | 19 | return true; |
194 | |
} |
195 | |
} |
196 | |
|
197 | |
void getSuperClasses(boolean all, SortedClassArray result) { |
198 | 18 | boolean isCompound = isCompoundClass(); |
199 | 18 | Class cls = isCompound ? refInterface : refClass; |
200 | 18 | Class[] interfaces = cls.getInterfaces(); |
201 | 18 | LocalReflectionContext context = getReflectionContect(); |
202 | 18 | if (! isCompound) { |
203 | 8 | Class s = cls.getSuperclass(); |
204 | 8 | if (s != null) { |
205 | 3 | LocalClassRef cl = (LocalClassRef) context.makeClassRef(s); |
206 | 3 | if (result.insert(cl) && all) |
207 | 2 | cl.getSuperClasses(all, result); |
208 | |
} |
209 | |
} |
210 | 45 | for (int i = 0; i < interfaces.length; i++) { |
211 | 27 | Class iface = interfaces[i]; |
212 | 27 | if (iface.getName().equals(LocalReflectionContext.FXOBJECT_NAME)) |
213 | 12 | continue; |
214 | 15 | LocalClassRef cl = (LocalClassRef) context.makeClassRef(iface); |
215 | 15 | if (result.insert(cl) && all) |
216 | 12 | cl.getSuperClasses(all, result); |
217 | |
} |
218 | 18 | } |
219 | |
|
220 | |
public List<ClassRef> getSuperClasses(boolean all) { |
221 | 4 | SortedClassArray result = new SortedClassArray(); |
222 | 4 | if (all) |
223 | 2 | result.insert(this); |
224 | 4 | getSuperClasses(all, result); |
225 | 4 | return result; |
226 | |
} |
227 | |
|
228 | 0 | public MemberRef getMember(String name, TypeRef type) { throw new Error(); } |
229 | 0 | public AttributeRef getAttribute(String name) { throw new Error(); } |
230 | 0 | public MethodRef getMethod(String name, TypeRef... argType) { throw new Error(); } |
231 | 0 | public ObjectRef allocate () { throw new Error(); } |
232 | |
|
233 | 0 | public void getMembers(MemberHandler handler, boolean all) { throw new Error(); } |
234 | |
|
235 | |
|
236 | |
} |
237 | |
|
238 | 0 | class LocalObjectRef extends ObjectRef { |
239 | |
Object obj; |
240 | |
ClassRef type; |
241 | |
|
242 | 1 | public LocalObjectRef(Object obj, LocalReflectionContext context) { |
243 | 1 | type = context.makeClassRef(obj.getClass()); |
244 | 1 | this.obj = obj; |
245 | 1 | } |
246 | |
|
247 | |
public ClassRef getType() { |
248 | 1 | return type; |
249 | |
} |
250 | |
|
251 | |
public boolean isNull() { |
252 | 0 | return obj == null; |
253 | |
} |
254 | |
} |