Optimization of Android Internal classes

Source: Internet
Author: User

There is an article on performance in the developer.android.com documentation that mentions the use of internal classes. The article recommends "use package access for private internal classes instead of private access",

This is to say that when an inner class accesses a member or method of an external class, if the inner class is private and the members of the outer class are private, then the compiler adds a static method to the inner class in the outer part.

Is that really the case? You only know when you try.

We use a simple example to test the following:

public class One {private int a;private class B{public int Geta () {return A;}}}

The top code creates a private inner class B for one, and B has a method that accesses private member a in one. We have compiled the above code into a Dex file and exported the program's instruction code and class information.

The results of the export are as follows:

Processing ' one.dex ' ...  Opened ' One.dex ', Dex version ' 035 ' Class #0-class descriptor: ' lone$b; '  Access flags:0x0000 () superclass: ' Ljava/lang/object; ' Interfaces-static fields-instance fields-#0: (in lone$b;) Name: ' This      $ ' type: ' LOne; ' ACCESS:0X1010 (FINAL synthetic) Direct methods-#0: (in lone$b;) Name: ' < Init> ' type: ' (LOne;) V ' access:0x10002 (PRIVATE CONSTRUCTOR) Code-reg                                        Isters:2 ins:2 outs:1 Insns size:6 16-bit code units0001a0: | [0001a0] one.b.<init>:(LOne;) v0001b0:5b01 0000 |0000:iput-object v1, v0, lone$b;. This$0:lone; [email protected]0001b4:7010 0400 0000 |0002:invoke-direct {V0}, ljava/lang/object;. <init>:() V//[email protected]0001ba:0e00 |0005:return-void catches: (none)   positions:0x0000 line=7 locals:0x0000-0x0006 reg=0 this lone$b;        Virtual methods-#0: (in lone$b;) name: ' Geta ' type: ' () I ' access : 0x0001 (public) Code-registers:2 ins:1 outs:1 ins NS Size:7 16-bit code UNITS0001BC: | [0001BC] One.b.geta: () i0001cc:5410 0000 |0000:iget-object v0, v1, lone$b;. This$0:lone; [email protected]0001d0:7110 0300 0000 |0002:invoke-static {V0}, LOne;. Access$0: (LOne;) I//[email protected]0001d6:0a00 |0005:move-result v00001d8:0f0    0 |0006:return V0 catches: (none) positions : 0x0000 line=10 locals:0x0000-0x0007 reg=1 this lone$b;  Source_file_idx:10 (One.java) Class #1-class descriptor: ' LOne; '  Access flags:0x0001 (public) Superclass: ' Ljava/lang/object; '      Interfaces-static fields-instance fields-#0: (in LOne;) name: ' A '           Type: ' I ' access:0x0002 (PRIVATE) Direct methods-#0: (in LOne;) name          : ' <init> ' type: ' () V ' access:0x10001 (public CONSTRUCTOR) code                                        -Registers:1 ins:1 outs:1 Insns size:4 16-bit code UNITS0001DC: | [0001DC] one.<init>:() v0001ec:7010 0400 0000 |0000:invoke-direct {V0}, ljava/lang/object;. <init>:() V//[email protected]0001f2:0e00 |0003:Return-void catches: (none) positions:0x0000 line=3 locals:0x0000-0     x0004 reg=0 this LOne; #1: (in LOne;) name: ' access$0 ' type: ' (LOne;) I ' access:0x1008 ( STATIC synthetic) code-registers:2 ins:1 outs:0 Insns siz E:3 16-bit Code UNITS0001F4: | [0001f4] one.access$0: (LOne;) i000204:5210 0100 |0000:iget v0, v1, LOne;.      A:I//[email protected]000208:0f00 |0002:return V0 catches: (none) positions:0x0000 line=5 locals:virtual methods-source_file_idx:10 (One.java)

Ha see not one of the more methods, and our source code is not.
One.access$0: (LOne;) I

Now let's change the source code.

public class One {private int a;class b{public int Geta () {return A;}}}

Then in the compilation, export:

Processing ' one.dex ' ...  Opened ' One.dex ', Dex version ' 035 ' Class #0-class descriptor: ' lone$b; '  Access flags:0x0000 () superclass: ' Ljava/lang/object; ' Interfaces-static fields-instance fields-#0: (in lone$b;) Name: ' This      $ ' type: ' LOne; ' ACCESS:0X1010 (FINAL synthetic) Direct methods-#0: (in lone$b;) Name: ' <     Init> ' type: ' (LOne;) V ' access:0x10000 (CONSTRUCTOR) code-registers                                        : 2 ins:2 outs:1 Insns size:6 16-bit code units0001a0: | [0001a0] one.b.<init>:(LOne;) v0001b0:5b01 0000 |0000:iput-object v1, v0, lone$b;. This$0:lone; [email protected]0001b4:7010 0400 0000 |0002:invoke-direct {V0}, ljava/lang/object;. <init>:() V//[Email protected]0001ba:0e00 |0005:return-void catches: (none) po   sitions:0x0000 line=7 locals:0x0000-0x0006 reg=0 this lone$b;        Virtual methods-#0: (in lone$b;) name: ' Geta ' type: ' () I ' access : 0x0001 (public) Code-registers:2 ins:1 outs:1 ins NS Size:7 16-bit code UNITS0001BC: | [0001BC] One.b.geta: () i0001cc:5410 0000 |0000:iget-object v0, v1, lone$b;. This$0:lone; [email protected]0001d0:7110 0300 0000 |0002:invoke-static {V0}, LOne;. Access$0: (LOne;) I//[email protected]0001d6:0a00 |0005:move-result v00001d8:0f0      0 |0006:return V0 catches: (none) Positions:   0x0000 line=10 locals:0x0000-0x0007 reg=1 this lone$b;  Source_file_idx:10 (One.java) Class #1-class descriptor: ' LOne; '  Access flags:0x0001 (public) Superclass: ' Ljava/lang/object; '      Interfaces-static fields-instance fields-#0: (in LOne;) name: ' A '           Type: ' I ' access:0x0002 (PRIVATE) Direct methods-#0: (in LOne;) name          : ' <init> ' type: ' () V ' access:0x10001 (public CONSTRUCTOR) code                                        -Registers:1 ins:1 outs:1 Insns size:4 16-bit code UNITS0001DC: | [0001DC] one.<init>:() v0001ec:7010 0400 0000 |0000:invoke-direct {V0}, ljava/lang/object;. <init>:() V//[email protected]0001f2:0e00 |0003:return-vOID catches: (none) positions:0x0000 line=3 locals:0x0000-0x0004 re     G=0 this LOne; #1: (in LOne;) name: ' access$0 ' type: ' (LOne;) I ' access:0x1008 ( STATIC synthetic) code-registers:2 ins:1 outs:0 Insns siz E:3 16-bit Code UNITS0001F4: | [0001f4] one.access$0: (LOne;) i000204:5210 0100 |0000:iget v0, v1, LOne;.      A:I//[email protected]000208:0f00 |0002:return V0 catches: (none) positions:0x0000 line=5 locals:virtual methods-source_file_idx:10 (One.java)
There are still additional methods.
One.access$0: (LOne;) I


We're under revision.

public class One {int a;class b{public int Geta () {return A;}}}

In the compilation export

Processing ' one.dex ' ...  Opened ' One.dex ', Dex version ' 035 ' Class #0-class descriptor: ' lone$b; '  Access flags:0x0000 () superclass: ' Ljava/lang/object; ' Interfaces-static fields-instance fields-#0: (in lone$b;) Name: ' This      $ ' type: ' LOne; ' ACCESS:0X1010 (FINAL synthetic) Direct methods-#0: (in lone$b;) Name: ' <     Init> ' type: ' (LOne;) V ' access:0x10000 (CONSTRUCTOR) code-registers                                        : 2 ins:2 outs:1 Insns size:6 16-bit code units000184: | [000184] one.b.<init>:(LOne;) v000194:5b01 0000 |0000:iput-object v1, v0, lone$b;. This$0:lone; [email protected]000198:7010 0300 0000 |0002:invoke-direct {V0}, ljava/lang/object;. <init>:() V//[Email protected]00019e:0e00 |0005:return-void catches: (none) po   sitions:0x0000 line=7 locals:0x0000-0x0006 reg=0 this lone$b;        Virtual methods-#0: (in lone$b;) name: ' Geta ' type: ' () I ' access : 0x0001 (public) Code-registers:2 ins:1 outs:0 ins NS Size:5 16-bit code units0001a0: | [0001a0] One.b.geta: () i0001b0:5410 0000 |0000:iget-object v0, v1, lone$b;. This$0:lone; [email protected]0001b4:5200 0100 |0002:iget v0, V0, LOne;.      A:I//[email protected]0001b8:0f00 |0004:return V0 catches: (none)   positions:0x0000 line=10 locals:0x0000-0x0005 reg=1 this lone$b; SourcE_file_idx:9 (One.java) Class #1-class descriptor: ' LOne; '  Access flags:0x0001 (public) Superclass: ' Ljava/lang/object; '      Interfaces-static fields-instance fields-#0: (in LOne;) name: ' A '          Type: ' I ' access:0x0000 () Direct methods-#0: (in LOne;) name      : ' <init> ' type: ' () V ' access:0x10001 (public CONSTRUCTOR) code-                                        Registers:1 ins:1 outs:1 Insns size:4 16-bit code UNITS0001BC: | [0001BC] one.<init>:() v0001cc:7010 0300 0000 |0000:invoke-direct {V0}, ljava/lang/object;.       <init>:() V//[email protected]0001d2:0e00 |0003:return-void catches  : (none) positions:0x0000 line=3 locals:       0x0000-0x0004 reg=0 this LOne; Virtual Methods-source_file_idx:9 (One.java)

No additional methods are already in the way.

If the members of an external class are package-accessible, the inner class is private and does not produce an additional method.

Internal classes access to external class members is not directly accessible to private members, and the compiler adds additional helper methods, avoiding the option of modifying the members of the external class to access the package, which in the document also mentions some degree of destruction

Packaging.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.