use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.
the class FieldCouldBeLocal method visitClassContext.
/**
* overrides the visitor to collect localizable fields, and then report those that survive all method checks.
*
* @param classContext
* the context object that holds the JavaClass parsed
*/
@Override
public void visitClassContext(ClassContext classContext) {
try {
localizableFields = new HashMap<>();
visitedBlocks = new BitSet();
clsContext = classContext;
clsName = clsContext.getJavaClass().getClassName();
clsSig = SignatureUtils.classToSignature(clsName);
JavaClass cls = classContext.getJavaClass();
Field[] fields = cls.getFields();
ConstantPool cp = classContext.getConstantPoolGen().getConstantPool();
for (Field f : fields) {
if (!f.isStatic() && !f.isVolatile() && (f.getName().indexOf(Values.SYNTHETIC_MEMBER_CHAR) < 0) && f.isPrivate()) {
FieldAnnotation fa = new FieldAnnotation(cls.getClassName(), f.getName(), f.getSignature(), false);
boolean hasExternalAnnotation = false;
for (AnnotationEntry entry : f.getAnnotationEntries()) {
ConstantUtf8 cutf = (ConstantUtf8) cp.getConstant(entry.getTypeIndex());
if (!cutf.getBytes().startsWith(Values.JAVA)) {
hasExternalAnnotation = true;
break;
}
}
localizableFields.put(f.getName(), new FieldInfo(fa, hasExternalAnnotation));
}
}
if (!localizableFields.isEmpty()) {
buildMethodFieldModifiers(classContext);
super.visitClassContext(classContext);
for (FieldInfo fi : localizableFields.values()) {
FieldAnnotation fa = fi.getFieldAnnotation();
SourceLineAnnotation sla = fi.getSrcLineAnnotation();
BugInstance bug = new BugInstance(this, BugType.FCBL_FIELD_COULD_BE_LOCAL.name(), NORMAL_PRIORITY).addClass(this).addField(fa);
if (sla != null) {
bug.addSourceLine(sla);
}
bugReporter.reportBug(bug);
}
}
} finally {
localizableFields = null;
visitedBlocks = null;
clsContext = null;
methodFieldModifiers = null;
}
}
use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.
the class NonRecycleableTaglibs method sawOpcode.
/**
* implements the visitor to record storing of fields, and where they occur
*
* @param seen
* the currently parsed opcode
*/
@Override
public void sawOpcode(int seen) {
if (seen == PUTFIELD) {
QMethod methodInfo = new QMethod(getMethodName(), getMethodSig());
Map<Map.Entry<String, String>, SourceLineAnnotation> fields = methodWrites.get(methodInfo);
if (fields == null) {
fields = new HashMap<>();
methodWrites.put(methodInfo, fields);
}
String fieldName = getNameConstantOperand();
String fieldSig = getSigConstantOperand();
FieldAnnotation fa = new FieldAnnotation(getDottedClassName(), fieldName, fieldSig, false);
fieldAnnotations.put(fieldName, fa);
fields.put(new AbstractMap.SimpleImmutableEntry(fieldName, fieldSig), SourceLineAnnotation.fromVisitedInstruction(this));
}
}
use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.
the class OverzealousCasting method sawOpcode.
/**
* implements the visitor to look for a checkcast followed by a astore, where the types of the objects are different.
*
* @param seen
* the opcode of the currently parsed instruction
*/
@Override
public void sawOpcode(int seen) {
switch(state) {
case SAW_NOTHING:
if (seen == Const.CHECKCAST) {
castClass = getClassConstantOperand();
state = State.SAW_CHECKCAST;
} else if (seen == Const.INVOKEINTERFACE) {
// enhanced for loops add an incorrect checkcast instruction, so
// ignore checkcasts after iterator.next()
String clsName = getClassConstantOperand();
String methodName = getNameConstantOperand();
if ("java/util/Iterator".equals(clsName) && "next".equals(methodName)) {
state = State.SAW_NEXT;
}
}
break;
case SAW_CHECKCAST:
if (OpcodeUtils.isAStore(seen)) {
int reg = RegisterUtils.getAStoreReg(this, seen);
LocalVariable lv = lvt.getLocalVariable(reg, getNextPC());
if (lv != null) {
String sig = lv.getSignature();
if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
sig = SignatureUtils.trimSignature(sig);
}
if (!sig.equals(castClass)) {
bugReporter.reportBug(new BugInstance(this, BugType.OC_OVERZEALOUS_CASTING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
} else if (seen == Const.PUTFIELD) {
FieldAnnotation f = FieldAnnotation.fromReferencedField(this);
String sig = f.getFieldSignature();
if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
sig = SignatureUtils.trimSignature(sig);
}
// if the signature is Object, the field might be genericized, so ignore
if (!sig.equals(Values.SLASHED_JAVA_LANG_OBJECT) && !sig.equals(castClass)) {
bugReporter.reportBug(new BugInstance(this, BugType.OC_OVERZEALOUS_CASTING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
state = State.SAW_NOTHING;
break;
case SAW_NEXT:
default:
state = State.SAW_NOTHING;
break;
}
}
use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.
the class NeedlessMemberCollectionSynchronization method visitField.
/**
* implements the visitor to find collection fields
*
* @param obj
* the context object of the currently parse field
*/
@Override
public void visitField(Field obj) {
if (obj.isPrivate()) {
String signature = obj.getSignature();
if (signature.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
try {
JavaClass cls = Repository.lookupClass(SignatureUtils.stripSignature(signature));
if (cls.implementationOf(collectionClass) || cls.implementationOf(mapClass)) {
FieldAnnotation fa = FieldAnnotation.fromVisitedField(this);
collectionFields.put(fa.getFieldName(), new FieldInfo(fa));
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
}
}
}
use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.
the class Section508Compliance method visitField.
/**
* looks for fields that are JLabels and stores them in a set
*
* @param obj
* the field object of the current field
*/
@Override
public void visitField(Field obj) {
String fieldSig = obj.getSignature();
if ("Ljavax/swing/JLabel;".equals(fieldSig)) {
FieldAnnotation fa = FieldAnnotation.fromVisitedField(this);
fieldLabels.add(XFactory.createXField(fa));
}
}
Aggregations