Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11646/src/com/mebigfatguy/fbcontrib/detect
Added Files:
NeedlessAutoboxing.java
Log Message:
initial checkin, NAB detector
--- NEW FILE: NeedlessAutoboxing.java ---
package com.mebigfatguy.fbcontrib.detect;
import java.util.HashMap;
import java.util.Map;
import org.apache.bcel.classfile.Method;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;
import edu.umd.cs.findbugs.StatelessDetector;
public class NeedlessAutoboxing extends BytecodeScanningDetector implements StatelessDetector
{
private static final int SEEN_NOTHING = 0;
private static final int SEEN_VALUE = 1;
private static final Map<String, String[]> boxClasses = new HashMap<String, String[]>();
static {
boxClasses.put("java/lang/Boolean", new String[] { "booleanValue()Z", "(Z)V" });
boxClasses.put("java/lang/Character", new String[] { "charValue()C", "(C)V" });
boxClasses.put("java/lang/Byte", new String[] { "byteValue()B", "(B)V" });
boxClasses.put("java.lang/Short", new String[] { "shortValue()S", "(S)V" });
boxClasses.put("java/lang/Integer", new String[] { "intValue()I", "(I)V" });
boxClasses.put("java/lang/Long", new String[] { "longValue()J", "(J)V" });
boxClasses.put("java/lang/Float", new String[] { "floatValue()F", "(F)V" });
boxClasses.put("java/lang/Double", new String[] { "doubleValue()D", "(D)V" });
}
private BugReporter bugReporter;
private int state;
private String boxClass;
public NeedlessAutoboxing(BugReporter bugReporter) {
this.bugReporter = bugReporter;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public void visitMethod(Method obj) {
state = SEEN_NOTHING;
}
public void sawOpcode(int seen) {
switch (state) {
case SEEN_NOTHING:
if (seen == INVOKEVIRTUAL) {
boxClass = getClassConstantOperand();
String[] boxSigs = boxClasses.get(boxClass);
if (boxSigs != null) {
String methodName = getNameConstantOperand();
String methodSig = getSigConstantOperand();
if (boxSigs[0].equals(methodName + methodSig)) {
state = SEEN_VALUE;
}
}
}
break;
case SEEN_VALUE:
if (seen == INVOKESPECIAL) {
if (boxClass.equals(getClassConstantOperand())) {
String methodName = getNameConstantOperand();
String boxSig = boxClasses.get(boxClass)[1];
if ("<init>".equals(methodName)) {
String methodSig = getSigConstantOperand();
if (boxSig.equals(methodSig)) {
bugReporter.reportBug(new BugInstance(this, "NAB_NEEDLESS_AUTOBOXING", NORMAL_PRIORITY)
.addClass(this)
.addMethod(this)
.addSourceLine(this));
}
}
}
}
state = SEEN_NOTHING;
break;
}
}
}
|