Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3152/src/com/mebigfatguy/fbcontrib/detect
Added Files:
CopiedOverriddenMethod.java
Log Message:
initial check in - new COM detector
--- NEW FILE: CopiedOverriddenMethod.java ---
/*
* fb-contrib - Auxilliary detectors for Java programs
* Copyright (C) 2005 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 java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.Detector;
import edu.umd.cs.findbugs.SourceLineAnnotation;
import edu.umd.cs.findbugs.StatelessDetector;
import edu.umd.cs.findbugs.ba.ClassContext;
import edu.umd.cs.findbugs.visitclass.DismantleBytecode;
public class CopiedOverriddenMethod extends DismantleBytecode implements Detector, StatelessDetector
{
private BugReporter bugReporter;
private Map<String, Code> superclassCode = new HashMap<String, Code>();
private ClassContext classContext;
private String curMethodInfo;
/**
* constructs a COM detector given the reporter to report bugs on
* @param bugReporter the sync of bug reports
*/
public CopiedOverriddenMethod(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 visitor to accept classes derived from non java.lang.Object classes.
*
* @param classContext the context object of the currently parsed class
*/
public void visitClassContext(ClassContext classContext) {
try {
JavaClass cls = classContext.getJavaClass();
String superName = cls.getSuperclassName();
if (!"java.lang.Object".equals(superName)) {
this.classContext = classContext;
superclassCode.clear();
JavaClass superCls = cls.getSuperClass();
Method[] methods = superCls.getMethods();
for (Method m : methods) {
String methodName = m.getName();
if ((m.isPublic() || m.isProtected())
&& (!"<init>".equals(methodName) && !"<clinit>".equals(methodName))) {
String methodInfo = methodName + ":" + m.getSignature();
superclassCode.put(methodInfo, m.getCode());
}
}
cls.accept(this);
superclassCode.clear();
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
}
/**
* overrides the visitor to get the methodInfo
*
* @param obj the method object for the currently parsed method
*/
public void visitMethod(Method obj) {
curMethodInfo = obj.getName() + ":" + obj.getSignature();
}
/**
* overrides the visitor to find code blocks of methods that are the same as its parents
*
* @param obj the code object of the currently parsed method
*/
public void visitCode(Code obj) {
Method m = getMethod();
if (!m.isPublic() && !m.isProtected())
return;
Code superCode = superclassCode.get(curMethodInfo);
if (superCode != null) {
if (Arrays.equals(obj.getCode(), superCode.getCode())) {
bugReporter.reportBug(new BugInstance(this, "COM_COPIED_OVERRIDDEN_METHOD", NORMAL_PRIORITY)
.addClass(this)
.addMethod(this)
.addSourceLine(classContext, this, getPC()));
}
}
}
/**
* implements the detector with an empty implementation
*/
public void report() {
}
}
|