use of edu.umd.cs.findbugs.FieldAnnotation in project wcomponents by BorderTech.
the class CheckWComponentFields method visitField.
/**
* {@inheritDoc}
*/
@Override
public void visitField(final Field obj) {
// All fields inside WComponents must be static or final
if (!obj.isFinal() && !obj.isStatic()) {
FieldAnnotation ann = FieldAnnotation.fromVisitedField(this);
util.getBugReporter().reportBug(new BugInstance(this, "WCF_NON_FINAL_WCOMPONENT_FIELD", HIGH_PRIORITY).addClass(this).addField(ann));
}
// Component models must never be stored as fields
if (util.isComponentModel(util.getClassNameFromSignature(obj.getType().getSignature()))) {
FieldAnnotation ann = FieldAnnotation.fromVisitedField(this);
util.getBugReporter().reportBug(new BugInstance(this, "WCF_COMPONENT_MODEL_FIELD", HIGH_PRIORITY).addClass(this).addField(ann));
}
// UIContexts must never be stored as fields
if (util.isUIContext(util.getClassNameFromSignature(obj.getType().getSignature()))) {
FieldAnnotation ann = FieldAnnotation.fromVisitedField(this);
util.getBugReporter().reportBug(new BugInstance(this, "WCF_UICONTEXT_FIELD", HIGH_PRIORITY).addClass(this).addField(ann));
}
}
use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.
the class DeletingWhileIterating method sawOpcode.
/**
* implements the visitor to look for deletes on collections that are being iterated
*
* @param seen
* the opcode of the currently parsed instruction
*/
@Override
public void sawOpcode(int seen) {
int groupId = -1;
try {
stack.precomputation(this);
if (seen == Const.INVOKEINTERFACE) {
String className = getClassConstantOperand();
String methodName = getNameConstantOperand();
String signature = getSigConstantOperand();
QMethod methodInfo = new QMethod(methodName, signature);
if (isCollection(className)) {
if (collectionMethods.contains(methodInfo) || ITERATOR.equals(methodInfo)) {
if (stack.getStackDepth() > 0) {
OpcodeStack.Item itm = stack.getStackItem(0);
groupId = findCollectionGroup(itm, true);
}
} else if (REMOVE.equals(methodInfo)) {
if (stack.getStackDepth() > 1) {
OpcodeStack.Item itm = stack.getStackItem(1);
int id = findCollectionGroup(itm, true);
if ((id >= 0) && collectionGroups.get(id).isStandardCollection()) {
Integer it = groupToIterator.get(Integer.valueOf(id));
Loop loop = loops.get(it);
if (loop != null) {
int pc = getPC();
if (loop.hasPC(pc)) {
boolean needPop = !Values.SIG_VOID.equals(SignatureUtils.getReturnSignature(signature));
if (!breakFollows(loop, needPop) && !returnFollows(needPop)) {
bugReporter.reportBug(new BugInstance(this, BugType.DWI_DELETING_WHILE_ITERATING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
}
}
}
} else {
Integer numArgs = modifyingMethods.get(methodInfo);
if ((numArgs != null) && (stack.getStackDepth() > numArgs.intValue())) {
OpcodeStack.Item itm = stack.getStackItem(numArgs.intValue());
int id = findCollectionGroup(itm, true);
if (id >= 0) {
Integer it = groupToIterator.get(Integer.valueOf(id));
if (it != null) {
Loop loop = loops.get(it);
if (loop != null) {
int pc = getPC();
if (loop.hasPC(pc)) {
boolean needPop = !Values.SIG_VOID.equals(SignatureUtils.getReturnSignature(signature));
boolean breakFollows = breakFollows(loop, needPop);
boolean returnFollows = !breakFollows && returnFollows(needPop);
if (!breakFollows && !returnFollows) {
bugReporter.reportBug(new BugInstance(this, BugType.DWI_MODIFYING_WHILE_ITERATING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
}
}
}
}
}
} else if ("java/util/Iterator".equals(className) && HASNEXT.equals(methodInfo) && (stack.getStackDepth() > 0)) {
OpcodeStack.Item itm = stack.getStackItem(0);
Integer id = (Integer) itm.getUserValue();
if (id != null) {
groupId = id.intValue();
}
}
} else if ((seen == Const.PUTFIELD) || (seen == Const.PUTSTATIC)) {
if (stack.getStackDepth() > 1) {
OpcodeStack.Item itm = stack.getStackItem(0);
Integer id = (Integer) itm.getUserValue();
if (id == null) {
FieldAnnotation fa = FieldAnnotation.fromFieldDescriptor(new FieldDescriptor(getClassConstantOperand(), getNameConstantOperand(), getSigConstantOperand(), false));
itm = new OpcodeStack.Item(itm.getSignature(), fa, stack.getStackItem(1).getRegisterNumber());
removeFromCollectionGroup(itm);
groupId = findCollectionGroup(itm, true);
}
}
} else if (OpcodeUtils.isAStore(seen)) {
if (stack.getStackDepth() > 0) {
OpcodeStack.Item itm = stack.getStackItem(0);
Integer id = (Integer) itm.getUserValue();
if (id != null) {
int reg = RegisterUtils.getAStoreReg(this, seen);
try {
JavaClass cls = itm.getJavaClass();
if ((cls != null) && cls.implementationOf(iteratorClass)) {
Integer regIt = Integer.valueOf(reg);
Iterator<Integer> curIt = groupToIterator.values().iterator();
while (curIt.hasNext()) {
if (curIt.next().equals(regIt)) {
curIt.remove();
}
}
groupToIterator.put(id, regIt);
}
GroupPair pair = collectionGroups.get(id.intValue());
if (pair != null) {
pair.addMember(Integer.valueOf(reg));
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
} else {
String cls = itm.getSignature();
if ((cls != null) && cls.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
cls = SignatureUtils.trimSignature(cls);
if (isCollection(cls) || "java/util/Iterator".equals(cls)) {
int reg = RegisterUtils.getAStoreReg(this, seen);
removeFromCollectionGroup(new OpcodeStack.Item(itm, reg));
Iterator<Integer> it = groupToIterator.values().iterator();
while (it.hasNext()) {
if (it.next().intValue() == reg) {
it.remove();
break;
}
}
}
}
}
}
} else if (OpcodeUtils.isALoad(seen)) {
int reg = RegisterUtils.getALoadReg(this, seen);
OpcodeStack.Item itm = new OpcodeStack.Item(new OpcodeStack.Item(), reg);
groupId = findCollectionGroup(itm, false);
} else if ((seen == Const.IFEQ) && (stack.getStackDepth() > 0)) {
OpcodeStack.Item itm = stack.getStackItem(0);
Integer id = (Integer) itm.getUserValue();
if (id != null) {
int target = getBranchTarget();
int gotoAddr = target - 3;
int ins = getCode().getCode()[gotoAddr];
if (ins < 0) {
ins = 256 + ins;
}
if ((ins == Const.GOTO) || (ins == Const.GOTO_W)) {
Integer reg = groupToIterator.get(id);
if (reg != null) {
loops.put(reg, new Loop(getPC(), gotoAddr));
}
}
}
}
} finally {
TernaryPatcher.pre(stack, seen);
stack.sawOpcode(this, seen);
TernaryPatcher.post(stack, seen);
if ((groupId >= 0) && (stack.getStackDepth() > 0)) {
OpcodeStack.Item itm = stack.getStackItem(0);
itm.setUserValue(Integer.valueOf(groupId));
}
processEndOfScopes(Integer.valueOf(getPC()));
}
}
use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.
the class DubiousListCollection method reportBugs.
/**
* implements the detector, by reporting all remaining fields that only have set based access
*/
private void reportBugs() {
int major = getClassContext().getJavaClass().getMajor();
for (Map.Entry<String, FieldInfo> entry : fieldsReported.entrySet()) {
String field = entry.getKey();
FieldInfo fi = entry.getValue();
int cnt = fi.getSetCount();
if (cnt > 0) {
FieldAnnotation fa = getFieldAnnotation(field);
if (fa != null) {
// can't use LinkedHashSet in 1.3 so report at LOW
bugReporter.reportBug(new BugInstance(this, BugType.DLC_DUBIOUS_LIST_COLLECTION.name(), (major >= Const.MAJOR_1_4) ? NORMAL_PRIORITY : LOW_PRIORITY).addClass(this).addField(fa).addSourceLine(fi.getSourceLineAnnotation()));
}
}
}
}
use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.
the class DubiousListCollection method getFieldAnnotation.
/**
* builds a field annotation by finding the field in the classes' field list
*
* @param fieldName
* the field for which to built the field annotation
*
* @return the field annotation of the specified field
*/
@Nullable
private FieldAnnotation getFieldAnnotation(final String fieldName) {
JavaClass cls = getClassContext().getJavaClass();
Field[] fields = cls.getFields();
for (Field f : fields) {
if (f.getName().equals(fieldName)) {
return new FieldAnnotation(cls.getClassName(), fieldName, f.getSignature(), f.isStatic());
}
}
// shouldn't happen
return null;
}
use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.
the class DubiousMapCollection method visitClassContext.
@Override
public void visitClassContext(ClassContext classContext) {
if ((mapInterface == null) || (propertiesClass == null)) {
return;
}
try {
stack = new OpcodeStack();
mapFields = new HashMap<>();
super.visitClassContext(classContext);
for (FieldAnnotation mapField : mapFields.values()) {
bugReporter.reportBug(new BugInstance(this, BugType.DMC_DUBIOUS_MAP_COLLECTION.toString(), NORMAL_PRIORITY).addClass(this).addField(mapField));
}
} finally {
mapFields = null;
stack = null;
}
}
Aggregations