User development,
A new message was posted in the thread "cannot access added Field in some cases.
Please help.":
http://community.jboss.org/message/524341#524341
Author : Arvind K
Profile :
http://community.jboss.org/people/megalodon
Message:
--------------------------------------------------------------
Hello Jaikiran,
I do not think that the class with the issue has already been loaded. In my first post, I
mentioned that I am using java.lang.instrument API to do the loading and transformation
initiation for me. The ClassFileTransformer hands me the bytecode of the class and I
convert that into a CtClass object using CtClass.makeClassIfNew() and perform my
transformations. I then hand over the modified bytecode back to the classfiletransformer
to complete the loading process.
You are right that I am not in a position to post all of my code as-is mainly because I do
not know where the issue(s) lies and that it might be voluminous if I post everything and
of course I do not own it to post it but I would be happy to provice snippets of some
main parts for clarity. Here is the method in question where I do my simple modifications.
I call this method for every method of the class I am instrumenting but add the int field
'abc' to the class while instrumenting the first method only.
protected void prefixMethod(CtMethod myCtM, String methodName) {
try {
if (fieldsAdded == false)
{
CtField tempCtF = new CtField(CtClass.intType, "abc",this.CtC); //myCtM
is a method in this.CtC
tempCtF.setModifiers(Modifier.PUBLIC);
this.CtC.addField(tempCtF,"10");
displayFields(false); // displays all the fields in this.CtC. Field
'abc' shows up here always.
Log2File.write("Added Fields");
// this.CtC.rebuildClassFile(); // i thought this function might solve the issue
but it did not
tempCtF = this.CtC.getField("abc"); // just a precaution
}
myCtM.insertBefore("System.out.println(\"In " + this.className +
"." + methodName + ". abc is \" + this.abc);");
} catch (CannotCompileException cce) {
System.out.println("prefixMethod for " + className + " method
name " + methodName + ". Exception: " + cce);
cce.printStackTrace();
Log2File.writeErr(cce,"prefixMethod for " + className);
}
catch (NotFoundException ntfnde) {
System.out.println("prefixMethod for " + className + " method
name " + methodName + ". Exception: " + ntfnde);
ntfnde.printStackTrace();
Log2File.writeErr(ntfnde,"prefixMethod for " + className);
}
}
*Another twist is that, it works for a few methods in a class but not others in the same
class*. I do not know what made the difference there. I have a suspision that it works for
non-empty methods and not empty ones but I am not sure. I tried skipping the empty ones
with CtMethod.isEmpty() but I couldn't seem to skip them.
Appreciate your help so far.
-Arvind
--------------------------------------------------------------
To reply to this message visit the message page:
http://community.jboss.org/message/524341#524341