Service Virtualization

 View Only
  • 1.  java-decompiler issue (jd) with finally

    Posted Dec 21, 2017 11:03 AM

    #java-decompiler issue (jd) with finally 

     

    When you write a java class with finally block & compile the source code, decompiled class shows bunch of byte code errors and server won't execute the code. Remove finally and it works.. any ideas how to make jd work with finally in the code?

     

    Eg:-  notice finally  

    public static void connectToDB(Connection con) throws SQLException {
       Statement stmt = null;
       String query = "select * from sometable";
       try {
          stmt = con.createStatement();
          ResultSet rs = stmt.executeQuery(query);
          while (rs.next()) {
                ///fetch data

             }
       } catch (SQLException e ) {
             log.info("Exception connecting to database - " + e.toString());
        } finally {
          if (stmt != null) {

             try {
                stmt.close();
                   } catch (SQLException e) {
                // TODO Auto-generated catch block
                         e.printStackTrace();
                   }

           }
    }   }

     

    Above code after compilation shows  byte code errors like following when decompiled..like below

      /* Error */
    public static void connectToDB(Connection con) throws SQLException { 
    {
    // Byte code:
    // 0: bipush 80
    // 2: istore_1
    // 3: aconst_null
    // 4: astore_2
    // 5: ldc -28
    // 7: astore_3
    // 8: aload_0
    // 9: invokeinterface 230 1 0
    // 14: astore_2
    // 15: aload_2
    // 16: aload_3
    // 17: invokeinterface 236 2 0
    // 22: astore 4
    // 24: goto +40 -> 64
    // 27: aload 4
    // 29: ldc -14
    // 31: invokeinterface 244 2 0
    // 36: istore_1
    // 37: getstatic 18 com/arcot/sample/callout/helper/EvalCalloutHelper:log Lorg/apache/log4j/Logger;
    // 40: new 48 java/lang/StringBuilder
    // 43: dup
    // 44: ldc -6
    // 46: invokespecial 52 java/lang/StringBuilder:<init> (Ljava/lang/String;)V
    // 49: iload_1
    // 50: invokevirtual 252 java/lang/StringBuilder:append (I)Ljava/lang/StringBuilder;
    // 53: ldc -1
    // 55: invokevirtual 73 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
    // 58: invokevirtual 77 java/lang/StringBuilder:toString ()Ljava/lang/String;
    // 61: invokevirtual 44 org/apache/log4j/Logger:info (Ljava/lang/Object;)V
    // 64: aload 4
    // 66: invokeinterface 257 1 0
    // 71: ifne -44 -> 27
    // 74: goto +80 -> 154
    // 77: astore 4
    // 79: getstatic 18 com/arcot/sample/callout/helper/EvalCalloutHelper:log Lorg/apache/log4j/Logger;
    // 82: new 48 java/lang/StringBuilder
    // 85: dup
    // 86: ldc_w 261
    // 89: invokespecial 52 java/lang/StringBuilder:<init> (Ljava/lang/String;)V
    // 92: aload 4
    // 94: invokevirtual 263 java/sql/SQLException:toString ()Ljava/lang/String;
    // 97: invokevirtual 73 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
    // 100: invokevirtual 77 java/lang/StringBuilder:toString ()Ljava/lang/String;
    // 103: invokevirtual 44 org/apache/log4j/Logger:info (Ljava/lang/Object;)V
    // 106: aload_2
    // 107: ifnull +67 -> 174
    // 110: aload_2
    // 111: invokeinterface 266 1 0
    // 116: goto +58 -> 174
    // 119: astore 6
    // 121: aload 6
    // 123: invokevirtual 269 java/sql/SQLException:printStackTrace ()V
    // 126: goto +48 -> 174
    // 129: astore 5
    // 131: aload_2
    // 132: ifnull +19 -> 151
    // 135: aload_2
    // 136: invokeinterface 266 1 0
    // 141: goto +10 -> 151
    // 144: astore 6
    // 146: aload 6
    // 148: invokevirtual 269 java/sql/SQLException:printStackTrace ()V
    // 151: aload 5
    // 153: athrow
    // 154: aload_2
    // 155: ifnull +19 -> 174
    // 158: aload_2
    // 159: invokeinterface 266 1 0
    // 164: goto +10 -> 174
    // 167: astore 6
    // 169: aload 6
    // 171: invokevirtual 269 java/sql/SQLException:printStackTrace ()V
    // 174: iload_1
    // 175: ireturn
    // Line number table:
    // Java source line #102 -> byte code offset #0
    // Java source line #103 -> byte code offset #3
    // Java source line #104 -> byte code offset #5
    // Java source line #106 -> byte code offset #8
    // Java source line #107 -> byte code offset #15
    // Java source line #108 -> byte code offset #24
    // Java source line #109 -> byte code offset #27
    // Java source line #110 -> byte code offset #37
    // Java source line #108 -> byte code offset #64
    // Java source line #112 -> byte code offset #74
    // Java source line #113 -> byte code offset #79
    // Java source line #115 -> byte code offset #106
    // Java source line #116 -> byte code offset #110
    // Java source line #117 -> byte code offset #116
    // Java source line #119 -> byte code offset #121
    // Java source line #114 -> byte code offset #129
    // Java source line #115 -> byte code offset #131
    // Java source line #116 -> byte code offset #135
    // Java source line #117 -> byte code offset #141
    // Java source line #119 -> byte code offset #146
    // Java source line #121 -> byte code offset #151
    // Java source line #115 -> byte code offset #154
    // Java source line #116 -> byte code offset #158
    // Java source line #117 -> byte code offset #164
    // Java source line #119 -> byte code offset #169
    // Java source line #122 -> byte code offset #174
    // Local variable table:
    // start length slot name signature
    // 0 176 0 con Connection
    // 2 173 1 RiskScore int
    // 4 155 2 stmt java.sql.Statement
    // 7 10 3 query String
    // 22 43 4 rs java.sql.ResultSet
    // 77 16 4 e SQLException
    // 129 23 5 localObject Object
    // 119 3 6 e SQLException
    // 144 3 6 e SQLException
    // 167 3 6 e SQLException
    // Exception table:
    // from to target type
    // 8 74 77 java/sql/SQLException
    // 110 116 119 java/sql/SQLException
    // 8 106 129 finally
    // 135 141 144 java/sql/SQLException
    // 158 164 167 java/sql/SQLException

     

     

    Solution:- Remove finally block and it works and jd doesnt complain about byte code errors.



  • 2.  RE: java-decompiler issue (jd) with finally

    Posted Oct 21, 2020 10:54 AM
    Give a try to online Java Decompiler. It does not have this problem.