0

我需要识别 Java 代码中未使用的变量(参数、局部变量、类成员变量)。基本上我必须使用 BCEL 来访问字节码并达到我的目标。

我已经使用 ClassGen 来获取所有调用的方法,然后通过使用 MethodGen 我设法获取所有局部变量和函数参数。但是我仍然无法区分已使用和未使用的变量。

我猜我必须访问 JVM 堆栈才能查看实际加载了哪些变量以及未使用哪些变量。

所以问题很简单:如何使用 BCEL 访问 JVM 堆栈?

4

1 回答 1

1

通过获取方法的指令列表,可以从字节码中检测出使用/未使用的变量。

我之前没有尝试过,这只是一个实验,表明它是完全可能的,但我希望有一个比这更简单,更优雅的方法:

import org.apache.bcel.Repository;
import org.apache.bcel.classfile.Constant;
import org.apache.bcel.classfile.ConstantFieldref;
import org.apache.bcel.classfile.ConstantNameAndType;
import org.apache.bcel.classfile.ConstantPool;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.LocalVariable;
import org.apache.bcel.classfile.LocalVariableTable;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.GETFIELD;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.LoadInstruction;
import org.apache.bcel.generic.LocalVariableInstruction;
import org.apache.bcel.generic.MethodGen;

public class UnusedVariablesTest
{
    private int usedInt;
    private String usedString;
    private int unusedInt;
    private String unusedString;

    public static void main(String[] args) throws ClassNotFoundException
    {
        String className = "UnusedVariablesTest";
        JavaClass c = Repository.lookupClass(className);
        ConstantPool cp = c.getConstantPool();
        ConstantPoolGen cpg = new ConstantPoolGen(cp);
        for (Method m : c.getMethods())
        {
            //System.out.println("Method "+m);
            MethodGen mg = new MethodGen(m, className, cpg);
            InstructionList il = mg.getInstructionList();
            InstructionHandle[] ihs = il.getInstructionHandles();
             for(int i=0; i < ihs.length; i++) {
                InstructionHandle ih = ihs[i];
                Instruction instruction = ih.getInstruction();
                //System.out.println("    "+instruction);
                if (instruction instanceof LocalVariableInstruction)
                {
                    LocalVariableInstruction lvi = (LocalVariableInstruction)instruction;
                    LocalVariableTable lvt = m.getLocalVariableTable();
                    int index = lvi.getIndex();
                    LocalVariable lv = lvt.getLocalVariable(index, ih.getPosition());
                    if (lv != null)
                    {
                        System.out.println("Using "+lv.getName());
                    }
                }
                else if (instruction instanceof GETFIELD)
                {
                    GETFIELD getfield = (GETFIELD)instruction;
                    int index = getfield.getIndex();
                    Constant constant = cp.getConstant(index);
                    if (constant instanceof ConstantFieldref)
                    {
                        ConstantFieldref cfr = (ConstantFieldref)constant;
                        Constant constant2 = cp.getConstant(cfr.getNameAndTypeIndex());
                        if (constant2 instanceof ConstantNameAndType)
                        {
                            ConstantNameAndType cnat = (ConstantNameAndType)constant2;
                            System.out.println("Using "+cnat.getName(cp));
                        }
                    }
                }
             }
        }
    }

    void someMethod(int usedIntArgument, int unusedIntArgument)
    {
        System.out.println(usedInt+usedString+usedIntArgument);
    }
}

至少它会打印出使用的字段、参数和局部变量,这可以作为检测未使用变量的基础。

于 2014-03-07T00:04:17.143 回答