It's not a secret anymore!*
The moose likes Java in General and the fly likes Inherit/modify class at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inherit/modify class at runtime" Watch "Inherit/modify class at runtime" New topic
Author

Inherit/modify class at runtime

Matti Poro
Ranch Hand

Joined: Dec 03, 2005
Posts: 41


If I load a class that way, is it possible to replace (override) methods in the created object?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

No. It is possible to do it before you load the class using a byte-code manipulation library like BCEL


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

On the other hand, I think I was answering the question you asked, rather than solving the problem you actually have. You can create a proxy object for the object you load, which implements the same interface and/or extends the same base class as your "target" object. In this proxy class, you can implement most of the methods to simply "forward" the call to the target, while others might add code; i.e.,

 
 
subject: Inherit/modify class at runtime