Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2012 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.Repository;
 import  org.apache.bcel.classfile.Code;
 import  org.apache.bcel.classfile.ConstantString;
 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.BytecodeScanningDetector;
 import  edu.umd.cs.findbugs.ba.ClassContext;

finds methods of abstract classes that do nothing, or just throw exceptions. Since this is an abstract class, it may be more correct to just leave the method abstract.
 
 public class AbstractClassEmptyMethods extends BytecodeScanningDetector
 {
 	enum State {SAW_NOTHING, SAW_NEW, SAW_DUP, SAW_LDC, SAW_INVOKESPECIAL, SAW_DONE}
 
 	private static JavaClass EXCEPTION_CLASS;
 	static {
 		try {
 			 = Repository.lookupClass("java.lang.Exception");
 		} catch (ClassNotFoundException cnfe) {
 			//ignore
 		}
 	}
 	private final BugReporter bugReporter;
 	private String methodName;
 	private State state;

constructs a ACEM detector given the reporter to report bugs on

Parameters:
bugReporter the sync of bug reports
 
 	public AbstractClassEmptyMethods(BugReporter bugReporter) {
 		this. = bugReporter;
 	}

overrides the visitor to check for abstract classes.

Parameters:
classContext the context object that holds the JavaClass being parsed
 
 	public void visitClassContext(ClassContext classContext) {
 		JavaClass cls = classContext.getJavaClass();
 		if (cls.isAbstract())
 			super.visitClassContext(classContext);
 	}

overrides the visitor to grab the method name and reset the state.

Parameters:
obj the method being parsed
 
 	public void visitMethod(Method obj) {
 		 = obj.getName();
 	}

overrides the visitor to filter out constructors.

Parameters:
obj the code to parse
 
 	public void visitCode(Code obj) {
 		if ("<init>".equals() || "<clinit>".equals())
 			return;
 		
 		super.visitCode(obj);
 	}

overrides the visitor to look for empty methods or simple exception throwers.

Parameters:
seen the opcode currently being parsed
	public void sawOpcode(int seen) {
		try {
			switch () {
					if (seen == RETURN) {
						.reportBug(new BugInstance(this"ACEM_ABSTRACT_CLASS_EMPTY_METHODS", NORMAL_PRIORITY)
							.addClass(this)
							.addMethod(this)
							.addSourceLine(this));
else if (seen == NEW) {
						JavaClass exCls = Repository.lookupClass(newClass);
						if (( != null) && exCls.instanceOf())
						else
else
				break;
				case :
					if (seen == DUP)
					else
				break;
				case :
					if (((seen == LDC) || (seen == LDC_W)) && (getConstantRefOperand() instanceof ConstantString))
					else
				break;
				case :
					if ((seen == INVOKESPECIAL) && "<init>".equals(getNameConstantOperand()))
					else
				break;
					if (seen == ATHROW) {
						.reportBug(new BugInstance("ACEM_ABSTRACT_CLASS_EMPTY_METHODS", NORMAL_PRIORITY)
							.addClass(this)
							.addMethod(this)
							.addSourceLine(this));
					}
				break;
				case :
				break;
			}
catch (ClassNotFoundException cnfe) {
			.reportMissingClass(cnfe);
		}
	}
New to GrepCode? Check out our FAQ X