Search in sources :

Example 91 with JavaClass

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

the class Section508Compliance method visitClassContext.

/**
 * implements the visitor to create and clear the stack
 *
 * @param classContext
 *            the context object of the currently visited class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        if ((jcomponentClass != null) && (accessibleClass != null)) {
            JavaClass cls = classContext.getJavaClass();
            if (cls.instanceOf(jcomponentClass) && !cls.implementationOf(accessibleClass)) {
                bugReporter.reportBug(new BugInstance(this, BugType.S508C_NON_ACCESSIBLE_JCOMPONENT.name(), NORMAL_PRIORITY).addClass(cls));
            }
        }
        stack = new OpcodeStack();
        fieldLabels = new HashSet<>();
        localLabels = new HashMap<>();
        super.visitClassContext(classContext);
        for (XField fa : fieldLabels) {
            bugReporter.reportBug(new BugInstance(this, BugType.S508C_NO_SETLABELFOR.name(), NORMAL_PRIORITY).addClass(this).addField(fa));
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        stack = null;
        fieldLabels = null;
        localLabels = null;
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) XField(edu.umd.cs.findbugs.ba.XField) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 92 with JavaClass

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

the class Section508Compliance method processSetColorOps.

/**
 * looks for calls to set the color of components where the color isn't from UIManager
 *
 * @param methodName
 *            the method that is called
 *
 * @throws ClassNotFoundException
 *             if the gui component class can't be found
 */
private void processSetColorOps(String methodName) throws ClassNotFoundException {
    if ("setBackground".equals(methodName) || "setForeground".equals(methodName)) {
        int argCount = SignatureUtils.getNumParameters(getSigConstantOperand());
        if (stack.getStackDepth() > argCount) {
            OpcodeStack.Item item = stack.getStackItem(0);
            if (S508UserValue.FROM_UIMANAGER != item.getUserValue()) {
                item = stack.getStackItem(argCount);
                JavaClass cls = item.getJavaClass();
                if (((jcomponentClass != null) && cls.instanceOf(jcomponentClass)) || ((componentClass != null) && cls.instanceOf(componentClass))) {
                    bugReporter.reportBug(new BugInstance(this, BugType.S508C_SET_COMP_COLOR.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                }
            }
        }
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 93 with JavaClass

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

the class SillynessPotPourri method sawOpcode.

/**
 * implements the visitor to look for various silly bugs
 *
 * @param seen
 *            the opcode of the currently parsed instruction
 */
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "SF_SWITCH_FALLTHROUGH", justification = "This fall-through is deliberate and documented")
@Override
public void sawOpcode(int seen) {
    int reg = -1;
    SPPUserValue userValue = null;
    try {
        stack.precomputation(this);
        checkTrimLocations();
        if (isBranchByteCode(seen)) {
            Integer branchTarget = Integer.valueOf(getBranchTarget());
            BitSet branchInsSet = branchTargets.get(branchTarget);
            if (branchInsSet == null) {
                branchInsSet = new BitSet();
                branchTargets.put(branchTarget, branchInsSet);
            }
            branchInsSet.set(getPC());
        }
        // branch also matter here.
        if ((seen == Const.IFEQ) || (seen == Const.IFLE) || (seen == Const.IFNE)) {
            checkForEmptyStringAndNullChecks(seen);
        }
        // see above, several opcodes hit multiple branches.
        if ((seen == Const.IFEQ) || (seen == Const.IFNE) || (seen == Const.IFGT)) {
            checkSizeEquals0();
        }
        if (seen == Const.IFEQ) {
            checkNullAndInstanceOf();
        }
        switch(seen) {
            case Const.IFNE:
                checkNotEqualsStringBuilderLength();
                break;
            case Const.IFEQ:
                checkEqualsStringBufferLength();
                break;
            case Const.IRETURN:
                {
                    if (lastIfEqWasBoolean) {
                        checkForUselessTernaryReturn();
                    }
                }
            // $FALL-THROUGH$
            case Const.LRETURN:
            case Const.DRETURN:
            case Const.FRETURN:
            case Const.ARETURN:
                trimLocations.clear();
                break;
            case Const.LDC2_W:
                checkApproximationsOfMathConstants();
                break;
            case Const.DCMPL:
                checkCompareToNaNDouble();
                break;
            case Const.FCMPL:
                checkCompareToNaNFloat();
                break;
            case Const.ICONST_0:
            case Const.ICONST_1:
            case Const.ICONST_2:
            case Const.ICONST_3:
                userValue = sawIntConst();
                break;
            case Const.CALOAD:
                checkImproperToCharArrayUse();
                break;
            case Const.INVOKESTATIC:
                userValue = sawInvokeStatic();
                break;
            case Const.INVOKEVIRTUAL:
                userValue = sawInvokeVirtual();
                break;
            case Const.INVOKESPECIAL:
                sawInvokeSpecial();
                break;
            case Const.INVOKEINTERFACE:
                userValue = sawInvokeInterface();
                break;
            case Const.IFNULL:
            case Const.IFNONNULL:
                if (stack.getStackDepth() > 0) {
                    OpcodeStack.Item itm = stack.getStackItem(0);
                    JavaClass cls = itm.getJavaClass();
                    if ((cls != null) && optionalClasses.contains(cls.getClassName())) {
                        bugReporter.reportBug(new BugInstance(this, BugType.SPP_NULL_CHECK_ON_OPTIONAL.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                    }
                }
                break;
            default:
                if (OpcodeUtils.isALoad(seen)) {
                    sawLoad(seen);
                } else if (OpcodeUtils.isAStore(seen)) {
                    reg = RegisterUtils.getAStoreReg(this, seen);
                    checkTrimDupStore();
                    checkStutterdAssignment(seen, reg);
                    checkImmutableUsageOfStringBuilder(reg);
                }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        TernaryPatcher.pre(stack, seen);
        stack.sawOpcode(this, seen);
        TernaryPatcher.post(stack, seen);
        if ((stack.getStackDepth() > 0)) {
            OpcodeStack.Item item = stack.getStackItem(0);
            if (userValue != null) {
                item.setUserValue(userValue);
            } else {
                SPPUserValue uv = (SPPUserValue) item.getUserValue();
                if ((((uv != null) && (uv.getMethod() == SPPMethod.ITERATOR)) && (seen == Const.GETFIELD)) || (seen == Const.ALOAD) || ((seen >= Const.ALOAD_0) && (seen <= Const.ALOAD_3))) {
                    item.setUserValue(null);
                }
            }
        }
        lastOpcode = seen;
        lastReg = reg;
        System.arraycopy(lastPCs, 1, lastPCs, 0, 3);
        lastPCs[3] = getPC();
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass) BitSet(java.util.BitSet) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 94 with JavaClass

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

the class SillynessPotPourri method calendarBeforeAfterSilliness.

private void calendarBeforeAfterSilliness() {
    if (stack.getStackDepth() > 1) {
        OpcodeStack.Item item = stack.getStackItem(0);
        String itemSig = item.getSignature();
        // (BUG)
        if (!Values.SIG_JAVA_LANG_OBJECT.equals(itemSig) && !"Ljava/util/Calendar;".equals(itemSig) && !"Ljava/util/GregorianCalendar;".equals(itemSig)) {
            try {
                JavaClass cls = Repository.lookupClass(SignatureUtils.stripSignature(itemSig));
                if (!cls.instanceOf(calendarClass)) {
                    bugReporter.reportBug(new BugInstance(this, BugType.SPP_INVALID_CALENDAR_COMPARE.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                }
            } catch (ClassNotFoundException cnfe) {
                bugReporter.reportMissingClass(cnfe);
            }
        }
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantString(org.apache.bcel.classfile.ConstantString)

Example 95 with JavaClass

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

the class SillynessPotPourri method defaultToStringSilliness.

private void defaultToStringSilliness() throws ClassNotFoundException {
    if (stack.getStackDepth() >= 1) {
        OpcodeStack.Item item = stack.getStackItem(0);
        JavaClass toStringClass = item.getJavaClass();
        if (toStringClass != null) {
            String toStringClassName = toStringClass.getClassName();
            if (!toStringClass.isInterface() && !toStringClass.isAbstract() && !Values.DOTTED_JAVA_LANG_OBJECT.equals(toStringClassName) && !Values.DOTTED_JAVA_LANG_STRING.equals(toStringClassName) && toStringClasses.add(toStringClassName)) {
                try {
                    JavaClass cls = Repository.lookupClass(toStringClassName);
                    if (!hasToString(cls)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.SPP_NON_USEFUL_TOSTRING.name(), toStringClass.isFinal() ? NORMAL_PRIORITY : LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                    }
                } catch (ClassNotFoundException cnfe) {
                    bugReporter.reportMissingClass(cnfe);
                }
            }
        }
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantString(org.apache.bcel.classfile.ConstantString)

Aggregations

JavaClass (org.apache.bcel.classfile.JavaClass)144 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)45 BugInstance (edu.umd.cs.findbugs.BugInstance)43 Method (org.apache.bcel.classfile.Method)28 ToString (com.mebigfatguy.fbcontrib.utils.ToString)27 Field (org.apache.bcel.classfile.Field)17 HashSet (java.util.HashSet)14 HashMap (java.util.HashMap)11 ClassParser (org.apache.bcel.classfile.ClassParser)10 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)8 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)8 XField (edu.umd.cs.findbugs.ba.XField)7 Nullable (javax.annotation.Nullable)7 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)7 Type (org.apache.bcel.generic.Type)7 Iterator (java.util.Iterator)6 List (java.util.List)6 Map (java.util.Map)6 Set (java.util.Set)6