Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2014 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.classfile.JavaClass;
 import  org.apache.bcel.classfile.Method;
 
looks for classes that implement interfaces by relying on methods being implemented in super classes, even though the superclass knows nothing about the interface being implemented by the child.
 
 public class SpoiledChildInterfaceImplementor implements Detector {
 
 	private final BugReporter bugReporter;

constructs a SCII detector given the reporter to report bugs on

Parameters:
bugReporter the sync of bug reports
 
 		this. = bugReporter;
 	}

looks for classes that implement interfaces but don't provide those methods

Parameters:
classContext the context object of the currently parsed class
 
 	public void visitClassContext(ClassContext classContext) {
 		try {
 			JavaClass cls = classContext.getJavaClass();
 
 			if (cls.isAbstract() || cls.isInterface())
 				return;
 
 			if ("java.lang.Object".equals(cls.getSuperclassName()))
 				return;
 
 			JavaClass[] infs = cls.getInterfaces();
 			if (infs.length > 0) {
 				Set<StringclsMethods = buildMethodSet(cls);
 				for (JavaClass inf : cls.getInterfaces()) {
 					Set<StringinfMethods = buildMethodSet(inf);
 					if (infMethods.size() > 0) {
 						infMethods.removeAll(clsMethods);
 						if (infMethods.size() > 0) {
                             JavaClass superCls = cls.getSuperClass();
                             filterSuperInterfaceMethods(infinfMethodssuperCls);
                             if (infMethods.size() > 0) {
 								if (!superCls.implementationOf(inf)) {
 	                                int priority = AnalysisContext.currentAnalysisContext().isApplicationClass(superCls) ?  : ;
 									BugInstance bi = new BugInstance(this"SCII_SPOILED_CHILD_INTERFACE_IMPLEMENTOR"priority)
 											   .addClass(cls)
 											   .addString("Implementing interface: " + inf.getClassName())
 											   .addString("Methods:");
 									for (String nameSig : infMethods)
 										bi.addString("\t" + nameSig);
 
 									return;
 								}
                             }
 						}
 					}
 				}
 			}
 
 		} catch (ClassNotFoundException cnfe) {
 		}
 	}

required for implementing the interface
	public void report() {
	}

builds a set of all non constructor or static initializer method/signatures

Parameters:
cls the class to build the method set from
Returns:
a set of method names/signatures
	private Set<StringbuildMethodSet(JavaClass cls) {
		Set<Stringmethods = new HashSet<String>();
		for (Method m : cls.getMethods()) {
			String methodName = m.getName();
			if (!"<init>".equals(methodName) && !"<clinit>".equals(methodName) && (!"clone".equals(methodName))) {
				methods.add(methodName + ":" + m.getSignature());
			}
		}
		return methods;
	}

removes methods found in an interface when a super interface having the same methods is implemented in a parent. While this is somewhat hinky, we'll allow it.

Parameters:
inf the interface to look for super interfaces for
infMethods the remaining methods that are needed to be found
cls the super class to look for these methods in
	private void filterSuperInterfaceMethods(JavaClass infSet<StringinfMethods, JavaClass cls) {
		try {
			if (infMethods.isEmpty())
				return;
			JavaClass[] superInfs = inf.getInterfaces();
			for (JavaClass superInf : superInfs) {
				if (cls.implementationOf(superInf)) {
					Set<StringsuperInfMethods = buildMethodSet(superInf);
					infMethods.removeAll(superInfMethods);
					if (infMethods.isEmpty())
						return;
				}
				filterSuperInterfaceMethods(superInfinfMethodscls);
			}
catch (ClassNotFoundException cnfe) {
			infMethods.clear();
		}
	}
New to GrepCode? Check out our FAQ X