Search in sources :

Example 6 with SourceLineAnnotation

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

the class PartiallyConstructedObjectAccess method visitCode.

@Override
public void visitCode(final Code obj) {
    stack.resetForMethodEntry(this);
    String methodName = getMethodName();
    isCtor = Values.CONSTRUCTOR.equals(methodName);
    if (!Values.STATIC_INITIALIZER.equals(methodName)) {
        Method m = getMethod();
        methodToCalledMethods.put(m, new HashMap<Method, SourceLineAnnotation>());
        try {
            super.visitCode(obj);
            if (methodToCalledMethods.get(m).isEmpty()) {
                methodToCalledMethods.remove(getMethod());
            }
        } catch (StopOpcodeParsingException e) {
            methodToCalledMethods.remove(getMethod());
        }
    }
}
Also used : SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) Method(org.apache.bcel.classfile.Method)

Example 7 with SourceLineAnnotation

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

the class PartiallyConstructedObjectAccess method foundPrivateInChain.

@Nullable
private Deque<SourceLineAnnotation> foundPrivateInChain(Method m, Set<Method> checkedMethods) {
    Map<Method, SourceLineAnnotation> calledMethods = methodToCalledMethods.get(m);
    if (calledMethods != null) {
        for (Map.Entry<Method, SourceLineAnnotation> entry : calledMethods.entrySet()) {
            Method cm = entry.getKey();
            if (checkedMethods.contains(cm)) {
                continue;
            }
            if (!cm.isPrivate() && !cm.isFinal()) {
                Deque<SourceLineAnnotation> slas = new ArrayDeque<>();
                slas.addLast(entry.getValue());
                return slas;
            }
            checkedMethods.add(cm);
            Deque<SourceLineAnnotation> slas = foundPrivateInChain(cm, checkedMethods);
            if (slas != null) {
                slas.addFirst(entry.getValue());
                return slas;
            }
        }
    }
    return null;
}
Also used : SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) Method(org.apache.bcel.classfile.Method) HashMap(java.util.HashMap) Map(java.util.Map) ArrayDeque(java.util.ArrayDeque) Nullable(javax.annotation.Nullable)

Example 8 with SourceLineAnnotation

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

the class PartiallyConstructedObjectAccess method reportChainedMethods.

private void reportChainedMethods() {
    Set<Method> checkedMethods = new HashSet<>();
    JavaClass cls = getClassContext().getJavaClass();
    for (Map.Entry<Method, Map<Method, SourceLineAnnotation>> entry : methodToCalledMethods.entrySet()) {
        Method m = entry.getKey();
        if (Values.CONSTRUCTOR.equals(m.getName())) {
            checkedMethods.clear();
            Deque<SourceLineAnnotation> slas = foundPrivateInChain(m, checkedMethods);
            if (slas != null) {
                BugInstance bi = new BugInstance(this, BugType.PCOA_PARTIALLY_CONSTRUCTED_OBJECT_ACCESS.name(), LOW_PRIORITY).addClass(cls).addMethod(cls, m);
                for (SourceLineAnnotation sla : slas) {
                    bi.addSourceLine(sla);
                }
                bugReporter.reportBug(bi);
            }
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 9 with SourceLineAnnotation

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

the class PartiallyConstructedObjectAccess method sawOpcode.

@Override
public void sawOpcode(int seen) {
    try {
        stack.precomputation(this);
        if ((seen == Const.INVOKEVIRTUAL) || (seen == Const.INVOKEINTERFACE) || (seen == Const.INVOKESPECIAL)) {
            int parmCount = SignatureUtils.getNumParameters(getSigConstantOperand());
            if (stack.getStackDepth() > parmCount) {
                OpcodeStack.Item itm = stack.getStackItem(parmCount);
                if (itm.getRegisterNumber() == 0) {
                    JavaClass cls = itm.getJavaClass();
                    if (cls != null) {
                        Method m = findMethod(cls, getNameConstantOperand(), getSigConstantOperand());
                        if ((m != null) && (!m.isFinal())) {
                            if (isCtor && (seen != Const.INVOKESPECIAL)) {
                                bugReporter.reportBug(new BugInstance(this, BugType.PCOA_PARTIALLY_CONSTRUCTED_OBJECT_ACCESS.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, getPC()));
                                throw new StopOpcodeParsingException();
                            } else {
                                if (!Values.CONSTRUCTOR.equals(m.getName())) {
                                    Map<Method, SourceLineAnnotation> calledMethods = methodToCalledMethods.get(getMethod());
                                    calledMethods.put(m, SourceLineAnnotation.fromVisitedInstruction(this));
                                }
                            }
                        }
                    }
                }
            }
        }
    } 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) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 10 with SourceLineAnnotation

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

the class UnrelatedCollectionContents method mergeItem.

/**
 * intersects the set of possible superclass that this collection might have seen before with this one. If we find that there is no commonality between
 * superclasses, report it as a bug.
 *
 * @param supers
 *            the collection of possible superclass/interfaces that has been seen for this collection thus far
 * @param sla
 *            the location of this add
 * @param addItm
 *            the currently added item
 * @throws ClassNotFoundException
 *             if a superclass/interface can not be found
 */
private void mergeItem(final Set<String> supers, final Set<SourceLineAnnotation> sla, final OpcodeStack.Item addItm) throws ClassNotFoundException {
    if (supers.isEmpty()) {
        return;
    }
    Set<String> s = new HashSet<>();
    addNewItem(s, addItm);
    if (s.isEmpty()) {
        return;
    }
    intersection(supers, s);
    if (supers.isEmpty()) {
        BugInstance bug = new BugInstance(this, BugType.UCC_UNRELATED_COLLECTION_CONTENTS.name(), NORMAL_PRIORITY).addClass(this);
        if (addItm.getRegisterNumber() != -1) {
            bug.addMethod(this);
        }
        for (SourceLineAnnotation a : sla) {
            bug.addSourceLine(a);
        }
        bugReporter.reportBug(bug);
    }
}
Also used : SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance) HashSet(java.util.HashSet)

Aggregations

SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)11 BugInstance (edu.umd.cs.findbugs.BugInstance)6 Method (org.apache.bcel.classfile.Method)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JavaClass (org.apache.bcel.classfile.JavaClass)3 QMethod (com.mebigfatguy.fbcontrib.utils.QMethod)2 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)2 FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)2 AbstractMap (java.util.AbstractMap)2 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 FQField (com.mebigfatguy.fbcontrib.utils.FQField)1 ToString (com.mebigfatguy.fbcontrib.utils.ToString)1 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)1 XField (edu.umd.cs.findbugs.ba.XField)1 ArrayDeque (java.util.ArrayDeque)1 Nullable (javax.annotation.Nullable)1 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)1 ConstantPool (org.apache.bcel.classfile.ConstantPool)1