对我的代理类中的每个方法执行InvocationHandler调用方法

时间:2019-04-30 11:14:38

标签: java reflection dynamic-proxy invocationhandler

我已经实现了动态代理,以便在我的方法开始之前进行一些操作。 现在从代理类调用两个方法时遇到问题,代码如下:

动态代理类:

public class IPageProxy implements InvocationHandler {

    private Class <? extends IPage> screenClazz;

    public IPageProxy(final Class <? extends IPage> screenClazz) {
        this.screenClazz = screenClazz;
    }

    @SuppressWarnings("unchecked")
    public static <T extends IPage> T getInstance(final Class<? extends IPage> type)
            throws InstantiationException, IllegalAccessException {

        List<Class<?>> interfaces = new ArrayList<>();
        interfaces.addAll(Arrays.asList(type.getInterfaces()));

        return (T) Proxy.newProxyInstance(
                type.getClassLoader(),
                findInterfaces(type),
                new IPageProxy(type)
             );

    }


    static Class<?>[] findInterfaces(final Class<? extends IPage> type) {
        Class<?> current = type;

        do {
            final Class<?>[] interfaces = current.getInterfaces();

            if (interfaces.length != 0) {
                return interfaces;
            }
        } while ((current = current.getSuperclass()) != Object.class);

        throw new UnsupportedOperationException("The type does not implement any interface");
    }





    @Override
    public Object invoke(final Object proxy, final Method method, final Object[] args) throws InvocationTargetException,
            IllegalAccessException, IllegalArgumentException, InstantiationException, ParserConfigurationException, XPathExpressionException, NoSuchFieldException, SecurityException {

        // before method executed this code will be done
        System.out.println("*   Dynamic proxy invoke method executed for " +  method.getName());

        // Invoke original method
        return method.invoke(screenClazz.newInstance(), args);
    }
}

主类:

public static void main(String[] args) {
        try {
            //IEventDesignDialog a = new EventDesignDialog();
            IEventDesignDialog a  = (IEventDesignDialog)getInstance(EventDesignDialog.class);
            a.getEventType().getShow();

        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    @SuppressWarnings("unchecked")
    public static <T extends IPage> T getInstance(final Class<? extends IPage> type) throws InstantiationException, IllegalAccessException {
        return (T) IPageProxy.getInstance(type);
    }

代理课程:

public class EventDesignDialog implements IEventDesignDialog{


        private String show;


        private String dateAndTimeDisplayFormat;
        private String eventType;


        @Entity(visibileName = "Show")
        public IEventDesignDialog getShow() {
            System.out.println("get show method invokde successfully");
            return this;
        }

        @Entity(visibileName = "Date And Time display format")
        public IEventDesignDialog getDateAndTimeDisplayFormat() {
            System.out.println("get date and time display format method invokde successfully");
            return this;
        }

        @Entity(visibileName = "Event Type")
        public IEventDesignDialog getEventType() {
            System.out.println("get event type method invokde successfully");
            return this;
        }



}

实际输出:

***   Dynamic proxy invoke method executed for getEventType
get event type method invokde successfully
get show method invokde successfully**

如图所示,invoke方法仅在初始化代理后的第一个方法调用时执行,而第二个方法直接调用而没有代理功能。

我的目标是每次在我的集合中出现一个方法时都执行invoke方法,预期结果应如下所示。

预期输出:

***   Dynamic proxy invoke method executed for getEventType
get event type method invokde successfully
*   Dynamic proxy invoke method executed for getShow
get show method invokde successfully**

请让我知道是否需要更多说明。

2 个答案:

答案 0 :(得分:0)

我已经通过使用默认方法创建一个返回代理实例的接口来解决此问题,然后在执行调用的方法功能后将其返回:

更新的代码:

public interface IPage {
    default <T extends IPage> T getProxyInstance() {
        try {
            return (T) IPageProxy.getInstance(this.getClass());
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}

我的页面界面:

@Page(path = "MyPath")
public interface IEventDesignDialog extends IPage{
    @Entity(visibileName = "Show")
    public IEventDesignDialog getShow();

    @Entity(visibileName = "Date And Time display format")
    public IEventDesignDialog getDateAndTimeDisplayFormat();

    @Entity(visibileName = "Event Type")
    public IEventDesignDialog getEventType();    
}

我的页面类:

@Page(path = "MyPath")
public class EventDesignDialog implements IEventDesignDialog{
        @Entity(visibileName = "Show")
        public IEventDesignDialog getShow() {
            System.out.println("get show method invokde successfully");
            return getProxyInstance();
        }

        @Entity(visibileName = "Date And Time display format")
        public IEventDesignDialog getDateAndTimeDisplayFormat() {
            System.out.println("get date and time display format method invokde successfully");
            return getProxyInstance();
        }

        @Entity(visibileName = "Event Type")
        public IEventDesignDialog getEventType() {
            System.out.println("get event type method invokde successfully");
            return getProxyInstance();
        }
}

主类:

public class Main {

    public static void main(String[] args) {
        try {
            IEventDesignDialog a  = ((IEventDesignDialog)getInstance(EventDesignDialog.class)).getEventType().getShow();

            ((IShowDesignDialog)getInstance(ShowDesignDialog.class)).getShowName().getShowType();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    @SuppressWarnings("unchecked")
    public static <T extends IPage> T getInstance(final Class<? extends IPage> type) throws InstantiationException, IllegalAccessException {
        return (T) IPageProxy.getInstance(type);
    }

}

IProxy页面保持不变,没有变化。

答案 1 :(得分:0)

发生的事情是,首先只代理第一次调用,然后在非代理类上调用getShow(),这就是为什么要得到您提到的结果的原因。如果要实现您提到的目标,则需要基于创建的实例而不是仅基于类创建另一个代理。

更新: 我将提供示例代码,您可以将其粘贴到任何Java文件中并执行它。 在您看到TODO的地方,可以根据想要提供代理的方式放置自己的逻辑。重要时刻请参见“注意”。为了简化演示,我将所有类都放在一个文件中。

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.List;

class Scratch {
    public static void main(String[] args) {
        try {
            IEventDesignDialog a  = proxy(EventDesignDialog.class);
            a.getEventType().getShow();
            a.getDateAndTimeDisplayFormat().getShow();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    @SuppressWarnings("unchecked")
    private static <T extends IPage> T proxy(final Class<? extends IPage> type) throws InstantiationException, IllegalAccessException {
        return (T) IPageProxy.proxy(type);
    }
}
interface IPage{}
interface IEventDesignDialog extends IPage{
    IEventDesignDialog getShow();
    IEventDesignDialog getEventType();
    IEventDesignDialog getDateAndTimeDisplayFormat();
}
class EventDesignDialog implements IEventDesignDialog{

    public IEventDesignDialog getShow() {
        System.out.println("get show method invoked successfully");

        //NOTE: this will be treated as same proxy but not this
        return this;
    }

    public IEventDesignDialog getDateAndTimeDisplayFormat() {
        System.out.println("get date and time display format method invoked successfully");

        // NOTE: we supply some iinstance which will be proxied
        return new MyIEventDesignDialog();
    }

    public IEventDesignDialog getEventType() {
        System.out.println("get event type method invoked successfully");

        //NOTE: this will be treated as same proxy but not this
        return this;
    }

}
class IPageProxy implements InvocationHandler {

    private IPage instance;
    private List<Class<?>> interfaces;


    public IPageProxy(IPage instance, List<Class<?>> interfaces) {
        this.instance = instance;
        this.interfaces = interfaces;
    }

    @SuppressWarnings("unchecked")
    public static <T extends IPage> T proxy(final Class<? extends IPage> type)
            throws InstantiationException, IllegalAccessException {

        List<Class<?>> interfaces = Arrays.asList(type.getInterfaces());


        //TODO: get interfaces properly recursively
        return (T) Proxy.newProxyInstance(
                type.getClassLoader(),
                type.getInterfaces(),
                new IPageProxy(type.newInstance(), interfaces)
        );

    }

    @SuppressWarnings("unchecked")
    public static <T extends IPage> T proxy(T object) {

        //TODO: get interfaces properly recursively
        List<Class<?>> interfaces = Arrays.asList(object.getClass().getInterfaces());

        return (T) Proxy.newProxyInstance(
                object.getClass().getClassLoader(),
                object.getClass().getInterfaces(),
                new IPageProxy(object, interfaces)
        );

    }




    @Override
    public Object invoke(final Object proxy, final Method method, final Object[] args) throws Exception {

        // before method executed this code will be done
        System.out.println("*   Dynamic proxy invoke method executed for " +  method.getName());

        // Invoke original method
        Object invoke = method.invoke(instance, args);
        if (invoke == null) {
            return null;
        }

        //If some of the method returns the original object
        //we swap the returned object by our proxy
        if (invoke == instance) {
            return proxy;
        }

        //TODO: check if you want to swap in place
        //other interfaces
        if (interfaces.contains(method.getReturnType())) {
            return IPageProxy.proxy((IPage)invoke);
        }
        return invoke;
    }
}

class MyIEventDesignDialog implements IEventDesignDialog {
    @Override
    public IEventDesignDialog getShow() {
        return null;
    }

    @Override
    public IEventDesignDialog getEventType() {
        return null;
    }

    @Override
    public IEventDesignDialog getDateAndTimeDisplayFormat() {
        return null;
    }
}

输出:

*   Dynamic proxy invoke method executed for getEventType
get event type method invoked successfully
*   Dynamic proxy invoke method executed for getShow
get show method invoked successfully
*   Dynamic proxy invoke method executed for getDateAndTimeDisplayFormat
get date and time display format method invoked successfully
*   Dynamic proxy invoke method executed for getShow

您可以从Mockito的工作方式中获得一些想法。请检查以下页面:https://static.javadoc.io/org.mockito/mockito-core/2.27.0/org/mockito/Mockito.html#spy-T-

我知道它是用于测试的,但是您仍然可以从中获得想法。 因此,您可以在类和对象上应用spy()进行监视。