import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class TestProxy {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestModel tm=new TestModel();
TestProxy1 tp=new TestProxy1(tm);
Class<?> c=tp.getClass();
TestInterFace tif=(TestInterFace)Proxy.newProxyInstance(c.getClassLoader(),tm.getClass().getInterfaces(),tp);
tif.add();
}
}
class TestProxy1 implements InvocationHandler
{
private Object o;
public TestProxy1(Object o) {
this.o = o;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
method.invoke(o, args);
return null;
}
}
class TestModel implements TestInterFace
{
public void add()
{
System.out.println("hello");
}
}
interface TestInterFace
{
public void add();
} |