RFR: 8159746: (proxy) Support for default methods

Alan Bateman alanb at openjdk.java.net
Thu Oct 22 14:26:15 UTC 2020


On Thu, 22 Oct 2020 00:24:59 GMT, Mandy Chung <mchung at openjdk.org> wrote:

>> Hi Peter, thanks for coming with these alternatives.   The need for new `InvocationHandler2` and `InvocationHandler2.SuperInvoker` APIs and the complexity of `plevart:proxy-default-method-alt-api2` look unpleasing in order to keep the invocation of default methods in lambdas.  I prefer the `DelegatingInvocationHandler` abstract class approach without caller-sensitive method.
>
> Hi Peter, Alan
> 
> I have prototyped two alternatives ([mlchung:proxy-default-method-3](https://github.com/mlchung/jdk/tree/proxy-default-method-3)).
> 1. `DelegatingInvocationHandler` abstract class with:
> protected final Object invokeDefault(Object proxy, Method method, Object... params)
> 
> 2. `NewInvocationHandler` functional interface
>     public Object invoke(DefaultMethodInvoker invoker, Object proxy, Method method, Object... args) throws Throwable;
> 
>     /**
>      * {@inheritDoc}
>      */
>     public default Object invoke(Object proxy, Method method, Object[] args)
>             throws Throwable
>     {
>          return invoke(DefaultMethodInvoker.getInvoker(proxy, this), proxy, method, args);
>     }
> 
>    public final class DefaultMethodInvoker {
>         public final Object invoke(Object proxy, Method method, Object... args)
>                 throws InvocationTargetException
>    }
> 
> Similar idea as yours SuperInvoker.  But I think this is less complicated
> and DefaultMethodInvoker is a final class rather than an interface.
> The generated proxy class will keep invoking `InvocationHandler::invoke`
> (no change in `ProxyGenerator`).  `NewInvocationHandler` allows the use of lambdas.
> 
> I like `DelegatingInvocationHandler` which is simple and clean but not
> a functional interface.   `NewInvocationHandler` is a functional interface
> with the need of `DefaultMethodInvoker` to bridge between an invocation
> handler and an accessed check proxy object.
> 
> Thoughts?  I'm leaning toward `DelegatingInvocationHandler`.  
> 
> We need to decide on the exception if access checks fails on
> `Proxy::newProxyInstance` and  `Proxy::getInvocationHandler` 
> if the invocation handler needs the ability to invoke default
> methods.

Thanks for the update. Lots of challenges finding the right API as there are security and usability issues, not to mention performance and making it look like the support for default methods has always been there.

I've skimmed through both prototypes in proxy-method-default-3 branch, both seem to be secure.

NewInvocationHandler/DefaultMethodInvoker feels a bit awkward. If I have it right, to use it as a lambda expression to Proxy.newProxyInterface would require casting to NewInvocationHandler. I also need to be careful when using the invoker and not call it with a non-default method.

DelegatingInvocationHandler feels more like a base implementation that I extend when I need help invoking default methods. Naming aside, it feels like it fits in better but with the downside that an instance can't be used as a functional interface. So I agree this one is a better. The users of this API will be super advanced developers and I would expect retrofitting existing code shouldn't be too hard for that are compiled to 16+.

-------------

PR: https://git.openjdk.java.net/jdk/pull/313


More information about the core-libs-dev mailing list