[Fb-contrib-commit] fb-contrib/src/com/mebigfatguy/fbcontrib/detect NeedlessInstanceRetrieval.java,1
Brought to you by:
dbrosius
|
From: Dave B. <dbr...@us...> - 2006-02-05 02:12:05
|
Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30591/src/com/mebigfatguy/fbcontrib/detect Modified Files: NeedlessInstanceRetrieval.java Log Message: use the linenumbertable to make sure that the load of the constant is at the same line as the method call, otherwise many false positives occur with invokes before for loops. Index: NeedlessInstanceRetrieval.java =================================================================== RCS file: /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessInstanceRetrieval.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- NeedlessInstanceRetrieval.java 5 Feb 2006 00:56:16 -0000 1.2 +++ NeedlessInstanceRetrieval.java 5 Feb 2006 02:11:54 -0000 1.3 @@ -18,7 +18,9 @@ */ package com.mebigfatguy.fbcontrib.detect; -import org.apache.bcel.classfile.Method; +import org.apache.bcel.classfile.Code; +import org.apache.bcel.classfile.LineNumber; +import org.apache.bcel.classfile.LineNumberTable; import org.apache.bcel.generic.Type; import edu.umd.cs.findbugs.BugInstance; @@ -32,7 +34,9 @@ private static final int SEEN_POP = 2; private BugReporter bugReporter; + private LineNumberTable lnTable; private int state; + private int invokePC; /** * constructs a NIR detector given the reporter to report bugs on * @param bugReporter the sync of bug reports @@ -51,16 +55,20 @@ return super.clone(); } - /** - * overrides the interface to reset the state - * - * @param obj the context objeft of the currently parsed method - */ - @Override - public void visitMethod(Method obj) { - state = SEEN_NOTHING; - } - + /** + * overrides the interface to collect the line number table, and reset state + * + * @param obj the content object of the currently parsed code + */ + @Override + public void visitCode(Code obj) { + lnTable = obj.getLineNumberTable(); + if (lnTable != null) { + state = SEEN_NOTHING; + super.visitCode(obj); + } + } + /** * overrides the interface to find accesses of static variables off of an instance * immediately fetched from a method call. @@ -76,8 +84,10 @@ || (seen == INVOKEVIRTUAL)) { String sig = getSigConstantOperand(); Type retType = Type.getReturnType(sig); - if (retType.getSignature().startsWith("L")) + if (retType.getSignature().startsWith("L")) { + invokePC = getPC(); state = SEEN_INVOKE; + } } break; @@ -90,17 +100,19 @@ case SEEN_POP: if ((seen >= ACONST_NULL) && (seen <= DCONST_1)) { - bugReporter.reportBug(new BugInstance(this, "NIR_NEEDLESS_INSTANCE_RETRIEVAL", NORMAL_PRIORITY) - .addClass(this) - .addMethod(this) - .addSourceLine(this)); + if (lnTable.getSourceLine(invokePC) == lnTable.getSourceLine(getPC())) { + bugReporter.reportBug(new BugInstance(this, "NIR_NEEDLESS_INSTANCE_RETRIEVAL", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLine(this)); + } } state = SEEN_NOTHING; break; - - default: - state = SEEN_NOTHING; - break; + + default: + state = SEEN_NOTHING; + break; } } } |