Handling default methods with dynamic proxies in java
Java has a very powerful feature to create dynamic proxies for an object implementing a given set of interfaces at runtime. This can be used to modify some implementations or adding some aspects the the object, like logging, access checks or caching. The dynamic proxy pattern is used by a number of popular libraries to implement the core of their functionality. Some well-known libraries in this regard are:
- spring (for its aspect oriented features)
- retrofit
There is a runtime cost associated with the use of dynamic proxies, but it is usually not that bad, several benachmarks have been performed to prove their suitability for practical use-cases:
As a rule of thumb and based on my own experiments a dynamic proxy impacts performance such that an invocation of a proxied method is roughly 1.6 times slower compared to direct invocation of the same method. Which is not too bad when you consider what happens under the hood to make this working and comparing it with the performance of general reflection calls.
Let’s get to some code. In order to create a dynamic proxy for a given interface, one has to code the following:
This will create a dynamic proxy for the interface MyInterface and delegate all the method calls it receives to the Object delegate. To make this work, the delegate object must be able to process the delegated calls, i.e. it must implement the requested interface.
Now we do not want to go into more details of dynamic proxies and what you can do with them, but rather take a look into a problem introduced with some new feature in Java 8. This version of java added support for default methods in interfaces. But what has this to do with dynamic proxies? If you take the example from above and use it with an interface containing default methods, you will get a runtime exception like this:
This happens because the default method will be delegated to the actual object which usually does not have an implementation for this method (unless it has overridden it of course). In order to support such interfaces with dynamic proxies there is a workaround available for Java 8:
This will access a private constructor of the MethodHandles.Lookup class via reflection and use it to get a MethodHandle object for the default method, bind it to the actual proxy instance and invoke the method. A bit hacky but the only way to support that in Java 8.
Unfortunately, this approach fails with Java 9, but the good news is that there is supported API that we can use for the same purpose and does not required the use of reflection:
This is a bit more complex but it is supported with all versions since Java 9+. To wrap things up we have to modify our initial code for a delegating proxy to something like that:
The class DefaultMethodHandler wraps up the different mechanisms to get a method handle for a default method depending on the current JVM, the code can be found here. It is a derived work from the spring framework, released under Apache v2.0 license.
Taking default methods correctly into account opens up a lot of possibilities for creating dynamic proxies. In my own project Units of Measurement I use proxies to create concrete quantity implementations for different datatypes (double, BigDecimal) at runtime. This is a very flexible approach which can also easily be extended in the future with only a minimal performance impact.
Comments
No comments found for this article.
Join the discussion for this article on this ticket. Comments appear on this page instantly.