@@ -176,12 +176,18 @@ public class JavaMethod implements NativeMethod {
176
176
private final int mJSArgumentsNeeded ;
177
177
private final String mTraceName ;
178
178
179
- public JavaMethod (Method method ) {
179
+ public JavaMethod (Method method , boolean isSync ) {
180
180
mMethod = method ;
181
181
mMethod .setAccessible (true );
182
+
183
+ if (isSync ) {
184
+ mType = METHOD_TYPE_SYNC ;
185
+ }
186
+
187
+ // TODO: create these lazily
182
188
Class [] parameterTypes = method .getParameterTypes ();
183
189
mArgumentExtractors = buildArgumentExtractors (parameterTypes );
184
- mSignature = buildSignature (parameterTypes );
190
+ mSignature = buildSignature (mMethod , parameterTypes , isSync );
185
191
// Since native methods are invoked from a message queue executed on a single thread, it is
186
192
// save to allocate only one arguments object per method that can be reused across calls
187
193
mArguments = new Object [parameterTypes .length ];
@@ -197,9 +203,16 @@ public String getSignature() {
197
203
return mSignature ;
198
204
}
199
205
200
- private String buildSignature (Class [] paramTypes ) {
201
- StringBuilder builder = new StringBuilder (paramTypes .length );
202
- builder .append ("v." );
206
+ private String buildSignature (Method method , Class [] paramTypes , boolean isSync ) {
207
+ StringBuilder builder = new StringBuilder (paramTypes .length + 2 );
208
+
209
+ if (isSync ) {
210
+ builder .append (returnTypeToChar (method .getReturnType ()));
211
+ builder .append ('.' );
212
+ } else {
213
+ builder .append ("v." );
214
+ }
215
+
203
216
for (int i = 0 ; i < paramTypes .length ; i ++) {
204
217
Class paramClass = paramTypes [i ];
205
218
if (paramClass == ExecutorToken .class ) {
@@ -212,7 +225,9 @@ private String buildSignature(Class[] paramTypes) {
212
225
} else if (paramClass == Promise .class ) {
213
226
Assertions .assertCondition (
214
227
i == paramTypes .length - 1 , "Promise must be used as last parameter only" );
215
- mType = METHOD_TYPE_PROMISE ;
228
+ if (!isSync ) {
229
+ mType = METHOD_TYPE_PROMISE ;
230
+ }
216
231
}
217
232
builder .append (paramTypeToChar (paramClass ));
218
233
}
@@ -352,89 +367,36 @@ public void invoke(CatalystInstance catalystInstance, ExecutorToken executorToke
352
367
* Determines how the method is exported in JavaScript:
353
368
* METHOD_TYPE_ASYNC for regular methods
354
369
* METHOD_TYPE_PROMISE for methods that return a promise object to the caller.
370
+ * METHOD_TYPE_SYNC for sync methods
355
371
*/
356
372
@ Override
357
373
public String getType () {
358
374
return mType ;
359
375
}
360
376
}
361
377
362
- public class SyncJavaHook implements SyncNativeHook {
363
-
364
- private Method mMethod ;
365
- private final String mSignature ;
366
-
367
- public SyncJavaHook (Method method ) {
368
- mMethod = method ;
369
- mMethod .setAccessible (true );
370
- mSignature = buildSignature (method );
371
- }
372
-
373
- public Method getMethod () {
374
- return mMethod ;
375
- }
376
-
377
- public String getSignature () {
378
- return mSignature ;
379
- }
380
-
381
- private String buildSignature (Method method ) {
382
- Class [] paramTypes = method .getParameterTypes ();
383
- StringBuilder builder = new StringBuilder (paramTypes .length + 2 );
384
-
385
- builder .append (returnTypeToChar (method .getReturnType ()));
386
- builder .append ('.' );
387
-
388
- for (int i = 0 ; i < paramTypes .length ; i ++) {
389
- Class paramClass = paramTypes [i ];
390
- if (paramClass == ExecutorToken .class ) {
391
- if (!BaseJavaModule .this .supportsWebWorkers ()) {
392
- throw new RuntimeException (
393
- "Module " + BaseJavaModule .this + " doesn't support web workers, but " +
394
- mMethod .getName () +
395
- " takes an ExecutorToken." );
396
- }
397
- } else if (paramClass == Promise .class ) {
398
- Assertions .assertCondition (
399
- i == paramTypes .length - 1 , "Promise must be used as last parameter only" );
400
- }
401
- builder .append (paramTypeToChar (paramClass ));
402
- }
403
-
404
- return builder .toString ();
405
- }
406
- }
407
-
408
378
private @ Nullable Map <String , NativeMethod > mMethods ;
409
- private @ Nullable Map <String , SyncNativeHook > mHooks ;
410
379
411
380
private void findMethods () {
412
381
if (mMethods == null ) {
413
382
Systrace .beginSection (TRACE_TAG_REACT_JAVA_BRIDGE , "findMethods" );
414
383
mMethods = new HashMap <>();
415
- mHooks = new HashMap <>();
416
384
417
385
Method [] targetMethods = getClass ().getDeclaredMethods ();
418
386
for (Method targetMethod : targetMethods ) {
419
- if (targetMethod .getAnnotation (ReactMethod .class ) != null ) {
420
- String methodName = targetMethod .getName ();
421
- if (mHooks .containsKey (methodName ) || mMethods .containsKey (methodName )) {
422
- // We do not support method overloading since js sees a function as an object regardless
423
- // of number of params.
424
- throw new IllegalArgumentException (
425
- "Java Module " + getName () + " sync method name already registered: " + methodName );
426
- }
427
- mMethods .put (methodName , new JavaMethod (targetMethod ));
428
- }
429
- if (targetMethod .getAnnotation (ReactSyncHook .class ) != null ) {
387
+ ReactMethod annotation = targetMethod .getAnnotation (ReactMethod .class );
388
+ if (annotation != null ) {
430
389
String methodName = targetMethod .getName ();
431
- if (mHooks . containsKey ( methodName ) || mMethods .containsKey (methodName )) {
390
+ if (mMethods .containsKey (methodName )) {
432
391
// We do not support method overloading since js sees a function as an object regardless
433
392
// of number of params.
434
393
throw new IllegalArgumentException (
435
- "Java Module " + getName () + " sync method name already registered: " + methodName );
394
+ "Java Module " + getName () + " method name already registered: " + methodName );
436
395
}
437
- mHooks .put (methodName , new SyncJavaHook (targetMethod ));
396
+ mMethods .put (
397
+ methodName ,
398
+ new JavaMethod (targetMethod ,
399
+ annotation .isBlockingSynchronousMethod ()));
438
400
}
439
401
}
440
402
Systrace .endSection (TRACE_TAG_REACT_JAVA_BRIDGE );
@@ -454,11 +416,6 @@ public final Map<String, NativeMethod> getMethods() {
454
416
return null ;
455
417
}
456
418
457
- public final Map <String , SyncNativeHook > getSyncHooks () {
458
- findMethods ();
459
- return assertNotNull (mHooks );
460
- }
461
-
462
419
@ Override
463
420
public void initialize () {
464
421
// do nothing
0 commit comments