覆盖静态创建的对象中的方法

I82*_*uch 6 java overriding worldwind

所有,

由于我正在使用的库中的错误,我需要覆盖扩展某个类的所有对象上的dispose()方法,并使其成为NO-OP.我知道如果我直接创建类的新实例,这很容易做到:

layerManager = new LayerManagerLayer(wwd) {
    @Override
    public void dispose() {}
};
Run Code Online (Sandbox Code Playgroud)

问题是我获得的很多对象实例不是由我的客户端代码直接构造的,而是通过静态库方法调用创建的.

// Here I want to override the dispose method, but I cannot.
Layer l = ShapefileLoader.makeShapefileLayer(this.getClass().getResource("polylines10.shp"));
Run Code Online (Sandbox Code Playgroud)

有没有办法可以将我的dispose方法注入到静态创建的对象中,而无需修改原始源代码?

Bar*_*ski 5

如果客户端编译自己的代码,您可以使用AspectJ注入您的dispose()方法.其他选项是一些字节码修改工具,如cglib.请看这个列表:http: //java-source.net/open-source/bytecode-libraries