返回顶部

收藏

asm 设置读取字段值

更多

获得静态字段的值:

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

给静态字段赋值:

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

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

获得实例字段的值:

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.

设置实例字段的值

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
);

标签:asm,字段,java,字节码

收藏

0人收藏

支持

0

反对

0

发表评论