| Refresh | Home EGTry.com

get and set field value


Get a static field value

mv.visitFieldInsn(GETSTATIC, 
   "sample/FieldReadWrite", // classname 
   "len",  //static field name
   "I"); // the type of field.
//return a value in the stack


assign to a static field

mv.visitIntInsn(BIPUSH, 10); // put a constant integer onto the stack

mv.visitFieldInsn(PUTSTATIC, 
  "sample/FieldReadWrite", //classname
  "len", // field name
  "I"); // field type


get the value of an instance field

mv.visitVarInsn(ALOAD, 0); //from object onto the stack
mv.visitFieldInsn(GETFIELD, 
  "sample/FieldReadWrite", //the type of object
  "name", // field name 
 "Ljava/lang/String;" // field type
);
mv.visitInsn(ARETURN); // pop the top value of the stack and return.


set the value of an instance field

mv.visitVarInsn(ALOAD, 0);//push the object 
mv.visitVarInsn(ALOAD, 1);//push the value from which assign to the field
mv.visitFieldInsn(PUTFIELD, 
"sample/FieldReadWrite", // the class
"name", // the field name
"Ljava/lang/String;"//the field type
);