class TestAccessPrivateVar{
    private int a = 1;
    class MyInner{

    	/*
    	synthetic final TestAccessPrivateVar this$0
		<init>(synthetic final TestAccessPrivateVar this$0) {
		    this.this$0 = this$0;
		    super();
		}
		*/
        public void t(){
            int b = a;  // int b = TestAccessPrivateVar.access$000(this$0);
            a++;     // TestAccessPrivateVar.access$008(this$0)
            a += 1; // TestAccessPrivateVar.access$012(this$0, 1);

        }
    }

    /*
	synthetic static int access$000(TestAccessPrivateVar x0) {
	    return x0.a;
	}

	synthetic static int access$008(TestAccessPrivateVar x0) {
	    return x0.a++;
	}

	synthetic static int access$012(TestAccessPrivateVar x0, int x1) {
	    return x0.a += x1;
	}
	*/
}

 

class Outer {
    private Outer(){}

    /*
		synthetic <init>(com.test20.Outer$1 x0) {
		    this();
		}
        synthetic static class Outer$1  {  }
                       要用这个新的类,因为怕与现有类冲突
    */

    class AOuter extends Outer{
    	 // 将下面的构造函数更改为如下形式:
        /*
            synthetic final Outer this$0
			public <init>(synthetic final Outer this$0) {
			    this.this$0 = this$0
			    super();
			}
         */
        public AOuter(){
            super(); // 将会更改为super(null)进行调用
        }

    }
}

  

package com.test19;
public class TestProtectedVar {
	protected int a = 1;
}

  

package com.test20;
import com.test19.TestProtectedVar;
class Parent extends TestProtectedVar{
    public String method1() {
        return "";
    }
}
class Sub extends Parent {
    public String method1() {
        return null;
    }
    class inner {
        public void t() {
            Sub.super.method1(); // Sub.access$001(this$0)
            Sub.this.method1(); // this$0.method1()
            int x = a;  // int x = Sub.access$100(this$0)
            int y = Sub.super.a; // int y = Sub.access$201(this$0)
        }
    }

	/*
	    synthetic static String access$001(Sub x0) {
	      return x0.method1();
	    }

		synthetic static int access$100(Sub x0) {
		    return x0.a;
		}

		// 下面的方法虽然与上面的body中代码一致,但是编号不一样
		synthetic static int access$201(Sub x0) {
		    return x0.a;
		}
	*/
}

  

  

 

在内部类中获取外部类的私有变量,需要通过access method进行获取,举个例子,如下:

class TestAccessPrivateVar{
	private int a = 1;
	class MyInner{
		public void t(){
			int b = a; // Test03.access$000(this$0)
			a += 1; // Test03.access$012(this$0, 1)
		}
	}
}

通过在线工具http://www.javadecompilers.com/ 对class文件进行反编译后,结果如下:

package com.test19;

class TestAccessPrivateVar {
    private int a = 1;

    TestAccessPrivateVar() {   }

    static /* synthetic */ int access$000(TestAccessPrivateVar testAccessPrivateVar) {
        return testAccessPrivateVar.a;
    }

    static /* synthetic */ int access$012(TestAccessPrivateVar testAccessPrivateVar, int n) {
        return testAccessPrivateVar.a += n;
    }
}  

反编译内部类后的源代码如下:

package com.test19;
class TestAccessPrivateVar$MyInner {
    /*synthetic*/ final Test07 this$0
    TestAccessPrivateVar$MyInner(TestAccessPrivateVar var1) {
        this.this$0 = var1;
    }

    public void t() {
        int var1 = TestAccessPrivateVar.access$000(this.this$0);
        TestAccessPrivateVar.access$012(this.this$0, 1);
    }
}
Classfile /C:/TestAccessPrivateVar.class
  Last modified 2018-7-26; size 534 bytes
  MD5 checksum db3dba90c37e7c304a98af3f9d4438b1
  Compiled from "Test03.java"
class com.test19.TestAccessPrivateVar
  SourceFile: "Test03.java"
  InnerClasses:
       #6= #5 of #3; //MyInner=class com/test19/TestAccessPrivateVar$MyInner of class com/test19/TestAccessPrivateVar
  minor version: 0
  major version: 51
  flags: ACC_SUPER
Constant pool:
   #1 = Fieldref           #3.#20         //  com/test19/TestAccessPrivateVar.a:I
   #2 = Methodref          #4.#21         //  java/lang/Object."<init>":()V
   #3 = Class              #22            //  com/test19/TestAccessPrivateVar
   #4 = Class              #23            //  java/lang/Object
   #5 = Class              #24            //  com/test19/TestAccessPrivateVar$MyInner
   #6 = Utf8               MyInner
   #7 = Utf8               InnerClasses
   #8 = Utf8               a
   #9 = Utf8               I
  #10 = Utf8               <init>
  #11 = Utf8               ()V
  #12 = Utf8               Code
  #13 = Utf8               LineNumberTable
  #14 = Utf8               access$000
  #15 = Utf8               (Lcom/test19/TestAccessPrivateVar;)I
  #16 = Utf8               access$012
  #17 = Utf8               (Lcom/test19/TestAccessPrivateVar;I)I
  #18 = Utf8               SourceFile
  #19 = Utf8               Test03.java
  #20 = NameAndType        #8:#9          //  a:I
  #21 = NameAndType        #10:#11        //  "<init>":()V
  #22 = Utf8               com/test19/TestAccessPrivateVar
  #23 = Utf8               java/lang/Object
  #24 = Utf8               com/test19/TestAccessPrivateVar$MyInner
{
  com.test19.TestAccessPrivateVar();
    flags:
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: invokespecial #2                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: iconst_1
         6: putfield      #1                  // Field a:I
         9: return
      LineNumberTable:
        line 4: 0
        line 5: 4
        line 6: 9

  static int access$000(com.test19.TestAccessPrivateVar);
    flags: ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: getfield      #1                  // Field a:I
         4: ireturn
      LineNumberTable:
        line 4: 0

  static int access$012(com.test19.TestAccessPrivateVar, int);
    flags: ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=3, locals=2, args_size=2
         0: aload_0
         1: dup
         2: getfield      #1                  // Field a:I
         5: iload_1
         6: iadd
         7: dup_x1
         8: putfield      #1                  // Field a:I
        11: ireturn
      LineNumberTable:
        line 4: 0
}

  

Classfile /C:/TestAccessPrivateVar$MyInner.class
  Last modified 2018-7-26; size 597 bytes
  MD5 checksum faad0d2e11456029c49b2a821e94c67d
  Compiled from "Test03.java"
class com.test19.TestAccessPrivateVar$MyInner
  SourceFile: "Test03.java"
  InnerClasses:
       #23= #5 of #19; //MyInner=class com/test19/TestAccessPrivateVar$MyInner of class com/test19/TestAccessPrivateVar
  minor version: 0
  major version: 51
  flags: ACC_SUPER
Constant pool:
   #1 = Fieldref           #5.#17         //  com/test19/TestAccessPrivateVar$MyInner.this$0:Lcom/test19/TestAccessPrivateVar;
   #2 = Methodref          #6.#18         //  java/lang/Object."<init>":()V
   #3 = Methodref          #19.#20        //  com/test19/TestAccessPrivateVar.access$000:(Lcom/test19/TestAccessPrivateVar;)I
   #4 = Methodref          #19.#21        //  com/test19/TestAccessPrivateVar.access$012:(Lcom/test19/TestAccessPrivateVar;I)I
   #5 = Class              #22            //  com/test19/TestAccessPrivateVar$MyInner
   #6 = Class              #25            //  java/lang/Object
   #7 = Utf8               this$0
   #8 = Utf8               Lcom/test19/TestAccessPrivateVar;
   #9 = Utf8               <init>
  #10 = Utf8               (Lcom/test19/TestAccessPrivateVar;)V
  #11 = Utf8               Code
  #12 = Utf8               LineNumberTable
  #13 = Utf8               t
  #14 = Utf8               ()V
  #15 = Utf8               SourceFile
  #16 = Utf8               Test03.java
  #17 = NameAndType        #7:#8          //  this$0:Lcom/test19/TestAccessPrivateVar;
  #18 = NameAndType        #9:#14         //  "<init>":()V
  #19 = Class              #26            //  com/test19/TestAccessPrivateVar
  #20 = NameAndType        #27:#28        //  access$000:(Lcom/test19/TestAccessPrivateVar;)I
  #21 = NameAndType        #29:#30        //  access$012:(Lcom/test19/TestAccessPrivateVar;I)I
  #22 = Utf8               com/test19/TestAccessPrivateVar$MyInner
  #23 = Utf8               MyInner
  #24 = Utf8               InnerClasses
  #25 = Utf8               java/lang/Object
  #26 = Utf8               com/test19/TestAccessPrivateVar
  #27 = Utf8               access$000
  #28 = Utf8               (Lcom/test19/TestAccessPrivateVar;)I
  #29 = Utf8               access$012
  #30 = Utf8               (Lcom/test19/TestAccessPrivateVar;I)I
{
  final com.test19.TestAccessPrivateVar this$0;
    flags: ACC_FINAL, ACC_SYNTHETIC

  com.test19.TestAccessPrivateVar$MyInner(com.test19.TestAccessPrivateVar);
    flags:
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0
         1: aload_1
         2: putfield      #1                  // Field this$0:Lcom/test19/TestAccessPrivateVar;
         5: aload_0
         6: invokespecial #2                  // Method java/lang/Object."<init>":()V
         9: return
      LineNumberTable:
        line 6: 0

  public void t();
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=1
         0: aload_0
         1: getfield      #1                  // Field this$0:Lcom/test19/TestAccessPrivateVar;
         4: invokestatic  #3                  // Method com/test19/TestAccessPrivateVar.access$000:(Lcom/test19/TestAccessPrivateVar;)I
         7: istore_1
         8: aload_0
         9: getfield      #1                  // Field this$0:Lcom/test19/TestAccessPrivateVar;
        12: iconst_1
        13: invokestatic  #4                  // Method com/test19/TestAccessPrivateVar.access$012:(Lcom/test19/TestAccessPrivateVar;I)I
        16: pop
        17: return
      LineNumberTable:
        line 8: 0
        line 9: 8
        line 10: 17
}

  

05-11 17:13