JavaMirror.st
branchdevelopment
changeset 2965 bac7022ca26a
parent 2731 13f5be2bf83b
parent 2625 a91a1db9718e
child 2966 afd174546057
--- a/JavaMirror.st	Wed Dec 18 00:02:10 2013 +0100
+++ b/JavaMirror.st	Wed Dec 18 12:03:32 2013 +0100
@@ -23,38 +23,10 @@
 Object subclass:#JavaMirror
 	instanceVariableNames:'klass reflection'
 	classVariableNames:''
-	poolDictionaries:''
+	poolDictionaries:'JavaVMData'
 	category:'Languages-Java-Classes'
 !
 
-JavaMirror subclass:#AlienClassMirror
-	instanceVariableNames:''
-	classVariableNames:''
-	poolDictionaries:''
-	privateIn:JavaMirror
-!
-
-JavaMirror subclass:#JavaArrayMirror
-	instanceVariableNames:''
-	classVariableNames:''
-	poolDictionaries:''
-	privateIn:JavaMirror
-!
-
-JavaMirror subclass:#JavaClassMirror
-	instanceVariableNames:''
-	classVariableNames:''
-	poolDictionaries:''
-	privateIn:JavaMirror
-!
-
-JavaMirror subclass:#JavaPrimitiveMirror
-	instanceVariableNames:''
-	classVariableNames:''
-	poolDictionaries:''
-	privateIn:JavaMirror
-!
-
 !JavaMirror class methodsFor:'documentation'!
 
 copyright
@@ -117,9 +89,10 @@
 !JavaMirror class methodsFor:'accessing'!
 
 mirrorClassForAlienClass
-    ^ AlienClassMirror
+    ^ JavaAlienMirror
 
     "Created: / 31-07-2012 / 17:36:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 24-05-2013 / 10:54:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 mirrorClassForJavaArray
@@ -273,7 +246,7 @@
     "
 
     | ctor |
-    ctor := self create: (JavaVM classForName:'java.lang.reflect.Constructor')
+    ctor := self create: java_lang_reflect_Constructor
                     for: class
                  method: method
               signature: signature 
@@ -284,6 +257,7 @@
     ^ctor
 
     "Created: / 01-08-2012 / 10:20:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 18-05-2013 / 10:55:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 createFieldFor: javaField 
@@ -300,9 +274,15 @@
     name := JavaVM reflection javaStringObjectForString: javaField name
                 interned: true.
     slot := javaField index.
-    type := JavaVM javaClassObjectForClass: javaField typeClass.
+    "/ Following could be coded like (and actually it was):
+    "/
+    "/type := JavaVM javaClassObjectForClass: javaField typeClass.
+    "/
+    "/ however, for performance reasons its better to avoid ClassLoader query, so:
+    type := JavaVM javaClassObjectForClass:
+                ((JavaDescriptor fromString: javaField descriptor)  javaClassUsingClassLoader: javaField javaClass classLoader).
     modifiers := javaField accessFlags.
-    field := (JavaVM classForName: 'java.lang.reflect.Field') new.
+    field := java_lang_reflect_Field new.
     field
         instVarNamed: #clazz put: clazz;
         instVarNamed: #name put: name;
@@ -318,6 +298,7 @@
     ^ field.
 
     "Created: / 22-08-2012 / 12:09:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 18-05-2013 / 11:51:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 createMethodFor: class method: method name: name signature: signature modifiers: modifiers parameterTyoes: parameterClasses returnType: returnClass exceptionTypes: exceptionClasses annotations: annotations
@@ -335,7 +316,7 @@
     "
 
     | mthd |
-    mthd := self create: (JavaVM classForName:'java.lang.reflect.Method')
+    mthd := self create: java_lang_reflect_Method
                     for: class
                  method: method
               signature: signature 
@@ -352,484 +333,7 @@
     ^mthd
 
     "Created: / 01-08-2012 / 10:46:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaMirror::AlienClassMirror class methodsFor:'documentation'!
-
-documentation
-"
-    Specialized mirror for Smalltalk (and all other 
-    non-Java classes)
-
-    [author:]
-        Jan Vrany <jan.vrany@fit.cvut.cz>
-
-    [instance variables:]
-
-    [class variables:]
-
-    [see also:]
-
-"
-! !
-
-!JavaMirror::AlienClassMirror methodsFor:'accessing'!
-
-getClassLoader
-    "Returns a class loader that loaded this class"
-
-    ^ (JavaVM classForName:'stx.libjava.ClassLoader') instVarNamed: #scl.
-
-    "Modified: / 07-05-2013 / 11:19:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredConstructors:publicOnly
-    "Returns an java.lang.reflect.Constructor[] with all constructors 
-     declared by this class"
-
-    "Here, return onlu default constructor. Later, all methods annotated
-     with <jsignature: #'<init>(...)V'> wil be returned as well"
-
-    | ctors |
-
-    ctors := OrderedCollection new.
-    klass selectorsAndMethodsDo:[:sel :mthd|
-        (self isJavaConstructor: mthd selector: sel) ifTrue:[
-            ctors add: (self getDeclaredConstructorFor: mthd).
-        ].
-    ].
-
-    ctors isEmpty ifTrue:[
-        "/If no constructor is found, fake default one...    
-        ctors add: (self getDeclaredConstructorFor: (klass lookupMethodFor: #initialize)).
-    ].
-
-    ^(JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass withAll: ctors
-
-    "Modified: / 22-08-2012 / 11:37:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredFields:publicOnly
-    "Returns an java.lang.reflect.Field[] with all constructors 
-     declared by this class."
-
-    | fields |    
-
-    fields := OrderedCollection new.
-
-    klass instVarNames withIndexDo:[:nm :index|
-        | field |
-
-        field := JavaField new.
-        field 
-            setAccessFlags: JavaConstants ACC_PROTECTED;
-            setClass: klass;
-            setIndex: klass superclass instSize + index;
-            setDescriptor: #'Ljava/lang/Object;';
-            setName: nm.
-
-        fields add: (self createFieldFor: field)
-    ].
-
-    ^ (JavaVM classForName:'java.lang.reflect.Field') javaArrayClass 
-        withAll:fields
-
-    "Modified: / 22-08-2012 / 12:19:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredMethods:publicOnly 
-    "Returns an java.lang.reflect.Method[] with all methods 
-     declared by this class"
-
-    | methods |
-
-    methods := OrderedCollection new.
-    klass selectorsAndMethodsDo:[:sel :mthd|
-        (self isJavaMethod: mthd selector: sel) ifTrue:[
-            methods add: (self getDeclaredMethodFor: mthd).
-        ].
-    ].
-    ^(JavaVM classForName:'java.lang.reflect.Method') javaArrayClass withAll: methods
-
-    "Modified: / 22-08-2012 / 11:36:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getInterfaces
-    "Return a list if interfaces"
-
-    ^ Array with: (JavaVM classForName: 'java.lang.Cloneable')
-
-    "Modified: / 22-08-2012 / 11:06:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getModifiers
-    "Return class modifiers (public/abstract/final...)"
-
-    ^ JavaConstants ACC_PUBLIC
-
-    "Modified: / 22-08-2012 / 11:01:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getName
-    "Returns name of the class"
-    ^'SMALLTALK.' , klass name
-
-    "Created: / 22-08-2012 / 10:47:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaMirror::AlienClassMirror methodsFor:'accessing-private'!
-
-getDeclaredConstructorFor: method
-    ^ self 
-        createConstructorFor: klass 
-                      method: method 
-                   signature: (self getSignatureForConstructor: method)
-                   modifiers: JavaConstants ACC_PUBLIC 
-              parameterTyoes: #() 
-              exceptionTypes: #() 
-                 annotations: JavaMethodAnnotationContainer new
-
-    "Created: / 22-08-2012 / 11:36:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredMethodFor:method
-    | signature name java_lang_Object |
-
-    signature := self getSignatureForMethod: method short: false.
-    name := signature upTo: $(.
-    java_lang_Object := JavaVM classNamed: 'java.lang.Object'.
-    ^ self 
-        createMethodFor: klass 
-                      method: method 
-                        name: name
-                   signature: signature
-                   modifiers: JavaConstants ACC_PUBLIC 
-              parameterTyoes: ((1 to: method selector numArgs) collect:[:i|java_lang_Object])
-                  returnType: java_lang_Object
-              exceptionTypes: #() 
-                 annotations: JavaMethodAnnotationContainer new
-
-    "Created: / 22-08-2012 / 11:36:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getSignatureForConstructor: method
-    | signature |
-
-    signature :=
-        (String streamContents:[:s|
-            s nextPutAll: '<init>('.
-            method selector numArgs timesRepeat:[
-                s nextPutAll:'Ljava/lang/Object;'.
-            ].
-            s nextPutAll: ')V'.
-        ]) asSymbol.
-    ^signature
-
-    "Created: / 22-08-2012 / 11:40:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getSignatureForMethod: method short: short
-    | selector signature |
-
-    selector := method selector.
-    signature :=
-        (String streamContents:[:s|
-            short ifTrue:[
-                s nextPutAll: (selector upTo: $:)
-            ] ifFalse:[
-                selector numArgs == 1 ifTrue:[
-                    s nextPutAll: (selector copyTo: selector size - 1)
-                ] ifFalse:[
-                    s nextPutAll: (selector copyReplaceAll: $: with: $_)
-                ]
-            ].
-            s nextPut: $(.
-            method selector numArgs timesRepeat:[
-                s nextPutAll:'Ljava/lang/Object;'.
-            ].
-            s nextPutAll: ')'.
-            s nextPutAll:'Ljava/lang/Object;'.
-        ]) asSymbol.
-    ^signature
-
-    "Created: / 22-08-2012 / 11:46:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaMirror::AlienClassMirror methodsFor:'testing-private'!
-
-isJavaConstructor: mthd selector: sel
-
-    ^sel startsWith: #initialize
-
-    "Created: / 22-08-2012 / 11:47:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-isJavaMethod: mthd selector: sel
-
-    ^sel isBinarySelector not 
-        and:[(self isJavaConstructor: mthd selector: sel) not]
-
-    "Created: / 22-08-2012 / 11:47:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaMirror::JavaArrayMirror methodsFor:'accessing'!
-
-getClassLoader
-    "Returns a class loader that loaded this class"
-
-    ^ klass javaComponentClass javaMirror getClassLoader
-
-    "Modified: / 31-07-2012 / 18:35:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredConstructors:publicOnly
-    "Returns an java.lang.reflect.Constructor[] with all constructors 
-     declared by this class"
-
-    ^ (JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass new:0.
-
-    "Modified: / 31-07-2012 / 18:41:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredFields:publicOnly
-    "Returns an java.lang.reflect.Field[] with all constructors 
-     declared by this class."
-
-    ^(JavaVM classForName:'java.lang.reflect.Field') javaArrayClass new:0.
-
-    "Modified: / 22-08-2012 / 12:03:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredMethods:publicOnly 
-    "Returns an java.lang.reflect.Method[] with all methods 
-     declared by this class"
-    
-    ^ (JavaVM classForName:'java.lang.reflect.Method') javaArrayClass new:0.
-
-    "Modified: / 01-08-2012 / 11:08:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getInterfaces
-    "Return a list if interfaces"
-
-    ^ Array 
-        with: (JavaVM classForName: 'java.lang.Cloneable')
-        with: (JavaVM classForName: 'java.io.Serializable')
-
-    "Modified: / 22-08-2012 / 11:06:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getModifiers
-    "Return class modifiers (public/abstract/final...)"
-
-    ^ JavaConstants ACC_ABSTRACT | JavaConstants ACC_FINAL | JavaConstants ACC_PUBLIC
-
-    "Modified: / 22-08-2012 / 11:02:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaMirror::JavaClassMirror class methodsFor:'documentation'!
-
-documentation
-"
-    A specialized mirror for all Java classes
-
-    [author:]
-        Jan Vrany <jan.vrany@fit.cvut.cz>
-
-    [instance variables:]
-
-    [class variables:]
-
-    [see also:]
-
-"
-! !
-
-!JavaMirror::JavaClassMirror methodsFor:'accessing'!
-
-getClassLoader
-    "Returns a class loader that loaded this class"
-
-    ^ klass classLoader
-
-    "Modified: / 31-07-2012 / 18:26:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredConstructors:publicOnly
-    "Returns an java.lang.reflect.Constructor[] with all constructors 
-     declared by this class"
-
-    | ctors |
-
-    ctors := OrderedCollection new.
-    klass selectorsAndMethodsDo:[:sel :mthd|
-        (mthd isJavaConstructor and:[publicOnly not or:[mthd isPublic]]) ifTrue:[
-            ctors add: (self getDeclaredConstructorFor: mthd).
-        ].
-    ].
-    ^(JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass withAll: ctors
-
-    "Modified: / 01-08-2012 / 11:04:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredFields:publicOnly
-    "Returns an java.lang.reflect.Field[] with all constructors 
-     declared by this class."
-
-    | fields |    
-
-    fields := klass fields , klass staticFields.
-    publicOnly ifTrue:[ fields := fields select:[:f | f isPublic ] ].
-    JavaClassReader classLoaderQuerySignal answer: klass classLoader do:[
-        fields := fields collect:[:f | self createFieldFor:f ].
-    ].
-    ^ (JavaVM classForName:'java.lang.reflect.Field') javaArrayClass 
-        withAll:fields
-
-    "Modified: / 22-08-2012 / 12:10:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredMethods:publicOnly
-    "Returns an java.lang.reflect.Constructor[] with all constructors 
-     declared by this class"
-
-    | methods |
-
-    methods := OrderedCollection new.
-    klass selectorsAndMethodsDo:[:sel :mthd|
-        mthd isJavaMethod and:[
-            (mthd isJavaConstructor not 
-                and:[mthd isJavaStaticInitializer not
-                and:[publicOnly not or:[mthd isPublic]]]) ifTrue:[
-                methods add: (self getDeclaredMethodFor: mthd).
-            ]
-        ].
-    ].
-    ^(JavaVM classForName:'java.lang.reflect.Method') javaArrayClass withAll: methods
-
-    "Modified: / 19-09-2012 / 16:26:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getGenericSignature
-    "Returns Java generic signature (if a generic class) or nil"
-
-    ^klass signatureJ
-
-    "Created: / 22-08-2012 / 11:58:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getInterfaces
-    "Return a list if interfaces"
-
-    ^ klass interfaces
-
-    "Modified: / 22-08-2012 / 11:08:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getModifiers
-    "Return class modifiers (public/abstract/final...)"
-
-    | modifiers |    
-
-    "According to OpenJDK JVM, strip ACC_SUPER"
-    modifiers := (klass accessFlags & JavaConstants ACC_SUPER bitInvert) & 16r7FFF.
-    "JV@2011-10-30: It seems that private inner classes has no
-     private bit set, sigh"
-    (modifiers & 16r0007) == 0 ifTrue:[
-        modifiers := modifiers | JavaConstants ACC_PRIVATE.
-    ].
-    ^modifiers
-
-    "Modified: / 22-08-2012 / 11:03:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getProtectionDomain
-    ^klass protectionDomain
-
-    "Created: / 22-08-2012 / 12:56:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaMirror::JavaClassMirror methodsFor:'accessing-private'!
-
-getDeclaredConstructorFor: method
-
-    ^self createConstructorFor: method javaClass
-                        method: method
-                     signature: method signature
-                     modifiers: method accessFlags
-                parameterTyoes: (method descriptor parameterClassesUsingClassLoader: method javaClass classLoader)
-                exceptionTypes: method exceptionClasses
-                   annotations: method method annotations.
-
-    "Created: / 01-08-2012 / 00:46:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredMethodFor: method
-
-    ^self      createMethodFor: method javaClass
-                        method: method
-                          name: (method selector upTo: $()
-                     signature: method signature
-                     modifiers: method accessFlags
-                parameterTyoes: (method descriptor parameterClassesUsingClassLoader: method javaClass classLoader)
-                    returnType: (method descriptor returnClassUsingClassLoader: method javaClass classLoader)
-                exceptionTypes: method exceptionClasses
-                   annotations: method method annotations.
-
-    "Created: / 01-08-2012 / 11:11:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaMirror::JavaPrimitiveMirror methodsFor:'accessing'!
-
-getClassLoader
-    "Returns a class loader that loaded this class"
-
-    ^ nil
-
-    "Modified: / 31-07-2012 / 18:35:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredConstructors:publicOnly
-    "Returns an java.lang.reflect.Constructor[] with all constructors 
-     declared by this class"
-
-    ^ (JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass new:0.
-
-    "Modified: / 31-07-2012 / 18:41:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredFields:publicOnly
-    "Returns an java.lang.reflect.Field[] with all constructors 
-     declared by this class."
-
-    ^(JavaVM classForName:'java.lang.reflect.Field') javaArrayClass new:0.
-
-    "Modified: / 22-08-2012 / 12:03:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getDeclaredMethods:publicOnly 
-    "Returns an java.lang.reflect.Method[] with all methods 
-     declared by this class"
-    
-    ^ (JavaVM classForName:'java.lang.reflect.Method') javaArrayClass new:0.
-
-    "Modified: / 01-08-2012 / 11:08:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getInterfaces
-    "Return a list if interfaces"
-
-    ^ #()
-
-    "Modified: / 22-08-2012 / 11:07:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-getModifiers
-    "Return class modifiers (public/abstract/final...)"
-
-    ^ JavaConstants ACC_ABSTRACT | JavaConstants ACC_FINAL | JavaConstants ACC_PUBLIC
-
-    "Modified: / 22-08-2012 / 11:02:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 18-05-2013 / 10:55:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaMirror class methodsFor:'documentation'!