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());
}
}
}
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;
}
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);
}
}
}
}
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);
}
}
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);
}
}
Aggregations