1. 程式人生 > >android反編譯apk後,java檔案中出現的一些類似亂碼的問題

android反編譯apk後,java檔案中出現的一些類似亂碼的問題

概述

java class  反編譯之後,偶爾回碰到一些不正常的程式碼

例如:

label0 :_L1 MISSING_BLOCK_LABEL_30

JVM INSTR ret 7

JVM INSTR tableswitch 1 3: default 269、

JVM INSTR monitorexit

JVM INSTR monitorenter

這些一般是由特殊的for迴圈、try catch finally語句塊、synchronized語句反編譯後產生的。

下面,就簡單介紹一下,一些反編譯後的特殊程式碼的還原規則。



1、Exceptioin的還原 

 反編譯後的程式碼如下

public boolean f1() {
        return cal.getTime().after(new Date());
        Exception e;
        e;
        e.printStackTrace();
        return false;
    }

還原後的Java程式碼

public boolean f1() {
        try {
            return cal.getTime().after(new Date());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

2、finally程式碼的還原

反編譯後的Java程式碼如下

	 
	public boolean f2() { 
		boolean flag = cal.getTime().after(new Date());
		System.out.println("finally");
		return flag;
		Exception e;
		e;
		e.printStackTrace();
		System.out.println("finally");
		return false;
		Exception exception;
		exception;
		System.out.println("finally");
		throw exception;
	} 
還原後的程式碼如下
	 
	public boolean f2() { 
		try { 
			return cal.getTime().after(new Date());
		} catch (Exception e) { e.printStackTrace();
			return false;
		} finally { 
			System.out.println("finally");
		} 
	} 

3、MISSING_BLOCK_LABEL_的還原

反編譯後的程式碼

	public Object f22() { 
		Date date = cal.getTime();
		System.out.println("finally");
		return date;
		Exception e;
		e;
		e.printStackTrace();
		System.out.println("finally");
		break MISSING_BLOCK_LABEL_45;
		Exception exception;
		exception;
		System.out.println("finally");
		throw exception;
		return null;
	} 
還原後的Java程式碼
	 
	public Object f22() { 
		try { 
			return cal.getTime();
		} catch (Exception e) { 
			e.printStackTrace();
		} finally { 
			System.out.println("finally");
		} return null;
	} 

4、異常中:label的還原

反編譯後的程式碼

	public String f4() throws Exception { 
		label0: { 
		try { 
			Integer i = new Integer(1);
		if(i.intValue() >0) { 
			System.out.println(i);
			break label0;
		} 
			System.err.println(i);
		} catch(Exception dae) { 
			System.err.println(dae);
		throw new RuntimeException(dae);
		} 
			return null;
		} 
			return "Hello";
	} 
注意,這個程式碼有點詭異,實際程式碼如下:
 
	public String f4() throws Exception { 
			try { 
				Integer i = new Integer(1);
			if (i.intValue() >0) { 
				System.out.println(i);
			} else {
				System.err.println(i);
				return null;
			} 
			return "Hello";
			} catch (Exception dae) { 
				System.err.println(dae);
			throw new RuntimeException(dae);
			} 
	} 

5、典型資料庫操作程式碼還原

反編譯後代碼

	public HashMap f5() { 
		Connection conn = null;
		HashMap hashmap;
		HashMap map = new HashMap();
		Class.forName("");
		conn = DriverManager.getConnection("jdbc:odbc:");
		PreparedStatement pstmt = conn.prepareStatement("select * from table");
		pstmt.setString(1, "param");
		String columnVallue;
	for(ResultSet rs = pstmt.executeQuery();rs.next();map.put(columnVallue, "")) columnVallue = rs.getString("column");
		hashmap = map;
	if(conn != null) try { 
		conn.close();
	} catch(SQLException sqlce) { 
		sqlce.printStackTrace();
	} 
	return hashmap;
	ClassNotFoundException cnfe;
	cnfe;
	cnfe.printStackTrace();
	if(conn != null) 
		try { 
			conn.close();
		} catch(SQLException sqlce) { 
			sqlce.printStackTrace();
		} 
		break MISSING_BLOCK_LABEL_188;
		SQLException sqle;
		sqle;
		sqle.printStackTrace();
		if(conn != null) try { 
			conn.close();
		} catch(SQLException sqlce) { 
			sqlce.printStackTrace();
		} 
		break MISSING_BLOCK_LABEL_188;
		Exception exception;
		exception;
		if(conn != null) try { 
			conn.close();
		} catch(SQLException sqlce) { 
			sqlce.printStackTrace();
		} throw exception;
		return null;
	} 
實際程式碼如下
	public HashMap f5() { 
		Connection conn = null;
		try { 
			HashMap map = new HashMap();
			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;
		}

6、兩層異常巢狀程式碼還原

反編譯後的程式碼

	public int f6() { 
		int i = cal.getTime().compareTo(new Date());
		System.out.println("finally");
		return i;
		Exception e1;
		e1;
		e1.printStackTrace();
		System.out.println("finally");
		return -1;
		Exception e2;
		e2;
		e2.printStackTrace();
		System.out.println("finally");
		return -2;
		Exception exception;
		exception;
		System.out.println("finally");
		throw exception;
	} 
實際程式碼
	public int f6() { 
		try { 
			try { 
				return cal.getTime().compareTo(new Date());
		} catch (Exception e1) { 
			e1.printStackTrace();
			return -1;
		} } catch (Exception e2) { 
			e2.printStackTrace();
			return -2;
		} finally { 
			System.out.println("finally");
		} 
		}  
	}

7、非常詭異的程式碼

反編譯後的程式碼

	public int f7() { 
		int i = cal.getTime().compareTo(new Date());
	}
	System.out.println("finally");
	return i;
	Exception e1;
	e1;
	e1.printStackTrace();
	_L2: System.out.println("finally");
	return -1;
	Exception e2;
	e2;
	e2.printStackTrace();
	if(true) 
		goto _L2;
	else 
		goto _L1 _L1: 
			Exception exception;
	exception;
	System.out.println("finally");
	throw exception;
	} 
原始程式碼
	public int f7() { 
		try { 
			try { 
				return cal.getTime().compareTo(new Date());
			} catch (Exception e1) { 
				e1.printStackTrace();
				return -1;
			} } catch (Exception e2) {
				e2.printStackTrace();
				return -1;
			} finally { 
				System.out.println("finally");
			} 
		}