Java Reflection Proxy create instance
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; interface Adder { int add(int a, int b); } class MyObject implements Adder { public int add(int a, int b) { System.out.println("In add(): received: " + a + " and " + b); int result = a + b; System.out.println("In add(): Sent: " + result); return result; }// ww w . java 2s .c o m } class MyHandler implements InvocationHandler { Object target; public MyHandler(Object t) { target = t; } public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { double start = System.nanoTime(); Object o = m.invoke(target, args); double ellapseTime = System.nanoTime() - start; System.out.println(m.getName() + "() took " + ellapseTime + " ns"); return o; } } public class Main { public static void main(String args[]) throws Exception { MyObject o = new MyObject(); Adder s = (Adder) Proxy.newProxyInstance(o.getClass().getClassLoader(), o.getClass().getInterfaces(), new MyHandler(o)); int x = 4, y = 3; int result = s.add(x, y); System.out.println("In caller: sent: " + x + " and " + y); System.out.print("In caller: received(" + x + "+" + y + "=): " + result); } }