59 Java reflective data (expeccially to methods, constructors and |
59 Java reflective data (expeccially to methods, constructors and |
60 fields) as required by Java reflection API. |
60 fields) as required by Java reflection API. |
61 |
61 |
62 Different classes may use different mirrors - for example, |
62 Different classes may use different mirrors - for example, |
63 Smalltalk classes use a special mirror so non-Smalltalk classes |
63 Smalltalk classes use a special mirror so non-Smalltalk classes |
64 and instances could be introspected and manipulated by standard |
64 and instances could be introspected and manipulated by standard |
65 Java code. |
65 Java code. |
66 |
66 |
67 [author:] |
67 [author:] |
68 Jan Vrany <jan.vrany@fit.cvut.cz> |
68 Jan Vrany <jan.vrany@fit.cvut.cz> |
69 |
69 |
121 |
121 |
122 "Created: / 31-07-2012 / 18:25:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
122 "Created: / 31-07-2012 / 18:25:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
123 ! |
123 ! |
124 |
124 |
125 getDeclaredConstructors: publicOnly |
125 getDeclaredConstructors: publicOnly |
126 "Returns an java.lang.reflect.Constructor[] with all constructors |
126 "Returns an java.lang.reflect.Constructor[] with all constructors |
127 declared by this class. " |
127 declared by this class. " |
128 |
128 |
129 ^self subclassResponsibility |
129 ^self subclassResponsibility |
130 |
130 |
131 "Created: / 31-07-2012 / 18:39:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
131 "Created: / 31-07-2012 / 18:39:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
132 ! |
132 ! |
133 |
133 |
134 getDeclaredFields: publicOnly |
134 getDeclaredFields: publicOnly |
135 "Returns an java.lang.reflect.Field[] with all constructors |
135 "Returns an java.lang.reflect.Field[] with all constructors |
136 declared by this class." |
136 declared by this class." |
137 |
137 |
138 ^self subclassResponsibility |
138 ^self subclassResponsibility |
139 |
139 |
140 "Created: / 22-08-2012 / 12:03:04 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
140 "Created: / 22-08-2012 / 12:03:04 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
141 ! |
141 ! |
142 |
142 |
143 getDeclaredMethods:publicOnly |
143 getDeclaredMethods:publicOnly |
144 "Returns an java.lang.reflect.Method[] with all methods |
144 "Returns an java.lang.reflect.Method[] with all methods |
145 declared by this class." |
145 declared by this class." |
146 |
146 |
147 ^ self subclassResponsibility |
147 ^ self subclassResponsibility |
148 |
148 |
149 "Created: / 01-08-2012 / 11:07:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
149 "Created: / 01-08-2012 / 11:07:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
150 ! |
150 ! |
151 |
151 |
195 "Created: / 31-07-2012 / 17:40:47 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
195 "Created: / 31-07-2012 / 17:40:47 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
196 ! ! |
196 ! ! |
197 |
197 |
198 !JavaMirror methodsFor:'instance creation-java.lang.reflect.*'! |
198 !JavaMirror methodsFor:'instance creation-java.lang.reflect.*'! |
199 |
199 |
200 create: ctorOrMethodClass for: class method: method signature: signature modifiers: modifiers parameterTyoes: parameterClasses exceptionTypes: exceptionClasses annotations: annotations |
200 create: ctorOrMethodClass for: class method: method signature: signature modifiers: modifiers parameterTyoes: parameterClasses exceptionTypes: exceptionClasses annotations: annotations |
201 "Creates a new java.lang.Constructor. Arguments: |
201 "Creates a new java.lang.Constructor. Arguments: |
202 |
202 |
203 ctorOrMethodClass <java.lang.reflect.Constructor|java.lang.reflect.Method> |
203 ctorOrMethodClass <java.lang.reflect.Constructor|java.lang.reflect.Method> |
204 class <Class> |
204 class <Class> |
205 method <Method> |
205 method <Method> |
206 signature <String> |
206 signature <String> |
207 modifiers <SmallInteger> |
207 modifiers <SmallInteger> |
208 parameterClasses <Collection of Class> |
208 parameterClasses <Collection of Class> |
209 exceptionClasses <Collection of Class> |
209 exceptionClasses <Collection of Class> |
210 annotations <JavaAnnotationContainer>" |
210 annotations <JavaAnnotationContainer>" |
211 |
211 |
212 | ctorOrMethod | |
212 | ctorOrMethod | |
213 ctorOrMethod := ctorOrMethodClass new. |
213 ctorOrMethod := ctorOrMethodClass new. |
214 ctorOrMethod |
214 ctorOrMethod |
215 instVarNamed: #clazz |
215 instVarNamed: #clazz |
216 put: (reflection "method m" javaClassObjectForClass: class); |
216 put: (reflection "method m" javaClassObjectForClass: class); |
244 exceptionClasses <Collection of Class> |
244 exceptionClasses <Collection of Class> |
245 annotations <JavaAnnotationContainer> |
245 annotations <JavaAnnotationContainer> |
246 " |
246 " |
247 |
247 |
248 | ctor | |
248 | ctor | |
249 ctor := self create: java_lang_reflect_Constructor |
249 ctor := self create: _java_lang_reflect_Constructor_CLASS |
250 for: class |
250 for: class |
251 method: method |
251 method: method |
252 signature: signature |
252 signature: signature |
253 modifiers: modifiers |
253 modifiers: modifiers |
254 parameterTyoes: parameterClasses |
254 parameterTyoes: parameterClasses |
255 exceptionTypes: exceptionClasses |
255 exceptionTypes: exceptionClasses |
256 annotations: annotations. |
256 annotations: annotations. |
257 ^ctor |
257 ^ctor |
258 |
258 |
259 "Created: / 01-08-2012 / 10:20:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
259 "Created: / 01-08-2012 / 10:20:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
260 "Modified: / 18-05-2013 / 10:55:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
260 "Modified: / 18-05-2013 / 10:55:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
261 ! |
261 ! |
262 |
262 |
263 createFieldFor: javaField |
263 createFieldFor: javaField |
264 "given a java field, return the corresponding java.lang.Field |
264 "given a java field, return the corresponding java.lang.Field |
265 instance for it." |
265 instance for it." |
266 " |
266 " |
267 See OpenJDK7 source: |
267 See OpenJDK7 source: |
268 jdk7/hotspot/src/share/vm/runtime/reflection.cpp, |
268 jdk7/hotspot/src/share/vm/runtime/reflection.cpp, |
280 "/ |
280 "/ |
281 "/ however, for performance reasons its better to avoid ClassLoader query, so: |
281 "/ however, for performance reasons its better to avoid ClassLoader query, so: |
282 type := JavaVM javaClassObjectForClass: |
282 type := JavaVM javaClassObjectForClass: |
283 ((JavaDescriptor fromString: javaField descriptor) javaClassUsingClassLoader: javaField javaClass classLoader). |
283 ((JavaDescriptor fromString: javaField descriptor) javaClassUsingClassLoader: javaField javaClass classLoader). |
284 modifiers := javaField accessFlags. |
284 modifiers := javaField accessFlags. |
285 field := java_lang_reflect_Field new. |
285 field := _java_lang_reflect_Field_CLASS new. |
286 field |
286 field |
287 instVarNamed: #clazz put: clazz; |
287 instVarNamed: #clazz put: clazz; |
288 instVarNamed: #name put: name; |
288 instVarNamed: #name put: name; |
289 instVarNamed: #slot put: slot; |
289 instVarNamed: #slot put: slot; |
290 instVarNamed: #type put: type; |
290 instVarNamed: #type put: type; |
291 instVarNamed: #modifiers put: modifiers; |
291 instVarNamed: #modifiers put: modifiers; |
292 yourself. |
292 yourself. |
293 javaField annotations |
293 javaField annotations |
294 ifNotNil: [ |
294 ifNotNil: [ |
295 field instVarNamed: #annotations |
295 field instVarNamed: #annotations |
296 put: javaField annotations runtimeVisible rawAnnotations |
296 put: javaField annotations runtimeVisible rawAnnotations |
297 ]. |
297 ]. |
298 ^ field. |
298 ^ field. |
314 exceptionClasses <Collection of Class> |
314 exceptionClasses <Collection of Class> |
315 annotations <JavaAnnotationContainer> |
315 annotations <JavaAnnotationContainer> |
316 " |
316 " |
317 |
317 |
318 | mthd | |
318 | mthd | |
319 mthd := self create: java_lang_reflect_Method |
319 mthd := self create: _java_lang_reflect_Method_CLASS |
320 for: class |
320 for: class |
321 method: method |
321 method: method |
322 signature: signature |
322 signature: signature |
323 modifiers: modifiers |
323 modifiers: modifiers |
324 parameterTyoes: parameterClasses |
324 parameterTyoes: parameterClasses |
325 exceptionTypes: exceptionClasses |
325 exceptionTypes: exceptionClasses |
326 annotations: annotations. |
326 annotations: annotations. |
327 |
327 |
328 mthd |
328 mthd |
329 instVarNamed: #name put: (reflection javaStringObjectForString: name interned: true); |
329 instVarNamed: #name put: (reflection javaStringObjectForString: name interned: true); |
330 instVarNamed: #returnType put: (reflection javaClassObjectForClass: returnClass); |
330 instVarNamed: #returnType put: (reflection javaClassObjectForClass: returnClass); |