Hi!
I want to overwrite a simple bean like this
public class SomeBean {
private String name;
public String getName() { return name; }
}
and add some behavior at runtime:
public class SomeBeanImpl {
public String getName() { System.out.println("getName() is called");
return name; }
}
I want to overwrite the getName method, add some action (in this exampke
write some
text to the console) and then call the super method.
When I decompile my Class I get this
public static String getName()
{
return <returnValue>.SimpleBean.getName();
}
This is my foolish code:
<------------------ >%
------------------------------------------------------------------->
ClassGen cg = new ClassGen("SomeBeanImpl", "SomeBean",
"<generated>", ACC_PUBLIC | ACC_SUPER,
null);
ConstantPoolGen cp = cg.getConstantPool(); // cg creates constant pool
InstructionList il = new InstructionList();
MethodGen mg = new MethodGen(ACC_STATIC | ACC_PUBLIC, // access flags
Type.STRING, // return type
null, // argument types
null, // arg names
"getName", "SomeBeanImpl", // method,
class
il, cp);
InstructionFactory factory = new InstructionFactory(cg);
ObjectType i_stream = new ObjectType("java.io.InputStream");
ObjectType p_stream = new ObjectType("java.io.PrintStream");
il.append(factory.createFieldAccess("java.lang.System", "out", p_stream,
Constants.GETSTATIC));
il.append(new PUSH(cp, "getName() was called"));
il.append(factory.createInvoke("java.io.PrintStream", "print",
Type.VOID,
new Type[] { Type.STRING },
Constants.INVOKEVIRTUAL));
il.append(new ALOAD(0));
il.append(factory.createInvoke("SimpleBean", "getName", Type.STRING,
Type.NO_ARGS, Constants.INVOKESPECIAL));
il.append(InstructionConstants.ARETURN);
mg.setMaxStack();
cg.addMethod(mg.getMethod());
il.dispose(); // Allow instruction handles to be reused
cg.addEmptyConstructor(ACC_PUBLIC);
try {
cg.getJavaClass().dump("SomeBeanImpl.class");
} catch (Exception e) { e.printStackTrace(); }
<------------------ >%
------------------------------------------------------------------->
What am I doing wrong???
Thanx in advance
Marcus Lankenau
--
+++ GMX - Mail, Messaging & more http://www.gmx.net +++
Bitte l�cheln! Fotogalerie online mit GMX ohne eigene Homepage!
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]