Dynamic Proxies in Java   Leave a comment

In this post I shall explain how you can create proxies in java:

Consider a class JavaProxyTest that has a method proxyTestMethod that we want to proxy and this class implements an interface IJavaProxyTest. Here is the code:

package com.salil.aspects.javaproxy;

public class JavaProxyTest implements IJavaProxyTest{

	public void proxyTestMethod(){
		System.out.println("This is the proxyTestMethod");
	}

}
package com.salil.aspects.javaproxy;

public interface IJavaProxyTest {
	public void proxyTestMethod();
}

To create a dynamic proxy is quite simple. Define an instance of the java.lang.reflect.InvocationHandler interface and override the invoke() method. In the invoke() method implement your custom logic that will be executed. For eg. in the below example I am printing out some information about the proxy class and method invoked before invoking the actual method. You can just not call the original method called or call a completely different method of some other class.

package com.salil.aspects.javaproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class JavaProxyDecorator implements InvocationHandler{

	IJavaProxyTest target;

	public JavaProxyDecorator(JavaProxyTest javaProxyTest) {
		this.target=javaProxyTest;
	}

	@Override
	public Object invoke(Object proxy, Method methodInvoked, Object[] methodArgs)
			throws Throwable {
		System.out.println("Proxy class Canonical Name:" + proxy.getClass().getCanonicalName());
		System.out.println("Proxy class Simple Name:" + proxy.getClass().getSimpleName());
		System.out.println("Proxy class Name:" + proxy.getClass().getName());
		System.out.println("Method name : " + methodInvoked.getName());
		System.out.println("Method Modifiers : " + methodInvoked.getModifiers());
		System.out.println("Is Synthetic : " + methodInvoked.isSynthetic());
		System.out.println("Is Bridge : " + methodInvoked.isBridge());
		return methodInvoked.invoke(target, methodArgs);
	}
}

Now go ahead and create the proxy object using the Proxy.newProxyInstance() method. For eg. in the class below I have passed the interface to be proxied(IJavaProxyTest) and the proxying class(JavaProxyDecorator). This method returns the interface to the proxied object that can then be used in your application and each time any method of this proxied instance is called the proxy method will be called executing your custom logic. Go ahead and execute the JavaProxyMain class and you will see the logic in JavaProxyDecorator.invoke() method being executed finally leading to the execution of the JavaProxyTest.proxyTestMethod().

package com.salil.aspects.javaproxy;

import java.lang.reflect.Proxy;

public class JavaProxyMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		IJavaProxyTest jpt= (IJavaProxyTest) Proxy.newProxyInstance(
				 
				       JavaProxyTest.class.getClassLoader(),
				 
				       new Class[] { IJavaProxyTest.class },
				 
				       new JavaProxyDecorator(new JavaProxyTest()));
		jpt.proxyTestMethod();


	}

}

Posted June 25, 2013 by salilsurendran in Uncategorized

Follow

Get every new post delivered to your Inbox.

Join 449 other followers