-2

javassist または asm を使用して Java クラスでスイッチ ケースを識別するためのオペコードを見つける方法。

以下のスニペットを使用しました

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

スイッチ ケースの値を取得できません。お願い助けて....

4

1 に答える 1

1
  1. 非常に単純なswitch/caseステートメントのみを含むメソッドを使用してクラスを記述します
  2. そのクラスをコンパイルする
  3. を使用してバイトコードを見てくださいjavap -c MyClass

バイトコードにはTABLESWITCHorが含まれLOOKUPSWITCH、javassistsOpcodeクラスには対応する静的フィールドがあります。

于 2013-02-04T05:25:02.420 に答える