// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFun {
    public static void main(String[] args) {
        Test orig = new TestImpl();
        orig.dummyOutput();
        Test tst = (Test) Proxy.newProxyInstance(orig.getClass().getClassLoader(),
                                                 orig.getClass().getInterfaces(),
                                                 new AOP(orig));
        tst.dummyOutput();
    }

}

interface Test {
    public void dummyOutput();
}

class TestImpl implements Test {
    public void dummyOutput() {
        System.out.println( "dummy output" );
    }
}

class AOP implements InvocationHandler {
    private Object obj;
    public AOP(Object o) {
        this.obj = o;
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println( "trace -> run method: " + method.getName());
        return method.invoke(obj, args);
    }
}
