Search in sources :

Example 96 with JavaClass

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

the class SillynessPotPourri method equalsSilliness.

private void equalsSilliness(String className) {
    try {
        JavaClass cls = Repository.lookupClass(className);
        if (cls.isEnum()) {
            bugReporter.reportBug(new BugInstance(this, BugType.SPP_EQUALS_ON_ENUM.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
        } else {
            if (stack.getStackDepth() >= 2) {
                OpcodeStack.Item item = stack.getStackItem(1);
                cls = item.getJavaClass();
                if (cls != null) {
                    String clsName = cls.getClassName();
                    if (oddMissingEqualsClasses.contains(clsName)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.SPP_EQUALS_ON_STRING_BUILDER.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                    }
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantString(org.apache.bcel.classfile.ConstantString)

Example 97 with JavaClass

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

the class SluggishGui method visitClassContext.

/**
 * overrides the visitor to reset look for gui interfaces
 *
 * @param classContext
 *            the context object for the currently parsed class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        guiInterfaces = new HashSet<>();
        JavaClass cls = classContext.getJavaClass();
        JavaClass[] infs = cls.getAllInterfaces();
        for (JavaClass inf : infs) {
            String name = inf.getClassName();
            if ((name.startsWith("java.awt.") || name.startsWith("javax.swing.")) && name.endsWith("Listener")) {
                guiInterfaces.add(inf);
            }
        }
        if (!guiInterfaces.isEmpty()) {
            listenerCode = new LinkedHashMap<>();
            expensiveThisCalls = new HashSet<>();
            super.visitClassContext(classContext);
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        guiInterfaces = null;
        listenerCode = null;
        expensiveThisCalls = null;
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

Example 98 with JavaClass

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

the class StackedTryBlocks method sawOpcode.

/**
 * overrides the visitor to document what catch blocks do with regard to rethrowing the exceptions, and if the message is a static message
 *
 * @param seen
 *            the currently parsed opcode
 */
@Override
public void sawOpcode(int seen) {
    String message = null;
    try {
        stack.precomputation(this);
        if ((seen == Const.TABLESWITCH) || (seen == Const.LOOKUPSWITCH)) {
            int pc = getPC();
            for (int offset : getSwitchOffsets()) {
                transitionPoints.set(pc + offset);
            }
            transitionPoints.set(pc + getDefaultSwitchOffset());
        } else if (isBranch(seen) && (getBranchOffset() < 0)) {
            // throw out try blocks in loops, this could cause false
            // negatives
            // with two try/catches in one loop, but more unlikely
            Iterator<TryBlock> it = blocks.iterator();
            int target = getBranchTarget();
            while (it.hasNext()) {
                TryBlock block = it.next();
                if (block.getStartPC() >= target) {
                    it.remove();
                }
            }
        }
        int pc = getPC();
        TryBlock block = findBlockWithStart(pc);
        if (block != null) {
            inBlocks.add(block);
            block.setState(TryBlock.State.IN_TRY);
        }
        if (inBlocks.isEmpty()) {
            return;
        }
        TryBlock innerBlock = inBlocks.get(inBlocks.size() - 1);
        int nextPC = getNextPC();
        if (innerBlock.atHandlerPC(nextPC)) {
            if ((seen == Const.GOTO) || (seen == Const.GOTO_W)) {
                innerBlock.setEndHandlerPC(getBranchTarget());
            } else {
                inBlocks.remove(innerBlock);
                blocks.remove(innerBlock);
            }
        } else if (innerBlock.atHandlerPC(pc)) {
            innerBlock.setState(TryBlock.State.IN_CATCH);
        } else if (innerBlock.atEndHandlerPC(pc)) {
            inBlocks.remove(inBlocks.size() - 1);
            innerBlock.setState(TryBlock.State.AFTER);
        }
        if (transitionPoints.get(nextPC)) {
            if (innerBlock.inCatch() && (innerBlock.getEndHandlerPC() > nextPC)) {
                innerBlock.setEndHandlerPC(nextPC);
            }
        }
        if (innerBlock.inCatch()) {
            if (((seen >= Const.IFEQ) && ((seen <= Const.RET))) || (seen == Const.GOTO_W) || OpcodeUtils.isReturn(seen)) {
                blocks.remove(innerBlock);
                inBlocks.remove(inBlocks.size() - 1);
            } else if (seen == Const.ATHROW) {
                if (stack.getStackDepth() > 0) {
                    OpcodeStack.Item item = stack.getStackItem(0);
                    XMethod xm = item.getReturnValueOf();
                    if (xm != null) {
                        innerBlock.setThrowSignature(xm.getSignature());
                    }
                    innerBlock.setExceptionSignature(item.getSignature());
                    innerBlock.setMessage((String) item.getUserValue());
                } else {
                    inBlocks.remove(inBlocks.size() - 1);
                    innerBlock.setState(TryBlock.State.AFTER);
                }
            } else if ((seen == Const.INVOKESPECIAL) && Values.CONSTRUCTOR.equals(getNameConstantOperand())) {
                String cls = getClassConstantOperand();
                JavaClass exCls = Repository.lookupClass(cls);
                if (exCls.instanceOf(THROWABLE_CLASS)) {
                    String signature = getSigConstantOperand();
                    List<String> types = SignatureUtils.getParameterSignatures(signature);
                    if (!types.isEmpty()) {
                        if (Values.SIG_JAVA_LANG_STRING.equals(types.get(0)) && (stack.getStackDepth() >= types.size())) {
                            OpcodeStack.Item item = stack.getStackItem(types.size() - 1);
                            message = (String) item.getConstant();
                            if (message == null) {
                                message = "____UNKNOWN____" + System.identityHashCode(item);
                            }
                        }
                    } else {
                        message = "";
                    }
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        stack.sawOpcode(this, seen);
        if ((message != null) && (stack.getStackDepth() > 0)) {
            OpcodeStack.Item item = stack.getStackItem(0);
            item.setUserValue(message);
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) XMethod(edu.umd.cs.findbugs.ba.XMethod) Iterator(java.util.Iterator) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 99 with JavaClass

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

the class NonRecycleableTaglibs method visitClassContext.

/**
 * implements the visitor to look for classes that extend the TagSupport or BodyTagSupport class
 *
 * @param classContext
 *            the context object for the currently parsed class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        JavaClass[] superClasses = cls.getSuperClasses();
        for (JavaClass superCls : superClasses) {
            if (tagClasses.contains(superCls.getClassName())) {
                attributes = getAttributes(cls);
                if (!attributes.isEmpty()) {
                    methodWrites = new HashMap<>();
                    fieldAnnotations = new HashMap<>();
                    super.visitClassContext(classContext);
                    reportBugs();
                }
                break;
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        attributes = null;
        methodWrites = null;
        fieldAnnotations = null;
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

Example 100 with JavaClass

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

the class NonSymmetricEquals method sawOpcode.

/**
 * implements the visitor to look for checkcasts of the parameter to other types, and enter instances in a map for further processing in doReport.
 *
 * @param seen
 *            the opcode of the currently parsed instruction
 */
@Override
public void sawOpcode(int seen) {
    try {
        stack.precomputation(this);
        if ((seen == Const.CHECKCAST) && (stack.getStackDepth() > 0)) {
            OpcodeStack.Item item = stack.getStackItem(0);
            if (item.getRegisterNumber() == 1) {
                String thisCls = getClassName();
                String equalsCls = getClassConstantOperand();
                if (!thisCls.equals(equalsCls)) {
                    JavaClass thisJavaClass = getClassContext().getJavaClass();
                    JavaClass equalsJavaClass = Repository.lookupClass(equalsCls);
                    boolean inheritance = thisJavaClass.instanceOf(equalsJavaClass) || equalsJavaClass.instanceOf(thisJavaClass);
                    BugInstance bug = new BugInstance(this, BugType.NSE_NON_SYMMETRIC_EQUALS.name(), inheritance ? LOW_PRIORITY : NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this).addString(equalsCls);
                    Map<String, BugInstance> bugs = possibleBugs.get(thisCls);
                    if (bugs == null) {
                        bugs = new HashMap<>();
                        possibleBugs.put(thisCls, bugs);
                    }
                    bugs.put(equalsCls, bug);
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance)

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