samedi 24 février 2018

Creating dynamic proxy on yet unknown subclasses

Let's say I have a class I don't own:

public class Base implements A,B,C {...}

Now, let's suppose that my framework allows injection of proxy Base classes like that:

@Unique //example annotation
@Inject
public Base someField; // results in a proxy that checks upon every 
                       // method call if Base "object" is still unique in some context

So far works good, but now a user that is using my library created a class:

public class Subclass extends Base {
    public void freeFun() {...}
}

And used this class for injection like:

@Inject
public Subclass someField; 

Now I'm sad. That not-very-nice user created a function in his subclass that is not an implementation of an interface method. I can still inject it as pure object thanks to reflection, but I can't create a proxy because these require interfaces to implement.

At this point, I don't really need to include his free method in the proxy handling, but I would still love to create a proxy that can be injected into such field.

Would something like that be possible?





Aucun commentaire:

Enregistrer un commentaire