Close

Java - InvocationHandler Examples

Java 

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class MyInvocationHandler implements InvocationHandler {

@Override
public Object invoke (Object proxy, Method method, Object[] args) throws Throwable {

Arrays.stream(Thread.currentThread()
.getStackTrace())
.forEach(System.out::println);
System.out.println(method);


System.out.println("the invoked method: " + method);
return null;
}

public static void main (String[] args) {
MyInvocationHandler handler = new MyInvocationHandler();

MyInterface o = (MyInterface) Proxy.newProxyInstance(
MyInvocationHandler.class.getClassLoader(),
new Class[]{MyInterface.class}, handler);
o.doSomething();
}
}
Original Post




import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class MyInvocationHandler implements InvocationHandler {

private String theString;

public MyInvocationHandler (String theString) {
this.theString = theString;
}

@Override
public Object invoke (Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before method call : " + method.getName());
Object result = method.invoke(theString, args);
System.out.println("after method call : " + method.getName());
return result;
}

public static void main (String[] args) {

MyInvocationHandler handler = new MyInvocationHandler("the example string");

CharSequence o = (CharSequence) Proxy.newProxyInstance(
MyInvocationHandler.class.getClassLoader(),
new Class[]{CharSequence.class}, handler);
System.out.println(o.length());
System.out.println(o.subSequence(4, 8));
}
}
Original Post




Implementing Decorator pattern using JDK dynamic proxy.

public class GenericCacheDecorator implements InvocationHandler {

private Map<String, Object> cachedData = new HashMap<>();
private Object EMPTY = new Object();
private Object obj;

private GenericCacheDecorator (Object obj) {
this.obj = obj;
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
cachedData.put(desc.getReadMethod()
.getName(), EMPTY);
}

} catch (IntrospectionException e) {
e.printStackTrace();
}
}

public static <I, T extends I> I decorate (T t, Class<I> interfaceClass) {
GenericCacheDecorator cacheableDecorator = new GenericCacheDecorator(t);
return (I) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
new Class[]{interfaceClass}, cacheableDecorator);

}

public Object invoke (Object proxy, Method method, Object[] args) throws Throwable {
if (cachedData.containsKey(method.getName())) {
Object o = cachedData.get(method.getName());
if (o == EMPTY) {
Object returned = method.invoke(obj, args);
cachedData.put(method.getName(), returned);
return returned;
} else {
return o;
}
}
return method.invoke(args);
}

public static void main (String[] args) {
MyObject object = new MyObject();
IObject iObject = GenericCacheDecorator.decorate(object, IObject.class);
System.out.println(iObject.getData());
System.out.println(iObject.getData());
}
}
Original Post




See Also