mercredi 24 novembre 2021

How to make the JVM proxy the only one interface method?

public interface Action {
    void doSomething();
    void dontProxy();
}

For example with this interface, I just want the JVM to proxy the doSomething method.

class DynamicProxy implements InvocationHandler{
    private Action work;
    public DynamicProxy(Action action){
        this.work = action;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(method.getName()+" start");
        var tmp = method.invoke(work,args);
        System.out.println(method.getName()+" end");
        return tmp;
    }

Action action = (Action) Proxy.newProxyInstance(handler.getClass().getClassLoader(),work.getClass().getInterfaces(),handler);

The work is an instance of the Action interface implementation class. If I use Proxy.newProxyInstance, it seems to make all the interface methods be handled by the handler.

Aucun commentaire:

Enregistrer un commentaire