使用工具(例如apktool,dex2jar-2.0,jd-gui-windows-1.4.0)将APK反编译后得到的代码看起来很奇怪,这里试着说明java对内部类的处理,以方便大家阅读反编译后的代码。
下面举例对java内部类实现进行解析,以帮助分析反编译后的文件。
public class MyChartItem { private Stringx; private float y; private float lowTemp; private StringweatherDayStr; private StringweatherNightStr; private int dayIcon; private int nightIcon;
public MyChartItem(Stringvx,floatvy,floatlowT, StringdayStr, StringnightStr) { this.x =vx; this.y =vy; this.lowTemp =lowT; this.weatherDayStr =dayStr; this.weatherNightStr =nightStr; dayIcon = getWeatherIcon(dayStr,true); nightIcon = getWeatherIcon(nightStr,false); }
class forcastItem{ int wind; int dayTemp; int nightTemp; int aqi; public forcastItem(){ wind = 0; dayTemp = 0; nightTemp = 0; aqi = 0; }
public void setItem(){ y = dayTemp; lowTemp = nightTemp; String tempStr = x; tempStr += "5/29"; } } } |
编译上述代码后,JAVA编译器会把内部类forcastItem编译成一个独立的class文件”MyChartItem$forcastItem.class”,其形式为“外部类$内部类.class”。
// Referenced classes of package com.miva.myWeather: // MyChartItem
//有时http://www.showmycode.com 反编译出来的类名优点奇怪,例如我这里可能类名编程aqi…… class MyChartItem$forcastItem {
public void setItem() { MyChartItem.access$0(MyChartItem.this, dayTemp); MyChartItem.access$1(MyChartItem.this, nightTemp); StringtempStr = MyChartItem.access$2(MyChartItem.this); tempStr = (newStringBuilder(String.valueOf(tempStr))).append("5/29").toString(); }
int wind; int dayTemp; int nightTemp; int aqi; final MyChartItem this$0;
public () { this$0 = MyChartItem.this; super(); wind = 0; dayTemp = 0; nightTemp = 0; aqi = 0; } }
|
通过上述定义,我们看到内部类在构造时,会被编译器自动传入外部类对象的一个引用,针对这里就是变量“final MyChartItem this$0;”;这样内部类中就可以访问外部类中的成员变量和函数;
“MyChartItem.access$0(MyChartItem.this)” 是对MyChartItem类中第一个成员变量“y”的引用;
代码对应关系:
反编译后的代码 | 实际的代码 |
MyChartItem.access$0(MyChartItem.this, dayTemp) | MyChartItem.this.y = this.dayTemp; |
MyChartItem.access$1(MyChartItem.this, nightTemp); | MyChartItem.this.lowTemp = this.nightTemp; |
String tempStr = MyChartItem.access$2(MyChartItem.this); tempStr = (new StringBuilder(String.valueOf(tempStr))).append("5/29").toString(); | String tempStr = MyChartItem.this.x; tempStr += "5/29"; |