Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Proxy sample code with java.lang.reflect.Proxy and CGLib
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class CGLibProxySample {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List<String> ary = new ArrayList<String>();
ary.add("Hello");
ary.add("Proxy");
ary.add("World!!");
log("create a interface proxy");
List<String> proxyAry = (List<String>)Enhancer.create(List.class, new MyInvocationHandler(ary));
for (int i = 0; i < 4; i++) { log(proxyAry.get(i)); }
log("create a class proxy");
proxyAry = (List<String>)Enhancer.create(ArrayList.class, new MyInvocationHandler(ary));
for (int i = 0; i < 4; i++) { log(proxyAry.get(i)); }
}
static class MyInvocationHandler implements MethodInterceptor {
private List<String> ary;
public MyInvocationHandler(List<String> ary) {
this.ary = ary;
}
/**
* @param obj "this", the enhanced object
* @param method intercepted Method
* @param args argument array; primitive types are wrapped
* @param proxy used to invoke super (non-intercepted method); may be called
* as many times as needed
*/
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if (isFouthGet(method, args)) {
return "Bow!!";
}
return proxy.invoke(ary, args);
}
private boolean isFouthGet(Method method, Object[] args) {
return "get".equals(method.getName()) && ((Integer)args[0]) == 3;
}
}
private static void log(Object msg) {
System.out.println(msg);
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
public class ProxySample {
public static void main(String[] args) {
List<String> ary = new ArrayList<String>();
ary.add("Hello");
ary.add("Proxy");
ary.add("World!!");
ClassLoader loader = ProxySample.class.getClassLoader();
@SuppressWarnings("unchecked")
List<String> proxyAry = (List<String>) Proxy.newProxyInstance(loader, new Class<?>[]{List.class}, new MyInvocationHandler(ary));
for (int i = 0; i < 4; i++) {
log(proxyAry.get(i));
}
}
static class MyInvocationHandler implements InvocationHandler {
private List<String> ary;
public MyInvocationHandler(List<String> ary) {
this.ary = ary;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (isFouthGet(method, args)) {
return "Bow!!";
}
return method.invoke(ary, args);
}
private boolean isFouthGet(Method method, Object[] args) {
return "get".equals(method.getName()) && ((Integer)args[0]) == 3;
}
}
private static void log(Object msg) {
System.out.println(msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.