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 java.util.Map;
 import java.util.Set;
 
 import  org.apache.bcel.classfile.JavaClass;
 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.Detector;
 import  edu.umd.cs.findbugs.ba.ClassContext;
 import  edu.umd.cs.findbugs.visitclass.PreorderVisitor;

looks for methods that have the same signature, except where one uses a Character parameter, and the other uses an int, long, float, double parameter. Since autoboxing is available in 1.5 one might assume that
 test('a')
 
would map to
 public void test(Character c)
 
but instead maps to one that takes an int long, float or double.
 
 public class ConfusingAutoboxedOverloading  extends PreorderVisitor implements Detector
 {
 	private static final int JDK15_MAJOR = 49;
 
 	private static final Set<StringprimitiveSigs = new HashSet<String>();
 	static {
 	}
 	private BugReporter bugReporter;

constructs a CAO detector given the reporter to report bugs on

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

overrides the visitor to look for confusing signatures

Parameters:
classContext the context object that holds the JavaClass currently being parsed
 
 	public void visitClassContext(ClassContext classContext) {
 		JavaClass cls = classContext.getJavaClass();
 		
 		if (cls.isClass() && (cls.getMajor() >= )) {
 				
 			Map<StringSet<String>> methodInfo = new HashMap<StringSet<String>>();
 			populateMethodInfo(clsmethodInfo);
 			
 			Method[] methods = cls.getMethods();
 			for (Method m : methods) {
 				String name = m.getName();
 				String signature = m.getSignature();
 				
 				Set<Stringsigs = methodInfo.get(name);
 				if (sigs != null) {
 					for (String sig : sigs) {
 						if (confusingSignatures(sigsignature)) {
 							.reportBug(new BugInstance(this"CAO_CONFUSING_AUTOBOXED_OVERLOADING", NORMAL_PRIORITY)
 								.addClass(cls.getClassName())
 								.addString(name + signature)
 								.addString(name + sig));
 						}
 					}
 				}
 			}
		}
	}

returns if one signature is a Character and the other is a primitive

Parameters:
sig1 the first method signature
sig2 the second method signature
Returns:
if one signature is a Character and the other a primitive
	private boolean confusingSignatures(String sig1String sig2) {
		if (sig1.equals(sig2))
			return false;
		Type[] type1 = Type.getArgumentTypes(sig1);
		Type[] type2 = Type.getArgumentTypes(sig2);
		if (type1.length != type2.length)
			return false;
		boolean foundParmDiff = false;
		for (int i = 0; i < type1.length; i++) {
			Type t1 = type1[i];
			Type t2 = type2[i];
			if (!t1.getSignature().equals(t2.getSignature())) {
				if ("Ljava/lang/Character;".equals(t1.getSignature())) {
					if (!.contains(t2.getSignature()))
						return false;
else if ("Ljava/lang/Character;".equals(t2.getSignature())) {
					if (!.contains(t1.getSignature()))
						return false;
else 
					return false;
				foundParmDiff = true;
			}
		}
		return foundParmDiff;
	}

fills out a set of method details for possibly confusing method signatures

Parameters:
cls the current class being parsed
methodInfo a collection to hold possibly confusing methods
	private void populateMethodInfo(JavaClass clsMap<StringSet<String>> methodInfo) {
		try {
			if ("java.lang.Object".equals(cls.getClassName()))
				return;
			Method[] methods = cls.getMethods();
			for (Method m : methods) {
				String sig = m.getSignature();
					continue;
				String name = m.getName();				
				Set<Stringsigs = methodInfo.get(name);
				if (sigs == null) {
					sigs = new HashSet<String>();
					methodInfo.put(namesigs);
				}
				sigs.add(m.getSignature());
			}
			populateMethodInfo(cls.getSuperClass(), methodInfo);
catch (ClassNotFoundException cnfe) {
			.reportMissingClass(cnfe);
		}
	}

returns whether a method signature has either a Character or primitive

Parameters:
sig the method signature
Returns:
whether a method signature has either a Character or primitive
	private boolean isPossiblyConfusingSignature(String sig) {
		Type[] types = Type.getArgumentTypes(sig);
		for (Type t : types) {
			sig = t.getSignature();
				return true;
			if ("Ljava/lang/Character;".equals(sig))
				return true;
		}
		return false;
	}

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