Get java.lang.IllegalAccessError when accessing the private field of a outside class via ASM Java Bytecode?

You can't do it like that. The setAccessible(true) will only affect the current field-reference in the current execution of your program (that is it will not affect the execution of the resulting modified program ).

You can't do it like that. The setAccessible(true) will only affect the current field-reference in the current execution of your program (that is, it will not affect the execution of the resulting modified program). To access a private field when running your modified program, you basically have to embed the corresponding reflection-steps into the program.To access a private field YourClass.

ThePrivatefield of some object stored in local variable varId you do something like // Get hold of the field-reference mv. VisitLdcInsn(Type. GetType("LYourClass;")); mv.

VisitLdcInsn("thePrivateField"); mv. VisitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;"); // Duplicate the reference mv. VisitInsn(DUP); // Call setAccessible(true) using the first reference.

Mv. VisitInsn(ICONST_1); mv. VisitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Field", "setAccessible", "(Z)V"); // Call get(yourObject) using the second reference to the field.

Mv. VisitInsn(ALOAD, varId); mv. VisitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Field", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); If the field you're trying to make accessible is part of the cobe base that your'e rewriting, you could obviously also make that field public by using ACC_PUBLIC instead of ACC_PRIVATE.

Great. I will try it. By the way, is this Java ASM or BCEL?

Thanks – erwin davis Oct 5 '10 at 3:31 1 ASM............. – aioobe Oct 5 '10 at 6:14.

The actual problem is that you cannot legally access those variables. This is because the JVM defined its access rules before Java had inner classes, so javac creates synthetic accessors for fields that it cannot legally access in the JVM but can in Java. For example, class Sample { private int I = 0; class Inner { int foo = i; } } Then we can use javap to decompile the generated classes.

Fowles@morbo:/tmp$ javap -private Sample Compiled from "Sample. Java" class Sample extends java.lang. Object{ private int i; Sample(); static int access$000(Sample); } fowles@morbo:/tmp$ javap -c Sample.

Inner Compiled from "Sample. Java" class Sample$Inner extends java.lang. Object{ int foo; final Sample this$0; Sample$Inner(Sample); Code: 0: aload_0 1: aload_1 2: putfield #1; //Field this$0:LSample; 5: aload_0 6: invokespecial #2; //Method java/lang/Object."":()V 9: aload_0 10: aload_0 11: getfield #1; //Field this$0:LSample; 14: invokestatic #3; //Method Sample.

Access$000:(LSample;)I 17: putfield #4; //Field foo:I 20: return } Notice the access$000(Sample) method that got generated in Sample and used from Sample.Inner. Sadly, your options are to either Make the field accessible Use reflection Generate synthetic accessors.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions