gpt4 book ai didi

java - 为我的代理类中的每个方法执行 InvocableHandler 调用方法

转载 作者:行者123 更新时间:2023-12-02 06:09:38 27 4
gpt4 key购买 nike

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

动态代理类:

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**

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

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

预期输出:

***   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**

如果需要更多解释,请告诉我。

最佳答案

我通过创建一个带有默认方法的接口(interface)来解决这个问题,该方法返回代理实例,然后在执行调用的方法功能后返回它:

更新的代码:

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 页面保持不变,无需更改。

关于java - 为我的代理类中的每个方法执行 InvocableHandler 调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55919710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com