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
  * 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.Set;
 
 import  org.apache.bcel.Constants;
 import  org.apache.bcel.classfile.Code;
 import  org.apache.bcel.classfile.Field;
 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;

looks for classes that appear to implement the old style type safe enum pattern that was used before java added Enum support to the language. Since this class is compiled with java 1.5 or later, it would be simpler to just use java enums
 
 public class DeprecatedTypesafeEnumPattern extends BytecodeScanningDetector 
 {
 	enum State {SAW_NOTHING, SAW_INVOKESPECIAL, SAW_BUG}
 	
 	private final BugReporter bugReporter;
 	private int firstEnumPC;
 	private int enumCount;
 	private Set<StringenumConstNames;
constructs a DTEP detector given the reporter to report bugs on.

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

implements the visitor to look for classes compiled with 1.5 or better that have all constructors that are private

Parameters:
context the currently parsed class context object
 
 	public void visitClassContext(ClassContext context) {
 		try {
 			JavaClass cls = context.getJavaClass();
 			if (!cls.isEnum()) {
 				if (cls.getMajor() >= Constants.MAJOR_1_5) {
 					Method[] methods = cls.getMethods();
 					for (Method m : methods) {
 						if ("<init>".equals(m.getName())) {
 							if ((m.getAccessFlags() & Constants.ACC_PRIVATE) == 0)
 								return;
 						}
 					}
 					 = 0;
 					 = 0;
 					super.visitClassContext(context);
 				}
 			}
 		} finally {
 			 = null;
 		}
 	}

implements the visitor to look for fields that are public static final and are the same type as the owning class. it collects these object names for later

Parameters:
obj the context object of the currently parsed field
 
 	public void visitField(Field obj) {
 		int accessFlags = obj.getAccessFlags();
 		if (((accessFlags & Constants.ACC_STATIC) != 0)
 		&&  ((accessFlags & Constants.ACC_PUBLIC) != 0)
		&&  ((accessFlags & Constants.ACC_FINAL) != 0)) {
			JavaClass cls = getClassContext().getJavaClass();
			if (!obj.isEnum()) {
				String fieldClass = obj.getSignature();
				if (fieldClass.startsWith("L")) {
					fieldClass = fieldClass.substring(1, fieldClass.length() - 1);
					String clsClass = cls.getClassName();
					if (fieldClass.equals(clsClass)) {
						.add(obj.getName());
						super.visitField(obj);
					}
				}
			}
		}
	}

implements the visitor to look for static initializers to find enum generation

Parameters:
obj the context object of the currently parsed code block
	public void visitCode(Code obj) {
		if ("<clinit>".equals(getMethod().getName())) {
			super.visitCode(obj);
		}
	}

implements the visitor to find allocations of TypesafeEnum constants

Parameters:
seen the currently parsed opcode
	public void sawOpcode(int seen) {
		if ( == .) {
			if (seen == INVOKESPECIAL) {
			}
else if ( == .) {
			if (seen == PUTSTATIC) {
				if (.contains(fieldName)) {
					if ( == 0)
					if ( >= 2) {
						.reportBug(new BugInstance(this"DTEP_DEPRECATED_TYPESAFE_ENUM_PATTERN", NORMAL_PRIORITY)
									.addClass(this)
									.addMethod(this)
									.addSourceLine(this));
					}
				}
			}
			if ( != .)
		}
	}
New to GrepCode? Check out our FAQ X