Proxying una clase de Java con una conversión de clase interna

Here's a problem I am facing.

I am writing a plugin. There is an interface called SystemObject, and a default getter.

public class MyPlugin extends Plugin {
        public SystemObject getSystemObject() {
                return super.getSystemObject();

SystemObject interface has a method called getScreenSize() which I would like to proxy or intercept. When I create a proxy class, or simply implement this SystemObject interface myself, I get a class cast exception.

This is because the caller for getSystemObject (part of the plugin system) has this in their code (found via reverse-engineering):

private void foo() { 
     SystemObjectImpl impl = (SystemObjectImpl)plugin.getSystemObject(); 

Mi pregunta es: is there any way I can proxy calls on the SystemObject interface?

I tried implementing the interface and using java reflection proxy invocation to no avail. Unfortunately, I'm not responsible for running the java process, so I can't use an agent.


preguntado el 28 de agosto de 12 a las 10:08

1 Respuestas

Podrías usar algo como CGLIB to create a proxy class that extends SystemObjectImpl.

Respondido 28 ago 12, 11:08

but it creates a new instance, right? what if the caller in foo() uses private members set in the original instance? - Gilm

It seems like a design flaw in the plugin system if it makes it possible for you to provide a custom implementation of the SystemObject interface but then depends internally on the default implementation... All you can do is try it and see what happens. The point I was trying to make is simply that CGLIB would let you create a proxy object that is an instanceof SystemObjectImpl, so that the cast in foo() no falla - Ian Roberts

true. it's a very bad design. I couldn't find another solution. thank you for your answer! - Gilm

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.