[jboss-user] [Javassist] New message: "cannot access added Field in some cases. Please help."

Arvind K do-not-reply at jboss.com
Thu Feb 4 02:42:27 EST 2010


User development,

A new message was posted in the thread "cannot access added Field in some cases. Please help.":

http://community.jboss.org/message/524066#524066

Author  : Arvind K
Profile : http://community.jboss.org/people/megalodon

Message:
--------------------------------------------------------------
Hello,
 
*Some background regarding my issue*:
I have obtained the byteArray of the class using a ClassFileTransformer added to an Instrumentation object. I am then using the
ClassPool.makeClassIfNew() 

function to create the corresponding CtClass object.
 
*Issue*:
I am creating a simple int field using
     myCtField = CtField.new(CtClass.intType, "myInt", myCtClassObj) 

 
and then adding theCtField to the CtClass using
myCtClassObj.addField(myCtField);

 
Now I am adding access to the field, within a method in the class represented by the CtMethod object myCtM, using a simple statement like,
myCtM.insertBefore("myInt = 10;");               

 
I am then using CtClass.toBytecode() to get the instrumented bytecode which I am sending back to the ClassFileTransformer to be loaded. Now, When the class gets instantiated and 'executed', I would get a *CannotCompileException* as follows:
javassist.CannotCompileException: [source error] no such field:  myInt

 
*HOWEVER*,
when I perform the same instrumentations such as above in a simpler Javassist example and then use CtClass.toClass(), create a new class and a new instance of it using the newInstance() function and execute the modified method, I am able to access the newly added field just fine!
 
*What am I missing here?* Please provide your thoughts on this. Thanks!
 
-Arvind

--------------------------------------------------------------

To reply to this message visit the message page: http://community.jboss.org/message/524066#524066




More information about the jboss-user mailing list