所以我在玩 geotools,我想我会代理他们的一个数据访问类并跟踪它是如何在他们的代码中使用的。
我编写了一个动态代理并在其中包装了一个 FeatureSource(接口),然后它就愉快地关闭了。然后我还想看看 featureSource 返回的一些传递对象,因为 FeatureSource 所做的主要事情是返回一个 FeatureCollection(FeatureSource 类似于 sql DataSource 和 featurecollection 类似于 sql 语句)。
在我的调用处理程序中,我只是将调用传递给底层对象,打印出目标类/方法/参数和结果,但是对于返回 FeatureCollection(另一个接口)的调用,我将该对象包装在我的代理中(同一个类但是一个新实例,应该没关系吗?)并返回它。砰!类播异常:
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);
}
}
是否可以从代理接口动态返回接口代理,或者我做错了什么?干杯!