Android反编译系列一之从扫雷游戏学习反编译Switch语句,if条件语句
时间:2010-10-03 来源:永恒的记忆
今天学习Android反编译中的Switch和if
源码:
public void updateNumber(int text) {
if (text != 0) {
this.setText(Integer.toString(text));
switch (text) {
case 1:
this.setTextColor(Color.BLUE);
break;
case 2:
this.setTextColor(Color.rgb(0, 100, 0));
break;
case 3:
this.setTextColor(Color.RED);
break;
case 4:
this.setTextColor(Color.rgb(85, 26, 139));
break;
case 5:
this.setTextColor(Color.rgb(139, 28, 98));
break;
case 6:
this.setTextColor(Color.rgb(238, 173, 14));
break;
case 7:
this.setTextColor(Color.rgb(47, 79, 79));
break;
case 8:
this.setTextColor(Color.rgb(71, 71, 71));
break;
case 9:
this.setTextColor(Color.rgb(205, 205, 0));
break;
}
}
}
2.编译后的代码
public void updateNumber(int i)
{
char c;
char c1;
byte byte0;
byte byte1;
int j;
c = '\315';
c1 = '\213';
byte0 = 79;
byte1 = 71;
j = 0;
if (i == 0) goto _L2; else goto _L1
_L1:
String s = Integer.toString(i);
setText(s);
i;
JVM INSTR tableswitch 1 9: default 88
// 1 89
// 2 98
// 3 118
// 4 127
// 5 146
// 6 165
// 7 187
// 8 207
// 9 227;
goto _L2 _L3 _L4 _L5 _L6 _L7 _L8 _L9 _L10 _L11
_L2:
return;
_L3:
setTextColor(0xff0000ff);
continue; /* Loop/switch isn't completed */
_L4:
int k = Color.rgb(j, 100, j);
setTextColor(k);
continue; /* Loop/switch isn't completed */
_L5:
setTextColor(0xffff0000);
continue; /* Loop/switch isn't completed */
_L6:
int l = Color.rgb(85, 26, c1);
setTextColor(l);
continue; /* Loop/switch isn't completed */
_L7:
int i1 = Color.rgb(c1, 28, 98);
setTextColor(i1);
continue; /* Loop/switch isn't completed */
_L8:
int j1 = Color.rgb(238, 173, 14);
setTextColor(j1);
continue; /* Loop/switch isn't completed */
_L9:
int k1 = Color.rgb(47, byte0, byte0);
setTextColor(k1);
continue; /* Loop/switch isn't completed */
_L10:
int l1 = Color.rgb(byte1, byte1, byte1);
setTextColor(l1);
continue; /* Loop/switch isn't completed */
_L11:
int i2 = Color.rgb(c, c, j);
setTextColor(i2);
if (true) goto _L2; else goto _L12
_L12:
}
由此我们可以看到,Java源代码在编译后会产生许多的中间变量,而且变量的名称也会发生改变。
if条件语句
一般情况下
public void updateNumber(int text) {
If(text!=0)
{
this.setText(Integer.toString(text));
...
}
}
编译后是
public void updateNumber(int text) {
If(text==0)
Goto _L2 else goto _L1
_L2:
return;
_L1:
String s = Integer.toString(i);
setText(s);
...
}
Switch语句
JVM INSTR tableswitch 1 9: default 88是Switch语句的标志
JVM INSTR tableswitch 1 9: default 88
// 1 89
// 2 98
// 3 118
// 4 127
// 5 146
// 6 165
// 7 187
// 8 207
// 9 227;
goto _L2 _L3 _L4 _L5 _L6 _L7 _L8 _L9 _L10 _L11
从上面加蓝色的代码中我们我可以得出该Switch语句有9个分支case:1-9.
从上面加红色的代码中我们我可以得出
L2:
case default:break;
我们在源代码中的枚举值会转化为十六进制。一些经常用到的常数会将它定义为一个变量。
每个标志位的"continue"说明该分支break;
最后一个分支的标志是if (true) goto _L2; else goto _L12
我们再做一个例子
public void updateNumber(int text) {
if (text != 0) {
this.setText(Integer.toString(text));
switch (text) {
case 1:
this.setTextColor(Color.BLUE);
break;
case 2:
this.setTextColor(Color.rgb(0, 100, 0));
break;
case 9:
this.setTextColor(Color.rgb(205, 205, 0));
break;
}
}
}
编译后
是诸如JVM INSTR tableswitch 1 9: default 88是Switch语句的标志
JVM INSTR tableswitch 1 9: default 88
// 1 89
// 2 98
// 9 227;
goto _L2 _L3 _L4