Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20310/src/com/mebigfatguy/fbcontrib/detect
Added Files:
NeedlessInstanceRetrieval.java
Log Message:
intial checkin - NIR detector
--- NEW FILE: NeedlessInstanceRetrieval.java ---
/*
* fb-contrib - Auxilliary detectors for Java programs
* Copyright (C) 2005-2006 Dave Brosius
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.mebigfatguy.fbcontrib.detect;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.Type;
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 NeedlessInstanceRetrieval extends BytecodeScanningDetector implements StatelessDetector {
private static final int SEEN_NOTHING = 0;
private static final int SEEN_INVOKE = 1;
private static final int SEEN_POP = 2;
private BugReporter bugReporter;
private int state;
/**
* constructs a NIR detector given the reporter to report bugs on
* @param bugReporter the sync of bug reports
*/
public NeedlessInstanceRetrieval(BugReporter bugReporter) {
this.bugReporter = bugReporter;
}
/**
* clone this detector so that it can be a StatelessDetector
*
* @return a clone of this object
*/
@Override
public Object clone() throws CloneNotSupportedException {
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 find accesses of static variables off of an instance
* immediately fetched from a method call.
*
* @param seen the opcode of the currently visited instruction
*/
@Override
public void sawOpcode(int seen) {
switch (state)
{
case SEEN_NOTHING:
if ((seen == INVOKEINTERFACE)
|| (seen == INVOKEVIRTUAL)) {
String sig = getSigConstantOperand();
Type retType = Type.getReturnType(sig);
if (retType.getSignature().startsWith("L"))
state = SEEN_INVOKE;
}
break;
case SEEN_INVOKE:
if (seen == POP)
state = SEEN_POP;
else
state = SEEN_NOTHING;
break;
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));
}
state = SEEN_NOTHING;
break;
}
}
}
|