Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * fb-contrib - Auxiliary detectors for Java programs
  * Copyright (C) 2005-2015 Bhaskar Maddala
  * Copyright (C) 2005-2015 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;
In a JVM, Two classes are the same class (and consequently the same type) if they are loaded by the same class loader, and they have the same fully qualified name [JVMSpec 1999]. Two classes with the same name but different package names are distinct, as are two classes with the same fully qualified name loaded by different class loaders. Find usage involving comparison of class names, rather than the class itself.
public class CompareClassNameEquals extends OpcodeStackDetector {
    private boolean flag = false;
    private final BugReporter bugReporter;
    public CompareClassNameEquals(final BugReporter bugReporter) {
        this. = bugReporter;
    }
    @Override
    public boolean shouldVisitCode(Code obj) {
         = false;
        return lvt != null;
    }
    @Override
    public void afterOpcode(int seen) {
        super.afterOpcode(seen);
        if ( == true) {
            .getStackItem(0).setUserValue(.);
             = false;
        }
    }
    @Override
    public void sawOpcode(int seen) {
    	if (seen == ) {
            if ("getName".equals(getNameConstantOperand())
                    && "()Ljava/lang/String;".equals(getSigConstantOperand())
                    && "java/lang/Class".equals(getClassConstantOperand())) {
                 = true;
            } else if ("equals".equals(getNameConstantOperand())
                    && "(Ljava/lang/Object;)Z".equals(getSigConstantOperand())
                    && "java/lang/String".equals(getClassConstantOperand())) {
                Item item = .getItemMethodInvokedOn(this);
                Object srcValue = item.getUserValue();
                item = .getStackItem(0);
                Object dstValue = item.getUserValue();
                if (..equals(srcValue) && ..equals(dstValue)) {
                    .reportBug(new BugInstance(this..name(),)
                    .addClass(this)
                    .addMethod(this)
                    .addSourceLine(this));
                }
            }
        }
//		stack.sawOpcode(this, seen);
    }
New to GrepCode? Check out our FAQ X