Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2013 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
  * 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.Set;
 import  org.apache.bcel.Repository;
 import  org.apache.bcel.classfile.ExceptionTable;
 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.StringAnnotation;
 import  edu.umd.cs.findbugs.visitclass.PreorderVisitor;

looks for methods that declare Runtime exceptions in their throws clause. While doing so is not illegal, it may represent a misunderstanding as to the exception in question. If a RuntimeException is declared, it implies that this exception type is expected to happen, which if true, should be handled in code, and not propogated.
 public class DeclaredRuntimeException extends PreorderVisitor implements Detector
 	private final BugReporter bugReporter;
 	private static final Set<StringruntimeExceptions = new HashSet<String>();
 	private static JavaClass runtimeExceptionClass;
 	static {
 		try {
 			 = Repository.lookupClass("java.lang.RuntimeException");
 		} catch (ClassNotFoundException cnfe) {

constructs a DRE detector given the reporter to report bugs on

bugReporter the sync of bug reports
 	public DeclaredRuntimeException(final BugReporter bugReporter) {
 		this. = bugReporter;

overrides the visitor and accepts if the Exception class was loaded

classContext the context object for the currently parsed class
 	public void visitClassContext(final ClassContext classContext) {
 		if ( != null)

overrides the visitor to find declared runtime exceptions

obj the method object of the currently parsed method
 	public void visitMethod(final Method obj) {
 		ExceptionTable et = obj.getExceptionTable();
 		if (et != null) {
 			String[] exNames = et.getExceptionNames();
 			Set<StringmethodRTExceptions = new HashSet<String>(6);
 			int priority = LOW_PRIORITY;
 			boolean foundRuntime = false;
 			for (String ex : exNames) {
 				boolean isRuntime = false;
 					isRuntime = true;	
 				else {
 					try {
 						JavaClass exClass = Repository.lookupClass(ex);
 						if (exClass.instanceOf()) {
 							if (ex.startsWith("java.lang."))
 								priority = NORMAL_PRIORITY;
 							isRuntime = true;
catch (ClassNotFoundException cnfe) {
				if (isRuntime) {
					foundRuntime = true;
			if (foundRuntime) {	
				BugInstance bug = new BugInstance(this"DRE_DECLARED_RUNTIME_EXCEPTION"priority)
				for (String ex : methodRTExceptions) {
					bug.add(new StringAnnotation(ex));

implementation of the detector, with null implementation
	public void report() {
New to GrepCode? Check out our FAQ X