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.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;
 
 import  edu.umd.cs.findbugs.BugInstance;
 import  edu.umd.cs.findbugs.BugReporter;
 import  edu.umd.cs.findbugs.BytecodeScanningDetector;
 import  edu.umd.cs.findbugs.OpcodeStack;
 import  edu.umd.cs.findbugs.ba.ClassContext;

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 BugReporter bugReporter;
 	private OpcodeStack stack;
 	private Map<StringMap<String, BugInstance>> possibleBugs = new HashMap<StringMap<String, BugInstance>>();

constructs a NSE detector given the reporter to report bugs on

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

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

Parameters:
obj the context object of the currently parsed code block
 
     @Override
     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)) {
     		.resetForMethodEntry(this);
     		super.visitCode(obj);
     	}
     }
    
    
looks for methods that contain a checkcast instruction

Parameters:
method the context object of the current method
Returns:
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.

Parameters:
seen the opcode of the currently parsed instruction
    @Override
    public void sawOpcode(int seen) {
    	try {
            .precomputation(this);
            
    		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)
	    					            .addClass(this)
	    					            .addMethod(this)
	    					            .addSourceLine(this)
	    					            .addString(equalsCls);
	    					Map<String, BugInstance> bugs = .get(thisCls);
	    					if (bugs == null) {
	    						bugs = new HashMap<String, BugInstance>();
	    						.put(thisClsbugs);
	    					}
	    					bugs.put(equalsClsbug);
    					}
    				}
    			}
    		}
    	} catch (ClassNotFoundException cnfe) {
    		.reportMissingClass(cnfe);
    	} finally {
    		.sawOpcode(thisseen);
    	}
    }
    
    @Override
    public void report() {
    	for (Map.Entry<StringMap<String, BugInstance>> thisEntry : .entrySet()) {
    		Map<String, BugInstance> equalsClassesMap = thisEntry.getValue();
    		for (Map.Entry<String, BugInstance> equalsEntry : equalsClassesMap.entrySet()) {
    			String equalsCls = equalsEntry.getKey();
    			
    			Map<String, BugInstance> reverseEqualsClassMap = .get(equalsCls);
    			if (reverseEqualsClassMap == null) {
    				.reportBug(equalsClassesMap.values().iterator().next());
    				break;
    			}
    			
    			if (!reverseEqualsClassMap.containsKey(thisEntry.getKey())) {
    				.reportBug(equalsClassesMap.values().iterator().next());
    				break;
    			}
    		}
    	}
    	.clear();
    }
New to GrepCode? Check out our FAQ X