149 "Created: / 31-07-2012 / 18:25:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
149 "Created: / 31-07-2012 / 18:25:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
150 ! |
150 ! |
151 |
151 |
152 getDeclaredConstructors: publicOnly |
152 getDeclaredConstructors: publicOnly |
153 "Returns an java.lang.reflect.Constructor[] with all constructors |
153 "Returns an java.lang.reflect.Constructor[] with all constructors |
154 declared by this class" |
154 declared by this class. " |
155 |
155 |
156 ^self subclassResponsibility |
156 ^self subclassResponsibility |
157 |
157 |
158 "Created: / 31-07-2012 / 18:39:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
158 "Created: / 31-07-2012 / 18:39:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
159 ! |
|
160 |
|
161 getDeclaredFields: publicOnly |
|
162 "Returns an java.lang.reflect.Field[] with all constructors |
|
163 declared by this class." |
|
164 |
|
165 ^self subclassResponsibility |
|
166 |
|
167 "Created: / 22-08-2012 / 12:03:04 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
159 ! |
168 ! |
160 |
169 |
161 getDeclaredMethods:publicOnly |
170 getDeclaredMethods:publicOnly |
162 "Returns an java.lang.reflect.Method[] with all methods |
171 "Returns an java.lang.reflect.Method[] with all methods |
163 declared by this class" |
172 declared by this class." |
164 |
173 |
165 ^ self subclassResponsibility |
174 ^ self subclassResponsibility |
166 |
175 |
167 "Created: / 01-08-2012 / 11:07:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
176 "Created: / 01-08-2012 / 11:07:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
177 ! |
|
178 |
|
179 getGenericSignature |
|
180 "Returns Java generic signature (if a generic class) or nil" |
|
181 |
|
182 ^nil |
|
183 |
|
184 "Created: / 22-08-2012 / 11:57:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
185 ! |
|
186 |
|
187 getInterfaces |
|
188 "Return a list if interfaces" |
|
189 |
|
190 ^self subclassResponsibility |
|
191 |
|
192 "Created: / 22-08-2012 / 11:05:13 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
193 ! |
|
194 |
|
195 getModifiers |
|
196 "Return class modifiers (public/abstract/final...)" |
|
197 |
|
198 ^self subclassResponsibility |
|
199 |
|
200 "Created: / 22-08-2012 / 10:49:19 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
201 ! |
|
202 |
|
203 getName |
|
204 "Returns name of the class" |
|
205 ^klass javaName |
|
206 |
|
207 "Created: / 22-08-2012 / 10:46:34 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
168 ! ! |
208 ! ! |
169 |
209 |
170 !JavaMirror methodsFor:'initialization'! |
210 !JavaMirror methodsFor:'initialization'! |
171 |
211 |
172 setKlass: aClass |
212 setKlass: aClass |
246 ^ctor |
286 ^ctor |
247 |
287 |
248 "Created: / 01-08-2012 / 10:20:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
288 "Created: / 01-08-2012 / 10:20:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
249 ! |
289 ! |
250 |
290 |
|
291 createFieldFor: javaField |
|
292 "given a java field, return the corresponding java.lang.Field |
|
293 instance for it." |
|
294 " |
|
295 See OpenJDK7 source: |
|
296 jdk7/hotspot/src/share/vm/runtime/reflection.cpp, |
|
297 oop Reflection::new_field" |
|
298 |
|
299 | field clazz name slot type modifiers | |
|
300 |
|
301 clazz := reflection javaClassObjectForClass: klass. |
|
302 name := JavaVM reflection javaStringObjectForString: javaField name |
|
303 interned: true. |
|
304 slot := javaField index. |
|
305 type := JavaVM javaClassObjectForClass: javaField typeClass. |
|
306 modifiers := javaField accessFlags. |
|
307 field := (JavaVM classForName: 'java.lang.reflect.Field') new. |
|
308 field |
|
309 instVarNamed: #clazz put: clazz; |
|
310 instVarNamed: #name put: name; |
|
311 instVarNamed: #slot put: slot; |
|
312 instVarNamed: #type put: type; |
|
313 instVarNamed: #modifiers put: modifiers; |
|
314 yourself. |
|
315 javaField annotations |
|
316 ifNotNil: [ |
|
317 field instVarNamed: #annotations |
|
318 put: javaField annotations runtimeVisible rawAnnotations |
|
319 ]. |
|
320 ^ field. |
|
321 |
|
322 "Created: / 22-08-2012 / 12:09:27 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
323 ! |
|
324 |
251 createMethodFor: class method: method name: name signature: signature modifiers: modifiers parameterTyoes: parameterClasses returnType: returnClass exceptionTypes: exceptionClasses annotations: annotations |
325 createMethodFor: class method: method name: name signature: signature modifiers: modifiers parameterTyoes: parameterClasses returnType: returnClass exceptionTypes: exceptionClasses annotations: annotations |
252 "Creates a new java.lang.Constructor. Arguments: |
326 "Creates a new java.lang.Constructor. Arguments: |
253 |
327 |
254 class <Class> |
328 class <Class> |
255 method <Method> |
329 method <Method> |
313 |
387 |
314 getDeclaredConstructors:publicOnly |
388 getDeclaredConstructors:publicOnly |
315 "Returns an java.lang.reflect.Constructor[] with all constructors |
389 "Returns an java.lang.reflect.Constructor[] with all constructors |
316 declared by this class" |
390 declared by this class" |
317 |
391 |
318 ^ self shouldImplement |
392 "Here, return onlu default constructor. Later, all methods annotated |
|
393 with <jsignature: #'<init>(...)V'> wil be returned as well" |
|
394 |
|
395 | ctors | |
|
396 |
|
397 ctors := OrderedCollection new. |
|
398 klass selectorsAndMethodsDo:[:sel :mthd| |
|
399 (self isJavaConstructor: mthd selector: sel) ifTrue:[ |
|
400 ctors add: (self getDeclaredConstructorFor: mthd). |
|
401 ]. |
|
402 ]. |
|
403 |
|
404 ctors isEmpty ifTrue:[ |
|
405 "/If no constructor is found, fake default one... |
|
406 ctors add: (self getDeclaredConstructorFor: (klass lookupMethodFor: #initialize)). |
|
407 ]. |
|
408 |
|
409 ^(JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass withAll: ctors |
|
410 |
|
411 "Modified: / 22-08-2012 / 11:37:50 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
412 ! |
|
413 |
|
414 getDeclaredFields:publicOnly |
|
415 "Returns an java.lang.reflect.Field[] with all constructors |
|
416 declared by this class." |
|
417 |
|
418 | fields | |
|
419 |
|
420 fields := OrderedCollection new. |
|
421 |
|
422 klass instVarNames withIndexDo:[:nm :index| |
|
423 | field | |
|
424 |
|
425 field := JavaField new. |
|
426 field |
|
427 setAccessFlags: JavaConstants ACC_PROTECTED; |
|
428 setClass: klass; |
|
429 setIndex: klass superclass instSize + index; |
|
430 setDescriptor: #'Ljava/lang/Object;'; |
|
431 setName: nm. |
|
432 |
|
433 fields add: (self createFieldFor: field) |
|
434 ]. |
|
435 |
|
436 ^ (JavaVM classForName:'java.lang.reflect.Field') javaArrayClass |
|
437 withAll:fields |
|
438 |
|
439 "Modified: / 22-08-2012 / 12:19:05 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
319 ! |
440 ! |
320 |
441 |
321 getDeclaredMethods:publicOnly |
442 getDeclaredMethods:publicOnly |
322 "Returns an java.lang.reflect.Method[] with all methods |
443 "Returns an java.lang.reflect.Method[] with all methods |
323 declared by this class" |
444 declared by this class" |
324 |
445 |
325 ^ self shouldImplement |
446 | methods | |
|
447 |
|
448 methods := OrderedCollection new. |
|
449 klass selectorsAndMethodsDo:[:sel :mthd| |
|
450 (self isJavaMethod: mthd selector: sel) ifTrue:[ |
|
451 methods add: (self getDeclaredMethodFor: mthd). |
|
452 ]. |
|
453 ]. |
|
454 ^(JavaVM classForName:'java.lang.reflect.Method') javaArrayClass withAll: methods |
|
455 |
|
456 "Modified: / 22-08-2012 / 11:36:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
457 ! |
|
458 |
|
459 getInterfaces |
|
460 "Return a list if interfaces" |
|
461 |
|
462 ^ Array with: (JavaVM classForName: 'java.lang.Cloneable') |
|
463 |
|
464 "Modified: / 22-08-2012 / 11:06:08 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
465 ! |
|
466 |
|
467 getModifiers |
|
468 "Return class modifiers (public/abstract/final...)" |
|
469 |
|
470 ^ JavaConstants ACC_PUBLIC |
|
471 |
|
472 "Modified: / 22-08-2012 / 11:01:55 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
473 ! |
|
474 |
|
475 getName |
|
476 "Returns name of the class" |
|
477 ^'SMALLTALK.' , klass name |
|
478 |
|
479 "Created: / 22-08-2012 / 10:47:38 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
480 ! ! |
|
481 |
|
482 !JavaMirror::AlienClassMirror methodsFor:'accessing-private'! |
|
483 |
|
484 getDeclaredConstructorFor: method |
|
485 ^ self |
|
486 createConstructorFor: klass |
|
487 method: method |
|
488 signature: (self getSignatureForConstructor: method) |
|
489 modifiers: JavaConstants ACC_PUBLIC |
|
490 parameterTyoes: #() |
|
491 exceptionTypes: #() |
|
492 annotations: JavaMethodAnnotationContainer new |
|
493 |
|
494 "Created: / 22-08-2012 / 11:36:18 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
495 ! |
|
496 |
|
497 getDeclaredMethodFor:method |
|
498 | signature name | |
|
499 |
|
500 signature := self getSignatureForMethod: method short: false. |
|
501 name := signature upTo: $(. |
|
502 ^ self |
|
503 createMethodFor: klass |
|
504 method: method |
|
505 name: name |
|
506 signature: signature |
|
507 modifiers: JavaConstants ACC_PUBLIC |
|
508 parameterTyoes: #() |
|
509 returnType: (JavaVM classNamed: 'java.lang.Object') |
|
510 exceptionTypes: #() |
|
511 annotations: JavaMethodAnnotationContainer new |
|
512 |
|
513 "Created: / 22-08-2012 / 11:36:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
514 ! |
|
515 |
|
516 getSignatureForConstructor: method |
|
517 | signature | |
|
518 |
|
519 signature := |
|
520 (String streamContents:[:s| |
|
521 s nextPutAll: '<init>('. |
|
522 method selector numArgs timesRepeat:[ |
|
523 s nextPutAll:'Ljava/lang/Object;'. |
|
524 ]. |
|
525 s nextPutAll: ')V'. |
|
526 ]) asSymbol. |
|
527 ^signature |
|
528 |
|
529 "Created: / 22-08-2012 / 11:40:12 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
530 ! |
|
531 |
|
532 getSignatureForMethod: method short: short |
|
533 | selector signature | |
|
534 |
|
535 selector := method selector. |
|
536 signature := |
|
537 (String streamContents:[:s| |
|
538 short ifTrue:[ |
|
539 s nextPutAll: (selector upTo: $:) |
|
540 ] ifFalse:[ |
|
541 s nextPutAll: (selector copyReplaceAll: $: with: $_) |
|
542 ]. |
|
543 method selector numArgs timesRepeat:[ |
|
544 s nextPutAll:'Ljava/lang/Object;'. |
|
545 ]. |
|
546 s nextPutAll: ')'. |
|
547 s nextPutAll:'Ljava/lang/Object;'. |
|
548 ]) asSymbol. |
|
549 ^signature |
|
550 |
|
551 "Created: / 22-08-2012 / 11:46:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
552 ! ! |
|
553 |
|
554 !JavaMirror::AlienClassMirror methodsFor:'testing-private'! |
|
555 |
|
556 isJavaConstructor: mthd selector: sel |
|
557 |
|
558 ^sel startsWith: #initialize |
|
559 |
|
560 "Created: / 22-08-2012 / 11:47:34 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
561 ! |
|
562 |
|
563 isJavaMethod: mthd selector: sel |
|
564 |
|
565 ^sel isBinarySelector not |
|
566 and:[(self isJavaConstructor: mthd selector: sel) not] |
|
567 |
|
568 "Created: / 22-08-2012 / 11:47:12 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
326 ! ! |
569 ! ! |
327 |
570 |
328 !JavaMirror::JavaArrayMirror methodsFor:'accessing'! |
571 !JavaMirror::JavaArrayMirror methodsFor:'accessing'! |
329 |
572 |
330 getClassLoader |
573 getClassLoader |
340 declared by this class" |
583 declared by this class" |
341 |
584 |
342 ^ (JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass new:0. |
585 ^ (JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass new:0. |
343 |
586 |
344 "Modified: / 31-07-2012 / 18:41:12 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
587 "Modified: / 31-07-2012 / 18:41:12 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
588 ! |
|
589 |
|
590 getDeclaredFields:publicOnly |
|
591 "Returns an java.lang.reflect.Field[] with all constructors |
|
592 declared by this class." |
|
593 |
|
594 ^(JavaVM classForName:'java.lang.reflect.Field') javaArrayClass new:0. |
|
595 |
|
596 "Modified: / 22-08-2012 / 12:03:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
345 ! |
597 ! |
346 |
598 |
347 getDeclaredMethods:publicOnly |
599 getDeclaredMethods:publicOnly |
348 "Returns an java.lang.reflect.Method[] with all methods |
600 "Returns an java.lang.reflect.Method[] with all methods |
349 declared by this class" |
601 declared by this class" |
350 |
602 |
351 ^ (JavaVM classForName:'java.lang.reflect.Method') javaArrayClass new:0. |
603 ^ (JavaVM classForName:'java.lang.reflect.Method') javaArrayClass new:0. |
352 |
604 |
353 "Modified: / 01-08-2012 / 11:08:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
605 "Modified: / 01-08-2012 / 11:08:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
606 ! |
|
607 |
|
608 getInterfaces |
|
609 "Return a list if interfaces" |
|
610 |
|
611 ^ Array |
|
612 with: (JavaVM classForName: 'java.lang.Cloneable') |
|
613 with: (JavaVM classForName: 'java.io.Serializable') |
|
614 |
|
615 "Modified: / 22-08-2012 / 11:06:47 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
616 ! |
|
617 |
|
618 getModifiers |
|
619 "Return class modifiers (public/abstract/final...)" |
|
620 |
|
621 ^ JavaConstants ACC_ABSTRACT | JavaConstants ACC_FINAL | JavaConstants ACC_PUBLIC |
|
622 |
|
623 "Modified: / 22-08-2012 / 11:02:18 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
354 ! ! |
624 ! ! |
355 |
625 |
356 !JavaMirror::JavaClassMirror class methodsFor:'documentation'! |
626 !JavaMirror::JavaClassMirror class methodsFor:'documentation'! |
357 |
627 |
358 documentation |
628 documentation |
396 ^(JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass withAll: ctors |
666 ^(JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass withAll: ctors |
397 |
667 |
398 "Modified: / 01-08-2012 / 11:04:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
668 "Modified: / 01-08-2012 / 11:04:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
399 ! |
669 ! |
400 |
670 |
|
671 getDeclaredFields:publicOnly |
|
672 "Returns an java.lang.reflect.Field[] with all constructors |
|
673 declared by this class." |
|
674 |
|
675 | fields | |
|
676 |
|
677 fields := klass fields , klass staticFields. |
|
678 publicOnly ifTrue:[ fields := fields select:[:f | f isPublic ] ]. |
|
679 JavaClassReader classLoaderQuerySignal answer: klass classLoader do:[ |
|
680 fields := fields collect:[:f | self createFieldFor:f ]. |
|
681 ]. |
|
682 ^ (JavaVM classForName:'java.lang.reflect.Field') javaArrayClass |
|
683 withAll:fields |
|
684 |
|
685 "Modified: / 22-08-2012 / 12:10:04 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
686 ! |
|
687 |
401 getDeclaredMethods:publicOnly |
688 getDeclaredMethods:publicOnly |
402 "Returns an java.lang.reflect.Constructor[] with all constructors |
689 "Returns an java.lang.reflect.Constructor[] with all constructors |
403 declared by this class" |
690 declared by this class" |
404 |
691 |
405 | ctors | |
692 | methods | |
406 |
693 |
407 ctors := OrderedCollection new. |
694 methods := OrderedCollection new. |
408 klass selectorsAndMethodsDo:[:sel :mthd| |
695 klass selectorsAndMethodsDo:[:sel :mthd| |
409 (mthd isJavaConstructor not and:[publicOnly not or:[mthd isPublic]]) ifTrue:[ |
696 (mthd isJavaConstructor not and:[publicOnly not or:[mthd isPublic]]) ifTrue:[ |
410 ctors add: (self getDeclaredMethodFor: mthd). |
697 methods add: (self getDeclaredMethodFor: mthd). |
411 ]. |
698 ]. |
412 ]. |
699 ]. |
413 ^(JavaVM classForName:'java.lang.reflect.Method') javaArrayClass withAll: ctors |
700 ^(JavaVM classForName:'java.lang.reflect.Method') javaArrayClass withAll: methods |
414 |
701 |
415 "Modified: / 01-08-2012 / 11:10:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
702 "Modified: / 01-08-2012 / 11:10:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
703 ! |
|
704 |
|
705 getGenericSignature |
|
706 "Returns Java generic signature (if a generic class) or nil" |
|
707 |
|
708 ^klass signatureJ |
|
709 |
|
710 "Created: / 22-08-2012 / 11:58:04 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
711 ! |
|
712 |
|
713 getInterfaces |
|
714 "Return a list if interfaces" |
|
715 |
|
716 ^ klass interfaces |
|
717 |
|
718 "Modified: / 22-08-2012 / 11:08:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
719 ! |
|
720 |
|
721 getModifiers |
|
722 "Return class modifiers (public/abstract/final...)" |
|
723 |
|
724 | modifiers | |
|
725 |
|
726 "According to OpenJDK JVM, strip ACC_SUPER" |
|
727 modifiers := (klass accessFlags & JavaConstants ACC_SUPER bitInvert) & 16r7FFF. |
|
728 "JV@2011-10-30: It seems that private inner classes has no |
|
729 private bit set, sigh" |
|
730 (modifiers & 16r0007) == 0 ifTrue:[ |
|
731 modifiers := modifiers | JavaConstants ACC_PRIVATE. |
|
732 ]. |
|
733 ^modifiers |
|
734 |
|
735 "Modified: / 22-08-2012 / 11:03:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
416 ! ! |
736 ! ! |
417 |
737 |
418 !JavaMirror::JavaClassMirror methodsFor:'accessing-private'! |
738 !JavaMirror::JavaClassMirror methodsFor:'accessing-private'! |
419 |
739 |
420 getDeclaredConstructorFor: method |
740 getDeclaredConstructorFor: method |
462 ^ (JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass new:0. |
782 ^ (JavaVM classForName:'java.lang.reflect.Constructor') javaArrayClass new:0. |
463 |
783 |
464 "Modified: / 31-07-2012 / 18:41:01 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
784 "Modified: / 31-07-2012 / 18:41:01 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
465 ! |
785 ! |
466 |
786 |
|
787 getDeclaredFields:publicOnly |
|
788 "Returns an java.lang.reflect.Field[] with all constructors |
|
789 declared by this class." |
|
790 |
|
791 ^(JavaVM classForName:'java.lang.reflect.Field') javaArrayClass new:0. |
|
792 |
|
793 "Modified: / 22-08-2012 / 12:03:35 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
794 ! |
|
795 |
467 getDeclaredMethods:publicOnly |
796 getDeclaredMethods:publicOnly |
468 "Returns an java.lang.reflect.Method[] with all methods |
797 "Returns an java.lang.reflect.Method[] with all methods |
469 declared by this class" |
798 declared by this class" |
470 |
799 |
471 ^ (JavaVM classForName:'java.lang.reflect.Method') javaArrayClass new:0. |
800 ^ (JavaVM classForName:'java.lang.reflect.Method') javaArrayClass new:0. |
472 |
801 |
473 "Modified: / 01-08-2012 / 11:08:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
802 "Modified: / 01-08-2012 / 11:08:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
803 ! |
|
804 |
|
805 getInterfaces |
|
806 "Return a list if interfaces" |
|
807 |
|
808 ^ #() |
|
809 |
|
810 "Modified: / 22-08-2012 / 11:07:42 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
811 ! |
|
812 |
|
813 getModifiers |
|
814 "Return class modifiers (public/abstract/final...)" |
|
815 |
|
816 ^ JavaConstants ACC_ABSTRACT | JavaConstants ACC_FINAL | JavaConstants ACC_PUBLIC |
|
817 |
|
818 "Modified: / 22-08-2012 / 11:02:26 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
474 ! ! |
819 ! ! |
475 |
820 |
476 !JavaMirror class methodsFor:'documentation'! |
821 !JavaMirror class methodsFor:'documentation'! |
477 |
822 |
478 version_SVN |
823 version_SVN |