2013-02-04 35 views
-2

如何在java類或java中使用asm找到在java類中識別開關盒的操作碼。在javassist中識別開關盒的操作碼

我用下面的代碼片段

List methods = lClassFile.getMethods(); 
     for (Object m : methods) { 
      MethodInfo lMethodInfo = (MethodInfo) m; 
      System.out.println("method name: " + lMethodInfo.getName()); 
      CodeAttribute ca = lMethodInfo.getCodeAttribute(); 
      for (CodeIterator ci = ca.iterator(); ci.hasNext();) { 
       int index = ci.next(); 
       int op = ci.byteAt(index); 

       if (op == Opcode.TABLESWITCH) { 
        int a1 = ci.s16bitAt(index + 1); 
        String fieldrefType = " " + lClassFile.getConstPool().getFieldrefType(a1); 
        String fieldName = " " + lClassFile.getConstPool().getFieldrefName(a1); 
        String fieldrefClassName = " " + lClassFile.getConstPool().getFieldrefClassName(a1); 
        System.out.println("1 -> FieldrefType = " + fieldrefType + " field name: " + fieldName + "   FieldrefClassName name: " + fieldrefClassName); 
       } 
       if (op == Opcode.LOOKUPSWITCH) { 
        int a1 = ci.s16bitAt(index + 1); 
        String fieldrefType = " " + lClassFile.getConstPool().getFieldrefType(a1); 
        String fieldName = " " + lClassFile.getConstPool().getFieldrefName(a1); 
        String fieldrefClassName = " " + lClassFile.getConstPool().getFieldrefClassName(a1); 
        System.out.println("2 -> FieldrefType = " + fieldrefType + " field name: " + fieldName + "   FieldrefClassName name: " + fieldrefClassName); 
       } 
      } 
     } 

我輸出:

method name: <init> 
method name: execute 
2 -> FieldrefType = null field name: null   FieldrefClassName name: null 
1 -> FieldrefType = null field name: null   FieldrefClassName name: null 
method name: validate 
2 -> FieldrefType = null field name: null   FieldrefClassName name: null 
2 -> FieldrefType = null field name: null   FieldrefClassName name: null 
method name: postExecute 
2 -> FieldrefType = null field name: null   FieldrefClassName name: null 
2 -> FieldrefType = null field name: null   FieldrefClassName name: null 

廣東話能夠得到開關的情況下的值。 請幫助我....

回答

0
  1. 寫有隻包含一個非常簡單的開關/ case語句
  2. 的方法的類編譯類
  3. 使用
  4. 看字節碼javap -c MyClass

該字節碼包含TABLESWITCHLOOKUPSWITCH和javassists Opcode類具有相應的靜態字段。

+0

謝謝,我已經試過如上...但不能得到... – Prabhu