Possible Duplicate:
Can a Java class add a method to itself at runtime?
Is it possible to synthesize a new method at runtime for a class in Java? Is there a library to make that possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Beyond bytecode engineering libraries, if you have an interface for your class, you could use Java’s
Proxyclass.With an interface:
The concrete class:
To process the methods called, you use
InvocationHandler:Then create a
FooFactoryto generate and wrapFooImplinstances usingProxy:This would wrap the
FooImplobject so that this:Prints:
This is an alternative to the BCEL libraries, which can do this and a lot more, including generating classes from runtime information, but the BCEL libraries aren’t native. (
Proxyis injava.lang.reflecton everything since 1.3.)