Sunday 29 September 2013

The Power of Proxies in Java | Javalobby

From Evernote:

The Power of Proxies in Java | Javalobby

Clipped from: http://java.dzone.com/articles/power-proxies-java

The Power of Proxies in Java

05.10.2010
| 46633 views |
In this article, I'll show you the path that leads to true Java power, the use of proxies.
They are everywhere but only a handful of people know about them. Hibernate for lazy loading entities, Spring for AOP, LambdaJ for DSL, only to name a few: they all use their hidden magic. What are they? They are… Java's dynamic proxies.
Everyone knows about the GOF Proxy design pattern:
Allows for object level access control by acting as a pass through entity or a placeholder object.
Likewise, in Java, a dynamic proxy is an instance that acts as apass through to the real object. This powerful pattern let you changethe real behaviour from a caller point of view since method calls canbe intercepted by the proxy.

Pure Java proxies

Pure Java proxies have some interesting properties:
  • They are based on runtime implementations of interfaces
  • They are public, final and not abstract
  • They extend java.lang.reflect.Proxy
In Java, the proxy itself is not as important as the proxy's behaviour. The latter is done in an implementation of java.lang.reflect.InvocationHandler . It has only a single method to implement:
public Object invoke(Object proxy, Method method, Object[] args)
  • proxy: the proxy instance that the method was invoked on
  • method: the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Methodobject will be the interface that the method was declared in, which maybe a superinterface of the proxy interface that the proxy classinherits the method through
  • args: an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or nullif interface method takes no arguments. Arguments of primitive typesare wrapped in instances of the appropriate primitive wrapper class,such as java.lang.Integer or java.lang.Boolean
Let's take a simple example: suppose we want a List that can't be added elements to it. The first step is to create the invocation handler:
01.public class NoOpAddInvocationHandler implements InvocationHandler {
02. 
03.private final List proxied;
04. 
05.public NoOpAddInvocationHandler(List proxied) {
06. 
07.this.proxied = proxied;
08.}
09. 
10.public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
11. 
12.if (method.getName().startsWith("add")) {
13. 
14.return false;
15.}
16. 
17.return method.invoke(proxied, args);
18.}
19.}
The invoke method will intercept method calls and donothing if the method starts with "add". Otherwise, it will the callpass to the real proxied object. This is a very crude example but isenough to let us understand the magic behind.
Notice that in case you want your method call to pass through, youneed to call the method on the real object. For this, you'll need areference to the latter, something the invoke method does not provide. That's why in most cases, it's a good idea to pass it to the constructor and store it as an attribute.
Note: under no circumstances should you call the method on the proxyitself since it will be intercepted again by the invocation handler andyou will be faced with a StackOverflowError.
To create the proxy itself:
1.List proxy = (List) Proxy.newProxyInstance(
2.NoOpAddInvocationHandlerTest.class.getClassLoader(),
3.new Class[] { List.class },
4.new NoOpAddInvocationHandler(list));
The newProxyInstance method takes 3 arguments:
  • the class loader
  • an array of interfaces that will be implemented by the proxy
  • the power behind the throne in the form of the invocation handler
Now, if you try to add elements to the proxy by calling any add methods, it won't have any effect.

CGLib proxies

Java proxies are runtime implementations of interfaces. Objects donot necessarily implement interfaces, and collections of objects do notnecessarily share the same interfaces. Confronted with such needs, Javaproxies fail to provide an answser.
Here begins the realm of CGLib . CGlib is a third-party framework, based on bytecode manipulation provided by ASM that can help with the previous limitations. A word of advice first,CGLib's documentation is not on par with its features: there's notutorial nor documentation. A handful of JavaDocs is all you can counton. This said CGLib waives many limitations enforced by pure Javaproxies:
  • you are not required to implement interfaces
  • you can extend a class
For example, since Hibernate entities are POJO, Java proxies cannot be used in lazy-loading; CGLib proxies can.
There are matches between pure Java proxies and CGLib proxies: where you use Proxy, you use net.sf.cglib.proxy.Enhancer class, where you use InvocationHandler, you use net.sf.cglib.proxy.Callback. The two main differences is that Enhancer has a public constructor and Callback cannot be used as such but only through one of its subinterfaces:
  • Dispatcher: Dispatching Enhancer callback
  • FixedValue: Enhancer callback that simply returns the value to return from the proxied method
  • LazyLoader: Lazy-loading Enhancer callback
  • MethodInterceptor: General-purpose Enhancer callback which provides for "around advice"
  • NoOp: Methods using this Enhancer callback will delegate directly to the default (super) implementation in the base class
As an introductory example, let's create a proxy that returns thesame value for hash code whatever the real object behind. The featurelooks like a MethodInterceptor, so let's implement it as such:
01.<public class HashCodeAlwaysZeroMethodInterceptor implements MethodInterceptor {
02. 
03.public Object intercept(Object object, Method method, Object[] args,
04.MethodProxy methodProxy) throws Throwable {
05. 
06.if ("hashCode".equals(method.getName())) {
07. 
08.return 0;
09.}
10. 
11.return methodProxy.invokeSuper(object, args);
12.}
13.}
Looks awfully similar to a Java invocation handler, doesn't it? Now, in order to create the proxy itself:
1.Object proxy = Enhancer.create(
2.Object.class,
3.new HashCodeAlwaysZeroMethodInterceptor());
Likewise, the proxy creation isn't suprising. The real differences are:
  • there's no interface involved in the process
  • the proxy creation process also creates the proxied object. There'sno clear cut between proxy and proxied from the caller point of view
  • thus, the callback method can provide the proxied object and there's no need to create and store it in your own code

Conclusion

This article only brushed the surface of what can be done withproxies. Anyway, I hope it let you see that Java has some interestingfeatures and points of extension, whether out-of-the-box or coming fromsome third-party framework
You can find the sources for this article in Eclipse/Maven format here .
From http://blog.frankel.ch/the-power-of-proxies-in-java

No comments:

Post a Comment