gpt4 book ai didi

Java.lang.reflect.Proxy 从调用返回另一个代理导致赋值时出现 ClassCastException

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:16:50 25 4
gpt4 key购买 nike

所以我在玩 geotools,我想我应该代理他们的一个数据访问类,并跟踪它是如何在他们的代码中使用的。

我编写了一个动态代理,并在其中包装了一个 FeatureSource(接口(interface)),然后一切顺利。然后我还想看看 featureSource 返回的一些传递对象,因为 FeatureSource 所做的主要事情是返回一个 FeatureCollection(FeatureSource 类似于 sql 数据源,featurecollection 类似于 sql 语句)。

在我的 invocationhandler 中,我只是将调用传递给底层对象,打印出目标类/方法/args 和结果,但是对于返回 FeatureCollection(另一个接口(interface))的调用,我将该对象包装在我的代理(同一个类但是一个新实例,应该无关紧要吧?)并返回它。砰!类播异常:

java.lang.ClassCastException: $Proxy5 cannot be cast to org.geotools.feature.FeatureCollection  
at $Proxy4.getFeatures(Unknown Source)
at MyClass.myTestMethod(MyClass.java:295)

调用代码:

FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = ... // create the FS
featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) FeatureSourceProxy.newInstance(featureSource, features);
featureSource.getBounds();// ok
featureSource.getSupportedHints();// ok

DefaultQuery query1 = new DefaultQuery(DefaultQuery.ALL);
FeatureCollection<SimpleFeatureType, SimpleFeature> results = featureSource.getFeatures(query1); //<- explosion here

代理:

public class FeatureSourceProxy  implements java.lang.reflect.InvocationHandler {

private Object target;
private List<SimpleFeature> features;

public static Object newInstance(Object obj, List<SimpleFeature> features) {
return java.lang.reflect.Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(),
new FeatureSourceProxy(obj, features)
);
}

private FeatureSourceProxy(Object obj, List<SimpleFeature> features) {
this.target = obj;
this.features = features;
}

public Object invoke(Object proxy, Method m, Object[] args)throws Throwable{
Object result = null;
try {
if("getFeatures".equals(m.getName())){
result = interceptGetFeatures(m, args);
}
else{
result = m.invoke(target, args);
}
}
catch (Exception e) {
throw new RuntimeException("unexpected invocation exception: " + e.getMessage(), e);
}
return result;
}

private Object interceptGetFeatures(Method m, Object[] args) throws Exception{
return newInstance(m.invoke(target, args), features);
}

是否可以从代理接口(interface) 动态返回接口(interface)代理,或者我做错了什么?干杯!

最佳答案

Class.getInterfaces() 只返回类直接实现的接口(interface)。您需要一个传递闭包来获取所有接口(interface)。

更新

例子:

private static Class<?>[] getInterfaces(Class<?> c) {
List<Class<?>> result = new ArrayList<Class<?>>();
if (c.isInterface()) {
result.add(c);
} else {
do {
addInterfaces(c, result);
c = c.getSuperclass();
} while (c != null);
}
for (int i = 0; i < result.size(); ++i) {
addInterfaces(result.get(i), result);
}
return result.toArray(new Class<?>[result.size()]);
}

private static void addInterfaces(Class<?> c, List<Class<?>> list) {
for (Class<?> intf: c.getInterfaces()) {
if (!list.contains(intf)) {
list.add(intf);
}
}
}

您可能还需要“解包”作为参数传递的代理。

关于Java.lang.reflect.Proxy 从调用返回另一个代理导致赋值时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2642700/

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