1111import java .util .HashMap ;
1212import java .util .List ;
1313import java .util .Map ;
14- import java .util .logging .Level ;
15- import java .util .logging .Logger ;
1614
1715import com .sun .jna .Function ;
1816import com .sun .jna .Pointer ;
5351 * @since 1.1
5452 */
5553public class NSObject extends Proxy implements PeerableRecipient {
56- private static final Logger LOG = Logger .getLogger ("NSObject" );
57-
58-
5954 /**
6055 * Pointer to the parent objective-c object of this object.
6156 */
@@ -69,7 +64,7 @@ public class NSObject extends Proxy implements PeerableRecipient {
6964 /**
7065 * Maps string selectors to java methods for this class.
7166 */
72- private static Map <Class , Map <String ,Method >> methodMap = new HashMap <Class , Map < String , Method > >();
67+ private static final Map <Class <?>, Map <String ,Method >> methodMap = new HashMap <>();
7368
7469 /**
7570 * Returns the method map for a particular class. The Map that is returned maps string selectors
@@ -78,7 +73,7 @@ public class NSObject extends Proxy implements PeerableRecipient {
7873 * @param cls The class whose map we wish to obtain
7974 * @return The map that maps string selectors
8075 */
81- protected static Map <String ,Method > getMethodMap (Class cls ){
76+ protected static Map <String ,Method > getMethodMap (Class <?> cls ){
8277 Map <String ,Method > mm = methodMap .get (cls );
8378
8479 if ( mm == null ){
@@ -94,20 +89,20 @@ protected static Map<String,Method> getMethodMap(Class cls){
9489 }
9590
9691 for (Class <?> c : classes ) {
97- Method [] methods = c .getDeclaredMethods ();
92+ Method [] methods = c .getDeclaredMethods ();
9893
99- for ( int i =0 ; i <methods .length ; i ++){
100- Method method = methods [i ];
101-
102- // ignore methods of superclasses if override
103- if (!mm .containsKey (method .getName ())) {
104- Msg message = ( Msg ) method .getAnnotation (Msg .class );
105-
106- if ( message != null ){
107- mm .put (message .selector (), method );
108- }
109- }
110- }
94+ for ( int i =0 ; i <methods .length ; i ++){
95+ Method method = methods [i ];
96+
97+ // ignore methods of superclasses if override
98+ if (!mm .containsKey (method .getName ())) {
99+ Msg message = method .getAnnotation (Msg .class );
100+
101+ if ( message != null ){
102+ mm .put (message .selector (), method );
103+ }
104+ }
105+ }
111106 }
112107
113108 methodMap .put (cls , mm );
@@ -268,7 +263,7 @@ public long methodSignatureForSelector(long lselector) {
268263 Pointer selector = new Pointer (lselector );
269264 Method method = methodForSelector (selName (selector ));
270265 if ( method != null ){
271- Msg message = ( Msg ) method .getAnnotation (Msg .class );
266+ Msg message = method .getAnnotation (Msg .class );
272267 if ( !"" .equals (message .signature ()) ){
273268 long res = PointerTool .getPeer (
274269 msgPointer (cls ("NSMethodSignature" ), "signatureWithObjCTypes:" , message .signature ())
@@ -338,19 +333,17 @@ public void forwardInvocationToParent(long linvocation){
338333 strReturnType = strReturnType .substring (offset );
339334 }
340335
341- Object [] args = new Object [new Long ( numArgs ). intValue () ];
336+ Object [] args = new Object [( int ) numArgs ];
342337 args [0 ] = peer ;
343338 args [1 ] = parent ;
344339 for ( int i =2 ; i <numArgs ; i ++){
345- long argumentSigAddr = (Long )pSig .send ("getArgumentTypeAtIndex:" , i );
346- String argumentSignature = new Pointer (argumentSigAddr ).getString (0 );
347340 LongByReference ptrRef = new LongByReference ();
348341 msg (invocation , "getArgument:atIndex:" , ptrRef .getPointer (), i );
349342 args [i ] = ptrRef .getValue ();
350343 }
351344 char retTypeChar = strReturnType .charAt (0 );
352345
353- Class retType = null ;
346+ Class <?> retType = null ;
354347 switch ( retTypeChar ){
355348 case 'v' :
356349 retType = void .class ; break ;
@@ -392,11 +385,7 @@ public void forwardInvocationToParent(long linvocation){
392385 // the normal way
393386 //System.out.println("We give up... passing "+sel(selector)+" to parent");
394387 msg (invocation , "invokeWithTarget:" , parent );
395- return ;
396-
397-
398-
399-
388+ return ;
400389 }
401390
402391 Object retVal = func .invoke (retType , args );
@@ -466,80 +455,70 @@ public void forwardInvocation(long linvocation) {
466455 String selName = selName (selector );
467456 Method method = methodForSelector (selName );
468457 if ( method != null ){
469-
470- Msg message = (Msg )method .getAnnotation (Msg .class );
471- if ( true || !"" .equals (message .signature ()) ){
472- // Perform the method and provide the correct output for the invocation
473-
474-
475- Object [] args = new Object [new Long (numArgs ).intValue ()-2 ];
476- for ( int i =2 ; i <numArgs ; i ++){
477-
478- long argumentSigAddr = (Long )pSig .send ("getArgumentTypeAtIndex:" , i );
479- String argumentSignature = new Pointer (argumentSigAddr ).getString (0 );
480-
481- if ( "fd" .indexOf (argumentSignature .substring (0 ,1 )) != -1 ){
482- DoubleByReference ptrRef = new DoubleByReference ();
483-
484- msg (invocation , "getArgument:atIndex:" , ptrRef .getPointer (), i );
485-
486- args [i -2 ] = TypeMapper
487- .getInstance ()
488- .cToJ (
489- ptrRef .getValue (),
490- //argPtr.toNative(),
491- argumentSignature ,
492- TypeMapper .getInstance ()
493- );
494- } else {
495- LongByReference ptrRef = new LongByReference ();
496-
497- msg (invocation , "getArgument:atIndex:" , ptrRef .getPointer (), i );
498-
499- args [i -2 ] = TypeMapper
500- .getInstance ()
501- .cToJ (
502- ptrRef .getValue (),
503- //argPtr.toNative(),
504- argumentSignature ,
505- TypeMapper .getInstance ()
506- );
507- }
508-
509-
510-
511- }
458+ // Perform the method and provide the correct output for the invocation
459+ Object [] args = new Object [(int ) numArgs - 2 ];
460+ for ( int i =2 ; i <numArgs ; i ++){
461+
462+ long argumentSigAddr = (Long )pSig .send ("getArgumentTypeAtIndex:" , i );
463+ String argumentSignature = new Pointer (argumentSigAddr ).getString (0 );
512464
513- try {
514- method .setAccessible (true );
515- Object res = method .invoke (this , args );
465+ if ( "fd" .indexOf (argumentSignature .charAt (0 )) != -1 ){
466+ DoubleByReference ptrRef = new DoubleByReference ();
516467
517- // We should release the arguments now since we retained them before
518- // to prevent memory leaks.
519- for ( int i =0 ; i <args .length ; i ++){
520- Proxy .release (args [i ]);
521- }
522-
523- long returnType = (Long )pSig .send ("methodReturnType" );
468+ msg (invocation , "getArgument:atIndex:" , ptrRef .getPointer (), i );
524469
525- String strReturnType = new Pointer (returnType ).getString (0 );
470+ args [i -2 ] = TypeMapper
471+ .getInstance ()
472+ .cToJ (
473+ ptrRef .getValue (),
474+ //argPtr.toNative(),
475+ argumentSignature ,
476+ TypeMapper .getInstance ()
477+ );
478+ } else {
479+ LongByReference ptrRef = new LongByReference ();
526480
481+ msg (invocation , "getArgument:atIndex:" , ptrRef .getPointer (), i );
527482
528- res = TypeMapper
529- .getInstance ()
530- .jToC (res , strReturnType , TypeMapper .getInstance ());
531-
532- if ( !"v" .equals (strReturnType )){
533-
534- Object retVal = res == null ? new PointerByReference (Pointer .NULL ).getPointer () : RuntimeUtils .getAsReference (res , strReturnType );
535- msg (invocation , "setReturnValue:" , retVal );
536- }
537-
538- return ;
539- } catch (Exception ex ){
540- throw new NSMessageInvocationException (selName , method , ex );
483+ args [i -2 ] = TypeMapper
484+ .getInstance ()
485+ .cToJ (
486+ ptrRef .getValue (),
487+ //argPtr.toNative(),
488+ argumentSignature ,
489+ TypeMapper .getInstance ()
490+ );
491+ }
492+ }
493+
494+ try {
495+ method .setAccessible (true );
496+ Object res = method .invoke (this , args );
497+
498+ // We should release the arguments now since we retained them before
499+ // to prevent memory leaks.
500+ for ( int i =0 ; i <args .length ; i ++){
501+ Proxy .release (args [i ]);
541502 }
542-
503+
504+ long returnType = (Long ) pSig .send ("methodReturnType" );
505+
506+ String strReturnType = new Pointer (returnType ).getString (0 );
507+
508+
509+ res = TypeMapper
510+ .getInstance ()
511+ .jToC (res , strReturnType , TypeMapper .getInstance ());
512+
513+ if ( !"v" .equals (strReturnType )){
514+
515+ Object retVal = res == null ? new PointerByReference (Pointer .NULL ).getPointer () : RuntimeUtils .getAsReference (res , strReturnType );
516+ msg (invocation , "setReturnValue:" , retVal );
517+ }
518+
519+ return ;
520+ } catch (Exception ex ){
521+ throw new NSMessageInvocationException (selName , method , ex );
543522 }
544523 }
545524 // If we send using invokeWithTarget, then we will use the method of the parent
@@ -628,6 +607,4 @@ public NSObject dealloc(){
628607 }
629608
630609
631-
632-
633610}
0 commit comments