Slightly updated demo to use method.setAccessible(true) and made 
MethodHandles.Lookup to be static. The latest version is 
https://gist.github.com/chemerisuk/287e6cd6e1aa61a5680027967008b702. Numbers 
haven't changed a lot:

OldCordovaPlugin: 0.03s with v=6000000
MethodCordovaPlugin: 0.14s with v=6000000
MethodHandleCordovaPlugin: 0.12s with v=6000000
LambdaCordovaPlugin: 0.09s with v=6000000

Cache disabled

MethodCordovaPlugin: 0.97s with v=6060000
MethodHandleCordovaPlugin: 1.18s with v=6060000
LambdaCordovaPlugin: 7.32s with v=6060000

Also there were several questions in the conversation I have to address:

1. ProGuard - in the example implementation I intentionally mark action field 
of @CordovaMethod to be required. So the reflection lookup checks string value 
of the action field instead of method name that is not going to be ubfuscated.

2. Backward compatibility. @CordovaMethod logic will be in default 
implementation of execute 
https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaPlugin.java#L134.
 This implementation at present just returns false, and plugin authors must 
overload the execute method to put their actions logic there. So for existing 
plugins new reflection logic won't be invoked because that method now is 
overloaded. New plugin authors don't need to overload the method execute, they 
will simply use @CordovaMethod and that's it.

What do you think about creating a PR to cordova-android. Any 
concerns/questions?

On 2018/04/29 08:58:57, chemeri...@gmail.com <chemeri...@gmail.com> wrote: 
> Hi guys. cordova-ios has a nice method binding for plugins. Unfortunately 
> cordova-android requires at present boilerplate implementation of method 
> execute:
> 
> public class MyPlugin extends CordovaPlugin {
>     ...
>     @Override
>     public boolean execute(String action, JSONArray args, CallbackContext 
> callbackContext) throws JSONException {
>         if (METHOD_1.equals(action)) {
>             method1(args, callbackContext);
>         } else if (METHOD_2.equals(action)) {
>             method2(args, callbackContext);
>         ...
>         } else {
>             return false;
>         }
>         return true;
>     }
>     ...
> }
> 
> I suggest to implement support for @CordovaMethod that will allow for plugin 
> authors to reduce boilerplate code, so the implementation above will look 
> like:
> 
> public class MyPlugin extends CordovaPlugin {
>     ...
>     @CordovaMethod
>     private void method1(JSONArray args, CallbackContext callbackContext) 
> throws JSONException {
>         ...
>     }
> 
>     @CordovaMethod
>     private void method2(JSONArray args, CallbackContext callbackContext) 
> throws JSONException {
>         ...
>     }
>     ...
> }
> 
> Implementation of method execute in CordovaPlugin.java will check for methods 
> marked with @CordovaMethod and if action argument matches a method with 
> @CordovaMethod, the method will be invoked. Developer can also specify action 
> manually via the name parameter:
> 
> public class MyPlugin extends CordovaPlugin {
>     ...
>     @CordovaMethod(name = "method1")
>     private void myMethod(JSONArray args, CallbackContext callbackContext) 
> throws JSONException {
>         ...
>     }
>     ...
> }
> 
> Important to note that backward compatibility is preserved - old plugins 
> didn't have @CordovaMethod, but new ones can use it to simplify code.
> 
> What do you think?
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
> For additional commands, e-mail: dev-h...@cordova.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org

Reply via email to