Search in sources :

Example 11 with LocalVariable

use of org.apache.bcel.classfile.LocalVariable in project fb-contrib by mebigfatguy.

the class UnusedParameter method visitCode.

/**
 * implements the visitor to clear the parm set, and check for potential methods
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    unusedParms.clear();
    regToParm.clear();
    stack.resetForMethodEntry(this);
    Method m = getMethod();
    String methodName = m.getName();
    if (IGNORE_METHODS.contains(methodName)) {
        return;
    }
    int accessFlags = m.getAccessFlags();
    if (((accessFlags & (Const.ACC_STATIC | Const.ACC_PRIVATE)) == 0) || ((accessFlags & Const.ACC_SYNTHETIC) != 0)) {
        return;
    }
    List<String> parmTypes = SignatureUtils.getParameterSignatures(m.getSignature());
    if (parmTypes.isEmpty()) {
        return;
    }
    int firstReg = 0;
    if ((accessFlags & Const.ACC_STATIC) == 0) {
        ++firstReg;
    }
    int reg = firstReg;
    for (int i = 0; i < parmTypes.size(); ++i) {
        unusedParms.set(reg);
        regToParm.put(Integer.valueOf(reg), Integer.valueOf(i + 1));
        String parmSig = parmTypes.get(i);
        reg += SignatureUtils.getSignatureSize(parmSig);
    }
    try {
        super.visitCode(obj);
        if (!unusedParms.isEmpty()) {
            LocalVariableTable lvt = m.getLocalVariableTable();
            reg = unusedParms.nextSetBit(firstReg);
            while (reg >= 0) {
                LocalVariable lv = (lvt == null) ? null : lvt.getLocalVariable(reg, 0);
                if (lv != null) {
                    String parmName = lv.getName();
                    bugReporter.reportBug(new BugInstance(this, BugType.UP_UNUSED_PARAMETER.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addString("Parameter " + regToParm.get(Integer.valueOf(reg)) + ": " + parmName));
                }
                reg = unusedParms.nextSetBit(reg + 1);
            }
        }
    } catch (StopOpcodeParsingException e) {
    // no unusedParms left
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) LocalVariable(org.apache.bcel.classfile.LocalVariable) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 12 with LocalVariable

use of org.apache.bcel.classfile.LocalVariable in project fb-contrib by mebigfatguy.

the class DeletingWhileIterating method buildVariableEndScopeMap.

private void buildVariableEndScopeMap() {
    LocalVariableTable lvt = getMethod().getLocalVariableTable();
    if ((lvt == null) || (lvt.getLength() == 0)) {
        endOfScopes = Collections.emptyMap();
    } else {
        int len = lvt.getLength();
        endOfScopes = new HashMap<>((int) (len * 1.6));
        for (int i = 0; i < len; i++) {
            @SuppressWarnings("deprecation") LocalVariable lv = lvt.getLocalVariable(i);
            if (lv != null) {
                Integer endPC = Integer.valueOf(lv.getStartPC() + lv.getLength());
                BitSet vars = endOfScopes.get(endPC);
                if (vars == null) {
                    vars = new BitSet();
                    endOfScopes.put(endPC, vars);
                }
                vars.set(lv.getIndex());
            }
        }
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) LocalVariable(org.apache.bcel.classfile.LocalVariable) BitSet(java.util.BitSet)

Example 13 with LocalVariable

use of org.apache.bcel.classfile.LocalVariable in project fb-contrib by mebigfatguy.

the class ExceptionSoftening method updateEndPCsOnCatchRegScope.

/**
 * reduces the end pc based on the optional LocalVariableTable's exception register scope
 *
 * @param infos
 *            the list of active catch blocks
 * @param pc
 *            the current pc
 * @param seen
 *            the currently parsed opcode
 */
private void updateEndPCsOnCatchRegScope(List<CatchInfo> infos, int pc, int seen) {
    if (lvt != null) {
        for (CatchInfo ci : infos) {
            if ((ci.getStart() == pc) && OpcodeUtils.isAStore(seen)) {
                int exReg = RegisterUtils.getAStoreReg(this, seen);
                LocalVariable lv = lvt.getLocalVariable(exReg, pc + 1);
                if (lv != null) {
                    ci.setFinish(lv.getStartPC() + lv.getLength());
                }
                break;
            }
        }
    }
}
Also used : LocalVariable(org.apache.bcel.classfile.LocalVariable)

Example 14 with LocalVariable

use of org.apache.bcel.classfile.LocalVariable in project fb-contrib by mebigfatguy.

the class ContraVariantArrayAssignment method sawOpcode.

@Override
public void sawOpcode(int seen) {
    try {
        stack.precomputation(this);
        switch(seen) {
            case Const.ASTORE:
            case Const.ASTORE_0:
            case Const.ASTORE_1:
            case Const.ASTORE_2:
            case Const.ASTORE_3:
                if (stack.getStackDepth() > 0) {
                    LocalVariable lv = getMethod().getLocalVariableTable().getLocalVariable(RegisterUtils.getAStoreReg(this, seen), getNextPC());
                    if (lv != null) {
                        OpcodeStack.Item item = stack.getStackItem(0);
                        String sourceSignature = item.getSignature();
                        String targetSignature = lv.getSignature();
                        checkSignatures(sourceSignature, targetSignature);
                    }
                }
                break;
            case Const.PUTFIELD:
            case Const.PUTSTATIC:
                if (stack.getStackDepth() > 0) {
                    OpcodeStack.Item item = stack.getStackItem(0);
                    String sourceSignature = item.getSignature();
                    String targetSignature = getSigConstantOperand();
                    checkSignatures(sourceSignature, targetSignature);
                }
                break;
            case Const.AASTORE:
                /*
                 * OpcodeStack.Item arrayref = stack.getStackItem(2); OpcodeStack.Item value =
                 * stack.getStackItem(0);
                 *
                 * if(!value.isNull()) { String sourceSignature = value.getSignature(); String
                 * targetSignature = arrayref.getSignature(); if
                 * (!"Ljava/lang/Object;".equals(targetSignature)) { try{
                 * if(Type.getType(sourceSignature) instanceof ObjectType ) { ObjectType
                 * sourceType = (ObjectType) Type.getType(sourceSignature); ObjectType
                 * targetType = (ObjectType) ((ArrayType)
                 * Type.getType(targetSignature)).getBasicType();
                 * if(!sourceType.equals(targetType) && !sourceType.subclassOf(targetType)){
                 * bugReporter.reportBug(new BugInstance(this,
                 * BugType.CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT.name(), HIGH_PRIORITY)
                 * .addClass(this) .addMethod(this) .addSourceLine(this)); } } } catch
                 * (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } } }
                 */
                break;
        }
        super.sawOpcode(seen);
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) LocalVariable(org.apache.bcel.classfile.LocalVariable)

Example 15 with LocalVariable

use of org.apache.bcel.classfile.LocalVariable in project fb-contrib by mebigfatguy.

the class LostExceptionStackTrace method updateExceptionRegister.

/**
 * looks to update the catchinfo block with the register used for the exception variable. If their is a local variable table, but the local variable can't
 * be found return false, signifying an empty catch block.
 *
 * @param ci
 *            the catchinfo record for the catch starting at this pc
 * @param seen
 *            the opcode of the currently visited instruction
 * @param pc
 *            the current pc
 *
 * @return whether the catch block is empty
 */
private boolean updateExceptionRegister(CatchInfo ci, int seen, int pc) {
    if (OpcodeUtils.isAStore(seen)) {
        int reg = RegisterUtils.getAStoreReg(this, seen);
        ci.setReg(reg);
        exReg.put(Integer.valueOf(reg), Boolean.TRUE);
        LocalVariableTable lvt = getMethod().getLocalVariableTable();
        if (lvt != null) {
            LocalVariable lv = lvt.getLocalVariable(reg, pc + 2);
            if (lv != null) {
                int finish = lv.getStartPC() + lv.getLength();
                if (finish < ci.getFinish()) {
                    ci.setFinish(finish);
                }
            } else {
                return false;
            }
        }
    }
    return true;
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) LocalVariable(org.apache.bcel.classfile.LocalVariable)

Aggregations

LocalVariable (org.apache.bcel.classfile.LocalVariable)16 LocalVariableTable (org.apache.bcel.classfile.LocalVariableTable)12 BugInstance (edu.umd.cs.findbugs.BugInstance)5 ToString (com.mebigfatguy.fbcontrib.utils.ToString)3 ArrayList (java.util.ArrayList)3 Attribute (org.apache.bcel.classfile.Attribute)2 Code (org.apache.bcel.classfile.Code)2 Constant (org.apache.bcel.classfile.Constant)2 ConstantPool (org.apache.bcel.classfile.ConstantPool)2 ConstantString (org.apache.bcel.classfile.ConstantString)2 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)2 BugType (com.mebigfatguy.fbcontrib.utils.BugType)1 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)1 FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)1 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)1 Item (edu.umd.cs.findbugs.OpcodeStack.Item)1 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1