19
19
20
20
package org .rococoa .internal ;
21
21
22
+ import com .sun .jna .Pointer ;
23
+ import net .sf .cglib .proxy .MethodInterceptor ;
24
+ import net .sf .cglib .proxy .MethodProxy ;
25
+ import org .rococoa .*;
26
+ import org .rococoa .cocoa .CFIndex ;
27
+
22
28
import java .lang .reflect .AnnotatedElement ;
23
29
import java .lang .reflect .InvocationHandler ;
24
30
import java .lang .reflect .Method ;
27
33
import java .util .Arrays ;
28
34
import java .util .List ;
29
35
import java .util .concurrent .Callable ;
30
-
31
- import net .sf .cglib .proxy .MethodInterceptor ;
32
- import net .sf .cglib .proxy .MethodProxy ;
33
-
34
- import org .rococoa .Foundation ;
35
- import org .rococoa .ID ;
36
- import org .rococoa .IDByReference ;
37
- import org .rococoa .ObjCObject ;
38
- import org .rococoa .ObjCObjectByReference ;
39
- import org .rococoa .ReleaseInFinalize ;
40
- import org .rococoa .ReturnType ;
41
- import org .rococoa .Rococoa ;
42
- import org .rococoa .RococoaException ;
43
- import org .rococoa .RunOnMainThread ;
44
-
45
36
import java .util .logging .Level ;
46
37
import java .util .logging .Logger ;
47
38
48
- import com .sun .jna .Pointer ;
49
- import org .rococoa .cocoa .CFIndex ;
50
-
51
39
/**
52
40
* Listens to invocations of methods on a Java NSObject, and forwards them to
53
41
* its Objective-C counterpart.
54
42
*
55
43
* @author duncan
56
- *
57
44
*/
58
- @ SuppressWarnings ("nls" )
59
45
public class ObjCObjectInvocationHandler implements InvocationHandler , MethodInterceptor {
60
46
61
47
private static final int FINALIZE_AUTORELEASE_BATCH_SIZE = 1000 ;
@@ -73,16 +59,14 @@ public class ObjCObjectInvocationHandler implements InvocationHandler, MethodInt
73
59
OBJECT_HASHCODE = Object .class .getMethod ("hashCode" );
74
60
OBJECT_EQUALS = Object .class .getMethod ("equals" , Object .class );
75
61
OCOBJECT_ID = ObjCObject .class .getMethod ("id" );
76
- }
77
- catch (Exception x ) {
62
+ } catch (NoSuchMethodException x ) {
78
63
throw new RococoaException ("Error retrieving method" , x );
79
64
}
80
65
}
81
66
82
67
private ID ocInstance ;
83
68
private final String javaClassName ;
84
69
private final boolean invokeAllMethodsOnMainThread ;
85
-
86
70
private final boolean releaseOnFinalize ;
87
71
private volatile boolean finalized ;
88
72
@@ -94,7 +78,7 @@ public ObjCObjectInvocationHandler(final ID ocInstance, Class<? extends ObjCObje
94
78
95
79
if (logging .isLoggable (Level .FINEST )) {
96
80
CFIndex retainCount = Foundation .cfGetRetainCount (ocInstance );
97
- logging .finest (String .format ("Creating NSObjectInvocationHandler for id %s, javaclass %s. retain = %s, retainCount = %s" ,
81
+ logging .finest (String .format ("Creating ObjCObjectInvocationHandler for id %s, javaclass %s. retain = %s, retainCount = %s" ,
98
82
ocInstance , javaClass , retain , retainCount .intValue ()));
99
83
}
100
84
@@ -104,10 +88,7 @@ public ObjCObjectInvocationHandler(final ID ocInstance, Class<? extends ObjCObje
104
88
105
89
if (retain ) {
106
90
if (callAcrossToMainThread ()) {
107
- Foundation .runOnMainThread (new Runnable () {
108
- public void run () {
109
- Foundation .cfRetain (ocInstance );
110
- }});
91
+ Foundation .runOnMainThread (() -> Foundation .cfRetain (ocInstance ));
111
92
} else {
112
93
Foundation .cfRetain (ocInstance );
113
94
}
@@ -131,10 +112,7 @@ protected void finalize() throws Throwable {
131
112
}
132
113
try {
133
114
if (callAcrossToMainThread ()) {
134
- Foundation .runOnMainThread (new Runnable () {
135
- public void run () {
136
- release ();
137
- }});
115
+ Foundation .runOnMainThread (() -> release ());
138
116
} else {
139
117
AutoreleaseBatcher autoreleaseBatcher = AutoreleaseBatcher .forThread (FINALIZE_AUTORELEASE_BATCH_SIZE );
140
118
release ();
@@ -163,7 +141,7 @@ private void release() {
163
141
/**
164
142
* Callback from java.lang.reflect proxy
165
143
*/
166
- public Object invoke (Object proxy , Method method , Object [] args ) throws Exception {
144
+ public Object invoke (Object proxy , Method method , Object [] args ) throws Exception {
167
145
if (logging .isLoggable (Level .FINEST )) {
168
146
logging .finest (String .format ("invoking [%s %s].%s(%s)" ,
169
147
javaClassName , ocInstance , method .getName (), new VarArgsUnpacker (args )));
@@ -226,7 +204,7 @@ private Object invokeIsEqual(final ID another) {
226
204
return sendOnThisOrMainThread (null , ocInstance , "isEqual:" , Boolean .class , another );
227
205
}
228
206
229
- private Object invokeCocoa (final Method method , Object [] args ) {
207
+ protected Object invokeCocoa (final Method method , Object [] args ) {
230
208
String selectorName = selectorNameFor (method );
231
209
Class <?> returnType = returnTypeFor (method );
232
210
Object [] marshalledArgs = marshallArgsFor (args );
@@ -249,14 +227,14 @@ private Object invokeCocoa(final Method method, Object[] args) {
249
227
return result ;
250
228
}
251
229
252
- private void handleInitMethod (Object result ) {
230
+ private void handleInitMethod (Object result ) {
253
231
// Normally init methods return self, but on error they may return nil.
254
232
// In this case the ObjC object for which this is the handler is considered
255
233
// freed and should not be released when we are finalized.
256
234
if (result != null ) {
257
235
return ;
258
236
}
259
- ocInstance = ID .fromLong (0 );
237
+ ocInstance = ID .fromLong (0 );
260
238
}
261
239
262
240
private Object sendOnThisOrMainThread (Method method , final ID id , final String selectorName , final Class <?> returnType , final Object ... args ) {
@@ -285,7 +263,7 @@ private void fillInReferences(Object[] args, Object[] marshalledArgs) {
285
263
continue ;
286
264
}
287
265
((ObjCObjectByReference ) original ).setObject (
288
- Rococoa .wrap (((IDByReference ) marshalled ).getValue (), ObjCObject .class ));
266
+ Rococoa .wrap (((IDByReference ) marshalled ).getValue (), ObjCObject .class ));
289
267
}
290
268
}
291
269
}
@@ -294,8 +272,7 @@ private Class<?> returnTypeFor(final Method method) {
294
272
ReturnType annotation = method .getAnnotation (ReturnType .class );
295
273
if (annotation == null ) {
296
274
return method .getReturnType ();
297
- }
298
- else {
275
+ } else {
299
276
return annotation .value ();
300
277
}
301
278
}
@@ -304,14 +281,13 @@ private Object[] marshallArgsFor(Object[] args) {
304
281
if (args == null ) {
305
282
return null ;
306
283
}
307
- List <Object > result = new ArrayList <Object >(args .length );
308
- for (int i = 0 ; i < args . length ; i ++ ) {
309
- Object marshalled = marshall (args [ i ] );
284
+ List <Object > result = new ArrayList <>(args .length );
285
+ for (Object arg : args ) {
286
+ final Object marshalled = marshall (arg );
310
287
if (marshalled instanceof Object []) {
311
288
// flatten varags, it would never(?) make sense to pass Object[] to Cococoa
312
289
result .addAll (Arrays .asList ((Object []) marshalled ));
313
- }
314
- else {
290
+ } else {
315
291
result .add (marshalled );
316
292
}
317
293
}
@@ -325,14 +301,14 @@ private Object marshall(Object arg) {
325
301
return null ;
326
302
}
327
303
if (arg instanceof ObjCObjectByReference ) {
328
- // Forward conversion (another backwards conversion will take place in fillInReferences)
304
+ // Forward conversion (another backwards conversion will take place in fillInReferences)
329
305
IDByReference idref = new IDByReference ();
330
- ObjCObject ob = ((ObjCObjectByReference )arg ).getValueAs (ObjCObject .class );
331
- if (ob != null ) {
332
- idref .setValue (ob .id ());
306
+ ObjCObject ob = ((ObjCObjectByReference ) arg ).getValueAs (ObjCObject .class );
307
+ if (ob != null ) {
308
+ idref .setValue (ob .id ());
333
309
}
334
- return idref ;
335
- }
310
+ return idref ;
311
+ }
336
312
return arg ;
337
313
}
338
314
@@ -352,7 +328,7 @@ private String selectorNameFor(Method method) {
352
328
}
353
329
return result .toString ();
354
330
}
355
-
331
+
356
332
private boolean shouldInvokeMethodsOnMainThread (AnnotatedElement element ) {
357
333
return element != null && element .getAnnotation (RunOnMainThread .class ) != null ;
358
334
}
@@ -362,6 +338,6 @@ private boolean callAcrossToMainThread() {
362
338
}
363
339
364
340
private boolean callAcrossToMainThreadFor (Method m ) {
365
- return (invokeAllMethodsOnMainThread || shouldInvokeMethodsOnMainThread (m ) ) && !Foundation .isMainThread () ;
341
+ return (invokeAllMethodsOnMainThread || shouldInvokeMethodsOnMainThread (m )) && !Foundation .isMainThread ();
366
342
}
367
343
}
0 commit comments