Search in sources :

Example 6 with XMethod

use of edu.umd.cs.findbugs.ba.XMethod in project fb-contrib by mebigfatguy.

the class XClassUtils method getXMethod.

/**
 * Looks for the method up the class hierarchy.
 *
 * @param xClass
 *            the class where to look for the method
 * @param methodName
 *            the name of the method to look for
 * @param methodSig
 *            the signature of the method to look for
 * @return the method
 */
@Nullable
public static XMethod getXMethod(final XClass xClass, final String methodName, final String methodSig) {
    if (xClass == null) {
        return null;
    }
    XMethod xMethod = xClass.findMethod(methodName, methodSig, false);
    if (xMethod == null) {
        ClassDescriptor descriptor = xClass.getSuperclassDescriptor();
        if (descriptor != null) {
            final XClass superClass = getXClass(descriptor);
            xMethod = getXMethod(superClass, methodName, methodSig);
        }
    }
    return xMethod;
}
Also used : ClassDescriptor(edu.umd.cs.findbugs.classfile.ClassDescriptor) XMethod(edu.umd.cs.findbugs.ba.XMethod) XClass(edu.umd.cs.findbugs.ba.XClass) Nullable(javax.annotation.Nullable)

Example 7 with XMethod

use of edu.umd.cs.findbugs.ba.XMethod in project fb-contrib by mebigfatguy.

the class MapUsageIssues method sawOpcode.

@Override
public void sawOpcode(int seen) {
    try {
        if (!mapContainsKeyUsed.isEmpty()) {
            Iterator<Map.Entry<MapRef, ContainsKey>> it = mapContainsKeyUsed.entrySet().iterator();
            int pc = getPC();
            while (it.hasNext()) {
                Map.Entry<MapRef, ContainsKey> entry = it.next();
                if (!entry.getKey().isValid() || entry.getValue().outOfScope(pc)) {
                    it.remove();
                }
            }
        }
        // checking for a branch might be overkill, but for now lets go with it
        if (!mapGetUsed.isEmpty() && OpcodeUtils.isBranch(seen)) {
            Iterator<Map.Entry<MapRef, Get>> it = mapGetUsed.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<MapRef, Get> entry = it.next();
                it.remove();
            }
        }
        if ((seen == Const.IFNULL) || (seen == Const.IFNONNULL)) {
            if (stack.getStackDepth() > 0) {
                OpcodeStack.Item itm = stack.getStackItem(0);
                XMethod method = itm.getReturnValueOf();
                if ((method != null) && (mapClass != null)) {
                    if (COLLECTION_ACCESSORS.contains(method.getName())) {
                        JavaClass cls = Repository.lookupClass(method.getClassName());
                        if (cls.implementationOf(mapClass)) {
                            bugReporter.reportBug(new BugInstance(this, BugType.MUI_NULL_CHECK_ON_MAP_SUBSET_ACCESSOR.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                        }
                    }
                }
            }
        } else if (seen == Const.INVOKEINTERFACE) {
            FQMethod fqm = new FQMethod(getClassConstantOperand(), getNameConstantOperand(), getSigConstantOperand());
            if (CONTAINS_METHOD.equals(fqm)) {
                if (stack.getStackDepth() >= 2) {
                    OpcodeStack.Item item = stack.getStackItem(1);
                    if ((item.getRegisterNumber() < 0) && (item.getXField() == null)) {
                        XMethod xm = item.getReturnValueOf();
                        if ((xm != null) && COLLECTION_ACCESSORS.contains(xm.getName()) && Values.DOTTED_JAVA_UTIL_MAP.equals(xm.getClassName())) {
                            bugReporter.reportBug(new BugInstance(this, BugType.MUI_USE_CONTAINSKEY.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                        }
                    }
                }
            } else if (CONTAINSKEY_METHOD.equals(fqm)) {
                if (getNextOpcode() == Const.IFEQ) {
                    int ifEnd = getNextPC() + CodeByteUtils.getshort(getCode().getCode(), getNextPC() + 1);
                    if (stack.getStackDepth() >= 2) {
                        OpcodeStack.Item itm = stack.getStackItem(1);
                        mapContainsKeyUsed.put(new MapRef(itm), new ContainsKey(stack.getStackItem(0), ifEnd));
                    }
                }
            } else if (GET_METHOD.equals(fqm)) {
                if (stack.getStackDepth() >= 2) {
                    OpcodeStack.Item itm = stack.getStackItem(1);
                    ContainsKey ck = mapContainsKeyUsed.remove(new MapRef(itm));
                    if ((ck != null) && new ContainsKey(stack.getStackItem(0), 0).equals(ck)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.MUI_CONTAINSKEY_BEFORE_GET.name(), ck.getReportLevel()).addClass(this).addMethod(this).addSourceLine(this));
                    }
                    mapGetUsed.put(new MapRef(itm), new Get(stack.getStackItem(0)));
                }
            } else if (REMOVE_METHOD.equals(fqm)) {
                if (stack.getStackDepth() >= 2) {
                    OpcodeStack.Item itm = stack.getStackItem(1);
                    Get get = mapGetUsed.remove(new MapRef(itm));
                    if ((get != null) && new Get(stack.getStackItem(0)).equals(get)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.MUI_GET_BEFORE_REMOVE.name(), get.getReportLevel()).addClass(this).addMethod(this).addSourceLine(this));
                    }
                }
            }
            QMethod qm = new QMethod(getNameConstantOperand(), getSigConstantOperand());
            if (SIZE_METHOD.equals(qm)) {
                if (stack.getStackDepth() > 0) {
                    OpcodeStack.Item itm = stack.getStackItem(0);
                    if ((itm.getRegisterNumber() < 0) && (itm.getXField() == null)) {
                        XMethod xm = itm.getReturnValueOf();
                        if ((xm != null) && Values.DOTTED_JAVA_UTIL_MAP.equals(xm.getClassName()) && COLLECTION_ACCESSORS.contains(xm.getName())) {
                            bugReporter.reportBug(new BugInstance(this, BugType.MUI_CALLING_SIZE_ON_SUBCONTAINER.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                        }
                    }
                }
            }
        }
    } catch (ClassNotFoundException e) {
        bugReporter.reportMissingClass(e);
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) BugInstance(edu.umd.cs.findbugs.BugInstance) QMethod(com.mebigfatguy.fbcontrib.utils.QMethod) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod) JavaClass(org.apache.bcel.classfile.JavaClass) XMethod(edu.umd.cs.findbugs.ba.XMethod) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with XMethod

use of edu.umd.cs.findbugs.ba.XMethod in project fb-contrib by mebigfatguy.

the class InefficientStringBuffering method sawInvokeSpecial.

private ISBUserValue sawInvokeSpecial() {
    ISBUserValue userValue = null;
    String calledClass = getClassConstantOperand();
    if (SignatureUtils.isPlainStringConvertableClass(calledClass) && Values.CONSTRUCTOR.equals(getNameConstantOperand())) {
        String signature = getSigConstantOperand();
        if (SignatureBuilder.SIG_VOID_TO_VOID.equals(signature)) {
            OpcodeStack.Item itm = getStringBufferItemAt(2);
            if (itm != null) {
                userValue = new ISBUserValue(AppendType.NESTED);
            }
        } else if (SignatureBuilder.SIG_STRING_TO_VOID.equals(signature) && (stack.getStackDepth() > 0)) {
            OpcodeStack.Item itm = stack.getStackItem(0);
            userValue = (ISBUserValue) itm.getUserValue();
            if ((userValue != null) && (userValue.getAppendType() == AppendType.NESTED)) {
                bugReporter.reportBug(new BugInstance(this, BugType.ISB_INEFFICIENT_STRING_BUFFERING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
            }
            if (userValue == null) {
                XMethod m = itm.getReturnValueOf();
                if ((m != null) && ("valueOf".equals(m.getName()) && Values.DOTTED_JAVA_LANG_STRING.equals(m.getClassName()))) {
                    userValue = new ISBUserValue(AppendType.CLEAR, false);
                } else {
                    userValue = new ISBUserValue(AppendType.CLEAR, true);
                }
            }
        }
    }
    return userValue;
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) XMethod(edu.umd.cs.findbugs.ba.XMethod) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantString(org.apache.bcel.classfile.ConstantString)

Example 9 with XMethod

use of edu.umd.cs.findbugs.ba.XMethod in project fb-contrib by mebigfatguy.

the class LocalHangingExecutor method lookForCustomThreadFactoriesInConstructors.

private void lookForCustomThreadFactoriesInConstructors(int seen) {
    try {
        stack.precomputation(this);
        if (seen == Const.PUTFIELD) {
            XField f = getXFieldOperand();
            if ((f != null) && hangableSig.contains(f.getSignature())) {
                // look at the top of the stack, get the arguments passed
                // into the function that was called
                // and then pull out the types.
                // if the last type is a ThreadFactory, set the priority to
                // low
                XMethod method = stack.getStackItem(0).getReturnValueOf();
                if (method != null) {
                    List<String> argumentTypes = SignatureUtils.getParameterSignatures(method.getSignature());
                    if ((!argumentTypes.isEmpty()) && "Ljava/util/concurrent/ThreadFactory;".equals(argumentTypes.get(argumentTypes.size() - 1))) {
                        AnnotationPriority ap = this.hangingFieldCandidates.get(f);
                        if (ap != null) {
                            ap.priority = LOW_PRIORITY;
                            this.hangingFieldCandidates.put(f, ap);
                        }
                    }
                } else {
                    // if the object is initialized from parameter, it's not this class's job to
                    // close it
                    int reg = stack.getStackItem(0).getRegisterNumber();
                    if (reg >= 0) {
                        Map<Integer, String> ctorParmInfo = SignatureUtils.getParameterSlotAndSignatures(false, getMethod().getSignature());
                        if (ctorParmInfo.containsKey(Integer.valueOf(reg))) {
                            hangingFieldCandidates.remove(f);
                        }
                    }
                }
            }
        }
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : XField(edu.umd.cs.findbugs.ba.XField) XMethod(edu.umd.cs.findbugs.ba.XMethod) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 10 with XMethod

use of edu.umd.cs.findbugs.ba.XMethod in project wcomponents by BorderTech.

the class CheckGetComponentModel method sawOpcode.

/**
 * {@inheritDoc}
 */
@Override
public void sawOpcode(final int seen) {
    String methodName = getMethodName();
    boolean setter = methodName.startsWith("set");
    boolean getter = methodName.startsWith("get");
    String bug = null;
    int priority = NORMAL_PRIORITY;
    switch(seen) {
        case INVOKEVIRTUAL:
            {
                if (util.isWComponent(getClassConstantOperand())) {
                    // We don't check the specific return type as the code wouldn't have compiled if it's not a ComponentModel.
                    if (setter && "getComponentModel".equals(getNameConstantOperand()) && getSigConstantOperand().startsWith("()L")) {
                        // Suspicious to call getComponentModel in a setter,
                        // but will not necessarily lead to application errors.
                        bug = "WCGETM_INCORRECT_USE_OF_GETCOMPONENTMODEL";
                    } else if (getter && !"getOrCreateComponentModel".equals(methodName) && "getOrCreateComponentModel".equals(getNameConstantOperand()) && getSigConstantOperand().startsWith("()L")) {
                        // Suspicious to call getOrCreateComponentModel in a getter,
                        // but will not necessarily lead to application errors.
                        bug = "WCGETM_INCORRECT_USE_OF_GETORCREATECOMPONENTMODEL";
                    }
                } else if (util.isComponentModel(getClassConstantOperand()) && getNameConstantOperand().startsWith("set")) {
                    // TODO: this may not work if there are any double or long args.
                    Item model = stack.getStackItem(getNumberMethodArguments());
                    XMethod from = model.getReturnValueOf();
                    if (from != null && "getComponentModel".equals(from.getName()) && from.getSignature().startsWith("()L") && util.isWComponent(from.getClassName())) {
                        bug = "WCGETM_INCORRECT_USE_OF_GETCOMPONENTMODEL";
                        priority = HIGH_PRIORITY;
                    }
                }
                break;
            }
        case PUTFIELD:
            {
                if (util.isComponentModel(getClassConstantOperand())) {
                    Item model = stack.getStackItem(1);
                    XMethod from = model.getReturnValueOf();
                    if (from != null && "getComponentModel".equals(from.getName()) && from.getSignature().startsWith("()L") && util.isWComponent(from.getClassName())) {
                        bug = "WCGETM_INCORRECT_USE_OF_GETCOMPONENTMODEL";
                        priority = HIGH_PRIORITY;
                    }
                }
            }
    }
    if (bug != null) {
        util.getBugReporter().reportBug(new BugInstance(this, bug, priority).addClass(this).addMethod(MethodAnnotation.fromVisitedMethod(this)).addSourceLine(this, getPC()));
    }
}
Also used : Item(edu.umd.cs.findbugs.OpcodeStack.Item) XMethod(edu.umd.cs.findbugs.ba.XMethod) BugInstance(edu.umd.cs.findbugs.BugInstance)

Aggregations

XMethod (edu.umd.cs.findbugs.ba.XMethod)20 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)13 BugInstance (edu.umd.cs.findbugs.BugInstance)12 ToString (com.mebigfatguy.fbcontrib.utils.ToString)8 FQMethod (com.mebigfatguy.fbcontrib.utils.FQMethod)7 XField (edu.umd.cs.findbugs.ba.XField)5 JavaClass (org.apache.bcel.classfile.JavaClass)4 Nullable (javax.annotation.Nullable)3 QMethod (com.mebigfatguy.fbcontrib.utils.QMethod)2 Item (edu.umd.cs.findbugs.OpcodeStack.Item)2 XClass (edu.umd.cs.findbugs.ba.XClass)2 ClassDescriptor (edu.umd.cs.findbugs.classfile.ClassDescriptor)2 BitSet (java.util.BitSet)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MethodInfo (com.mebigfatguy.fbcontrib.collect.MethodInfo)1 Statistics (com.mebigfatguy.fbcontrib.collect.Statistics)1 FieldDescriptor (edu.umd.cs.findbugs.classfile.FieldDescriptor)1 MethodDescriptor (edu.umd.cs.findbugs.classfile.MethodDescriptor)1 AnalysisPass (edu.umd.cs.findbugs.plan.AnalysisPass)1