3

I want to create an object of a class, and override some of it's methods. eg:

Foo bar = new Foo(){

    public void fighters(){
          //some stuff
    }

};

The problem is: the name of the class is stored in a String. My obvious move was to create a new instance using the Constructor.newInstance(Object o) method, like this:

Class c = cl.loadClass("com.pom.Foo");
Foo f = (Foo) (c.getDeclaredConstructor(String.class).newInstance("Hello!"));

Although this piece of code successfully creates a new instance of the class, I don't know how to override it's methods now.

Any suggestions?

6
  • 2
    Why can't you just create a class which overrides the method, compile it, and instantiate this class instead of Foo?
    – JB Nizet
    Jan 22, 2012 at 13:58
  • Java is a statically-compiled language, you can't do this sort of thing. Wrong tool for the job.
    – skaffman
    Jan 22, 2012 at 13:58
  • @skaffman: that's not quite correct. You can do runtime code injection (with things like BCEL for instance). (Not saying you should, just that you can.)
    – Mat
    Jan 22, 2012 at 14:01
  • @JBNizet You can even do that dynamically if you really, really want. Jan 22, 2012 at 14:01
  • @PeterLawrey: I know that, but I wouldn't go this way if a simple static OO way solves the problem. Without knowing the problem behind the question, I wouldn't advise such a hard solution.
    – JB Nizet
    Jan 22, 2012 at 14:19

1 Answer 1

2

I think you have a few options, none of them nice and all stink to high heaven of an architectural issue with how you're addressing the problem you're presented with.

Delegating Wrapper

Create a DelegaingFoo class that looks like this:

class DelegatingFoo {

    Callable callMe;

    public DelegatingFoo(Callable callMe) {
        this.callMe = callMe;
    }

    public void fighters(){
          calLMe.call();
    }

};

Instanciate this instead, passing in a Callable object into the constructor as you are above. this disjoints the code you want to run from the bit that injects it.

Use a JVM language

Compile to something that can be run through javax.script, such as BeanShell, Groovy, etc. Depending on what you're ultimately doing, this may be a viable option.

Customised Classloader

If you have the option of using an alternative class loader (which presents it's own issues), which is something that would be fraught with it's own issues, and create something really quite complex. If you decide to consider this seriously, then looking at something like the OSGi class loading framework may give you some clues (it may even, at a stretch, be suitable).

Bytecode Manipulation

There are a few libraries which will help with bytecode munging / interception / generation / alteration on the fly:

WARNING

It should be noted that all the above are hacks, with increasing depravity as you go down them. I would get my architecture peer reviewed ASAP as I would put money on there being a cleaner & clearer approach to what you're doing.

Remember, code you write should be easier to read - otherwise you're creating a maintenance headache for yourself (or future project owners).

2
  • Thank you, all. @brainzzy thanks for the detailed reply. The delegating class idea is a really nice one.
    – divs1210
    Jan 22, 2012 at 21:21
  • I still recommend getting a wider review of the full architecture! In any case, good luck. Jan 22, 2012 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.