常見錯誤 一
try--catch--catch--finally
public class test
{
public HashMap f5()
{
Connection conn = null;
HashMap map = new HashMap();
try
{
Class.forName("");
conn = DriverManager.getConnection("jdbc:odbc:");
PreparedStatement pstmt = conn.prepareStatement("select * from table");
pstmt.setString(1, "param");
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
String columnVallue = rs.getString("column");
map.put(columnVallue, "");
}
return map;
}
catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException sqlce)
{
sqlce.printStackTrace();
}
}
}
return null;
}
}
反編譯
public class test
{
public test()
{
}
public HashMap f5()
{
Connection conn;
HashMap map;
conn = null; ---------聲明和初始化分開了
map = new HashMap();
HashMap hashmap; ---------多了hashmap ,是爲了邏輯的需要
Class.forName(""); ---------缺少try,看不出try入口在這裏
conn = DriverManager.getConnection("jdbc:odbc:");
PreparedStatement pstmt = conn.prepareStatement("select * from table");
pstmt.setString(1, "param");
String columnVallue; ---------缺少while,且把塊裏的聲明和初始化分開了
for(ResultSet rs = pstmt.executeQuery(); rs.next(); map.put(columnVallue, "")) ---------while變成for,邏輯也適當改變了
columnVallue = rs.getString("column");
hashmap = map;
if(conn != null)
try
{
conn.close();
}
catch(SQLException sqlce)
{
sqlce.printStackTrace();
}
return hashmap; ---------原來try裏的return跑這裏來了,這是原try塊結束點
ClassNotFoundException cnfe; ---------第一個catch塊異常
cnfe;
cnfe.printStackTrace();
if(conn != null) ---------if沒有塊{},break爲if塊結束標誌
try
{
conn.close();
}
catch(SQLException sqlce)
{
sqlce.printStackTrace();
}
break MISSING_BLOCK_LABEL_188; ---------這是棧標誌,字面意思是"信息塊標籤",反編譯時,把各路徑塊存入堆棧,然後依次彈出,這表示爲一個語句塊的分界點,也就是說有一個“}”
SQLException sqle; ---------第二個catch塊異常
sqle;
sqle.printStackTrace();
if(conn != null) ---------if沒有塊,break爲if塊結束標誌
try ---------finally塊的代碼多次出現是因爲他在多條路徑中,故多次從堆棧彈出
{
conn.close();
}
catch(SQLException sqlce)
{
sqlce.printStackTrace();
}
break MISSING_BLOCK_LABEL_188; ---------又一個塊結束標誌,第二個catch塊結束標誌
Exception exception; ---------finally塊總是以Exception 爲標誌
exception;
if(conn != null)
try
{
conn.close();
}
catch(SQLException sqlce)
{
sqlce.printStackTrace();
}
throw exception;
return null; ---------finally塊後的return,在throw後面
}
}
常見錯誤二
try--catch--finally+if--else
public class test
{
public String f4()
{
Integer i = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
i =Integer.parseInt(br.readLine());
if (i.intValue() > 0)
{
System.out.println(1);
return String.valueOf(1);
}
else if(i.intValue() == 0)
{
System.out.println(0);
return String.valueOf(0);
}else
{
System.err.println(-1);
return String.valueOf(-1);
}
}
catch (Exception dae)
{
System.err.println("有錯");
}
finally
{
if (i.intValue() == 3 )
{
try
{
i = new Integer(3);
}
catch (Exception sqlce)
{
sqlce.printStackTrace();
}
}
}
return String.valueOf(i);
}
}
反編譯:
public class test
{
public test()
{
}
public String f4()
{
Integer i;
BufferedReader br; ---------變量聲明和初始化分開了
i = Integer.valueOf(0);
br = new BufferedReader(new InputStreamReader(System.in));
String s;
i = Integer.valueOf(Integer.parseInt(br.readLine()));
if(i.intValue() <= 0) ---------if塊遺失,這裏到return爲if塊,但需剔除其中finally塊的代碼
break MISSING_BLOCK_LABEL_84; ---------注意:全部if的條件取反了,即==變!=,&&變||,<變>=...
System.out.println(1);
s = String.valueOf(1);
if(i.intValue() == 3) ---------因爲if到finally爲一條路徑,故有finally塊代碼
try
{
i = new Integer(3);
}
catch(Exception sqlce)
{
sqlce.printStackTrace();
}
return s;
if(i.intValue() != 0) ---------第二個if塊,可見反編譯吧if--else if--else拆散爲多個單獨的if塊了
break MISSING_BLOCK_LABEL_134;
System.out.println(0);
s = String.valueOf(0);
if(i.intValue() == 3)
try
{
i = new Integer(3);
}
catch(Exception sqlce)
{
sqlce.printStackTrace();
}
return s;
System.err.println(-1); ---------最後一個else沒有變爲if,而是直接寫出邏輯,和前面if塊相似
s = String.valueOf(-1);
if(i.intValue() == 3)
try
{
i = new Integer(3);
}
catch(Exception sqlce)
{
sqlce.printStackTrace();
}
return s;
Exception dae; ---------catch塊
dae;
System.err.println("/u6709/u9519");
if(i.intValue() == 3)
try
{
i = new Integer(3);
}
catch(Exception sqlce)
{
sqlce.printStackTrace();
}
break MISSING_BLOCK_LABEL_248; ---------catch塊結束
Exception exception; ---------finally塊
exception;
if(i.intValue() == 3)
try
{
i = new Integer(3);
}
catch(Exception sqlce)
{
sqlce.printStackTrace();
}
throw exception;
return String.valueOf(i); ---------finally後的語句
}
}
常見錯誤三
多個標記跳轉(一般是while--for--if的嵌套)
public class Conn
{ //死循環
public static void main(String args[])
{
int[] list = new int[] { 1, 2, 3, 4 };
if (Boolean.getBoolean("sys")) {
System.out.println("sys");
} else {
check: while (true) {
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]);
if (list[i] == 2) {
continue check;
}
}
break;
}
}
}
}
反編譯:
public class Conn
{
public Conn() -----------自動生成無參構造函數
{
}
public static void main(String args[])
{
int list[] = { -----------改變數組聲明方式
1, 2, 3, 4
};
if(!Boolean.getBoolean("sys")) goto _L2; else goto _L1 -----------if條件取反,if模塊爲_L1-->_L3-->OVER
_L1:
System.out.println("sys");
goto _L3 -----------注:_L3爲結束點,if,else,for,while塊的結束點,即 "}".
_L2:
int i = 0; -----------for循環條件被拆散,注意路徑 _L2-->_L4-->_L6-->_L5,可還原
goto _L4
_L6:
System.out.println(list[i]);
if(list[i] != 2) goto _L5; else goto _L2 -----------標記 'L1--->L6'以棧方式存取
_L5:
i++; -----------注: i++在
_L4:
if(i < list.length) goto _L6; else goto _L3 -----------L2的條件初始化和這裏的if,說明是一個for循環
_L3:
}
}
常見錯誤四
enum
public enum Test {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7),
PLUTO (1.27e+22, 1.137e6);
private final double mass; // in kilograms
private final double radius; // in meters
Test(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double mass() { return mass; }
public double radius() { return radius; }
// universal gravitational constant (m 3 kg -1 s-2)
public static final double G = 6.67300E-11;
public double surfaceGravity() {
return G * mass / (radius * radius);
}
public double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
public static void main(String[] args) {
double earthWeight = Double.parseDouble(args[0]);
double mass = earthWeight/EARTH.surfaceGravity();
for (Test p : Test.values())
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
}
}
反編譯:
public final class Test extends Enum
{
public static final Test MERCURY;
public static final Test VENUS;
public static final Test EARTH;
public static final Test MARS;
public static final Test JUPITER;
public static final Test SATURN;
public static final Test URANUS;
public static final Test NEPTUNE;
public static final Test PLUTO;
private final double mass;
private final double radius;
public static final double G = 6.6729999999999999E-011D;
private static final Test ENUM$VALUES[];
private Test(String s, int i, double mass, double radius)
{
super(s, i);
this.mass = mass;
this.radius = radius;
}
public double mass()
{
return mass;
}
public double radius()
{
return radius;
}
public double surfaceGravity()
{
return (6.6729999999999999E-011D * mass) / (radius * radius);
}
public double surfaceWeight(double otherMass)
{
return otherMass * surfaceGravity();
}
public static void main(String args[])
{
double earthWeight = Double.parseDouble(args[0]);
double mass = earthWeight / EARTH.surfaceGravity();
Test atest[];
int j = (atest = values()).length;
for(int i = 0; i < j; i++)
{
Test p = atest[i];
System.out.printf("Your weight on %s is %f%n", new Object[] {
p, Double.valueOf(p.surfaceWeight(mass))
});
}
}
public static Test[] values()
{
Test atest[];
int i;
Test atest1[];
System.arraycopy(atest = ENUM$VALUES, 0, atest1 = new Test[i = atest.length], 0, i);
return atest1;
}
public static Test valueOf(String s)
{
return (Test)Enum.valueOf(test/Test, s);
}
static
{
MERCURY = new Test("MERCURY", 0, 3.3030000000000001E+023D, 2439700D);
VENUS = new Test("VENUS", 1, 4.8690000000000001E+024D, 6051800D);
EARTH = new Test("EARTH", 2, 5.9760000000000004E+024D, 6378140D);
MARS = new Test("MARS", 3, 6.4209999999999999E+023D, 3397200D);
JUPITER = new Test("JUPITER", 4, 1.9000000000000001E+027D, 71492000D);
SATURN = new Test("SATURN", 5, 5.6879999999999998E+026D, 60268000D);
URANUS = new Test("URANUS", 6, 8.686E+025D, 25559000D);
NEPTUNE = new Test("NEPTUNE", 7, 1.0239999999999999E+026D, 24746000D);
PLUTO = new Test("PLUTO", 8, 1.2700000000000001E+022D, 1137000D);
ENUM$VALUES = (new Test[] {
MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE, PLUTO
});
}
}