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?
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?
Beyond bytecode engineering libraries, if you have an interface for your class, you could use Java's Proxy
class.
With an interface:
public interface Foo {
void bar();
}
The concrete class:
class FooImpl {
public void bar() {
System.out.println("foo bar");
}
}
To process the methods called, you use InvocationHandler
:
class FooInvocationHandler implements InvocationHandler {
private Foo foo;
FooInvocationHandler(Foo foo) {
this.foo = foo;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
if (method.getName().equals("bar"))
System.out.println("foo me");
// return null if you don't want to invoke the method below
}
return method.invoke(foo, args); // Calls the original method
}
}
Then create a FooFactory
to generate and wrap FooImpl
instances using Proxy
:
public class FooFactory {
public static Foo createFoo(...) {
Foo foo = new FooImpl(...);
foo = Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
new FooInvocationHandler(foo));
return foo;
}
}
This would wrap the FooImpl
object so that this:
Foo foo = FooFactory.createFoo(...);
foo.bar();
Prints:
foo me
foo bar
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. (Proxy
is in java.lang.reflect
on everything since 1.3.)
Into a already loaded class? Not to my knowledge.
However, you may be interested in generating a new class. I see two options:
like so:
public static void printTest() {
System.out.println("Hey!");
}
And then taking a look at the bytecode, seeing where in the ConstantPool "Hey!" is stored, then taking the bytecode for the method, caching it into a byte array, then dynamically creating a method with pre-defined bytes + constant pool (that you examined before), and modifying the constantpool with your string. This would get very tricky very fast though for more complicated methods (Note: the same could be done to change what method it calls, or field it gets, like changing out to err)
Since there aren't really any good options as far as I see though, so these are your best bets