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
  * 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  org.apache.bcel.Constants;
 import  org.apache.bcel.Repository;
 import  org.apache.bcel.classfile.Code;
 import  org.apache.bcel.classfile.JavaClass;
 import  org.apache.bcel.classfile.Method;
looks for classes that break the fundamental rule of equivalence, which is symmetry. If a equals b, then b equals a. While it is usually wrong to allow equals to compare different types, at the very least you should make sure that each class knows about each other and is able to compare themselves with each other.
 public class NonSymmetricEquals extends BytecodeScanningDetector {
 	private OpcodeStack stack;
constructs a NSE detector given the reporter to report bugs on

bugReporter the sync of bug reports
     public NonSymmetricEquals(BugReporter bugReporter) {
         this. = bugReporter;
implements the visitor to create the stack object

classContext the context object of the currently parsed class
     public void visitClassContext(ClassContext classContext) {
     	try {
     		 = new OpcodeStack();
     	} finally {
     		 = null;
implements the visitor to see if this method is equals(Object o)

obj the context object of the currently parsed code block
     public void visitCode(Code obj) {
     	Method m = getMethod();
     	String name = m.getName();
     	String signature = m.getSignature();
     	if ("equals".equals(name) && "(Ljava/lang/Object;)Z".equals(signature) && prescreen(m)) {
looks for methods that contain a checkcast instruction

method the context object of the current method
if the class does checkcast instructions
     public boolean prescreen(Method method) {
         BitSet bytecodeSet = getClassContext().getBytecodeSet(method);
         return (bytecodeSet != null) && (bytecodeSet.get(Constants.CHECKCAST));
implements the visitor to look for checkcasts of the parameter to other types, and enter instances in a map for further processing in doReport.

seen the opcode of the currently parsed instruction
    public void sawOpcode(int seen) {
    	try {
    		if (seen == CHECKCAST) {
    			if (.getStackDepth() > 0) {
    				OpcodeStack.Item item = .getStackItem(0);
    				if (item.getRegisterNumber() == 1) {
    					String thisCls = getClassName();
    					String equalsCls = getClassConstantOperand();
    					if (!thisCls.equals(equalsCls)) {
    						JavaClass thisJavaClass = getClassContext().getJavaClass();
    						JavaClass equalsJavaClass = Repository.lookupClass(equalsCls);
    						boolean inheritance = thisJavaClass.instanceOf(equalsJavaClass
    						                    || equalsJavaClass.instanceOf(thisJavaClass);
	    					BugInstance bug = new BugInstance(this"NSE_NON_SYMMETRIC_EQUALS"inheritance ? LOW_PRIORITY:NORMAL_PRIORITY)
	    					Map<StringBugInstancebugs = .get(thisCls);
	    					if (bugs == null) {
	    						bugs = new HashMap<StringBugInstance>();
    	} catch (ClassNotFoundException cnfe) {
    	} finally {
    public void report() {
    	for (Map.Entry<StringMap<StringBugInstance>> thisEntry : .entrySet()) {
    		Map<StringBugInstanceequalsClassesMap = thisEntry.getValue();
    		for (Map.Entry<StringBugInstanceequalsEntry : equalsClassesMap.entrySet()) {
    			String equalsCls = equalsEntry.getKey();
    			Map<StringBugInstancereverseEqualsClassMap = .get(equalsCls);
    			if (reverseEqualsClassMap == null) {
    			if (!reverseEqualsClassMap.containsKey(thisEntry.getKey())) {
New to GrepCode? Check out our FAQ X