Code restoration after Java class decompilation (2)

Source: Internet
Author: User
Tags try catch

After using Jad decompilation, Java class occasionally encounters some abnormal code, such as label0: _ L1 missing_block_label_30, JVM instr RET 7, and JVM instr tableswitch 1 3: default 269, JVM instr monitorexit, and JVM instr monitorenter are generally generated after special for loops, try catch finally statement blocks, and synchronized statements are decompiled. Next, we will briefly introduce the restoration rules for some specially compiled code. This article is tested in JDK 1.4.2 _ 08 + Jad 1.58f. Jad 1.5.8f can be downloaded from the http://download.csdn.net/source/470540 here.

Part 2: Exceptions

The following code assumes that the class has the following attributes: Calendar Cal = calendar. getinstance ();.

1. The restored decompiled code of exceptioin is as follows:

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

Restored Java code

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

2. The restored decompiled Java code of finally code is as follows:

    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;    }

The restored code is as follows:

    public boolean f2()    {        try        {            return cal.getTime().after(new Date());        }        catch (Exception e)        {            e.printStackTrace();            return false;        }        finally        {            System.out.println("finally");        }    }

3. Restore decompiled code of 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;    }

Restored Java code

    public Object f22()    {        try        {            return cal.getTime();        }        catch (Exception e)        {            e.printStackTrace();        }        finally        {            System.out.println("finally");        }        return null;    }

4. Exception: restored decompiled code of 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";    }

Note: This code is a bit strange. The actual code is as follows:

    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. Typical database operation code Restoration
Decompiled code

    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;    }

The actual code is as follows:

    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. Two-layer exception nested code Restoration
Decompiled code

    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;    }

Actual code

    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. Very strange code
Decompiled code

    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;    }

Original code

    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");        }    }
Related Article

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.