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  org.apache.bcel.Constants;
 import  org.apache.bcel.classfile.Code;
 import  org.apache.bcel.classfile.LocalVariable;
 import  org.apache.bcel.classfile.LocalVariableTable;
 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.FieldAnnotation;

looks for manual casts of objects that are more specific then needed as the value is assigned to a class or interface higher up in the inheritance chain. You only need to cast to that class or interface.
 
 public class OverzealousCasting
     extends BytecodeScanningDetector
 {
 	enum State {SAW_NOTHING, SAW_NEXT, SAW_CHECKCAST}
     
     BugReporter bugReporter;
     State state;
     LocalVariableTable lvt;
     String castClass;
    
    
constructs a OC detector given the reporter to report bugs on

Parameters:
bugReporter the sync of bug reports
 
     public OverzealousCasting(BugReporter bugReporter) {
         this. = bugReporter;
     }
    
    
implements the visitor to set the state on entry of the code block to SAW_NOTHING, and to see if there is a local variable table

Parameters:
obj the context object of the currently parsed code block
 
     @Override
     public void visitCode(Code obj) {
          = .;
          = obj.getLocalVariableTable();
         if ( != null && prescreen(getMethod()))
             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 a checkcast followed by a astore, where the types of the objects are different.

Parameters:
seen the opcode of the currently parsed instruction
 
     @Override
     public void sawOpcode(int seen) {
         switch () {
             case :
                 if (seen == CHECKCAST) {
                      = getClassConstantOperand();
                      = .;
                 } else if (seen == INVOKEINTERFACE) {
                     //enhanced for loops add an incorrect checkcast instruction, so ignore checkcasts after iterator.next()
                     String clsName = getClassConstantOperand();
                     String methodName = getNameConstantOperand();
                     if ("java/util/Iterator".equals(clsName) && "next".equals(methodName)) {
                         = .;
                    }
                }
            break;
                        
            case :
                if ((seen == ASTORE) || ((seen >= ASTORE_0) && (seen <= ASTORE_3))) {
                    int reg = RegisterUtils.getAStoreReg(thisseen);
                    LocalVariable lv = .getLocalVariable(reggetNextPC());
                    if (lv != null) {
                        String sig = lv.getSignature();
                        if (sig.charAt(0) == 'L') {
                            sig = sig.substring(1, sig.length() - 1);
                        }
                        if (!sig.equals()) {
                            .reportBug(new BugInstance(this"OC_OVERZEALOUS_CASTING", NORMAL_PRIORITY)
                                       .addClass(this)
                                       .addMethod(this)
                                       .addSourceLine(this));
                        }
                    }
                } else if (seen == PUTFIELD) {
                    FieldAnnotation f = FieldAnnotation.fromReferencedField(this);
                    String sig = f.getFieldSignature();
                    if (sig.charAt(0) == 'L') {
                        sig = sig.substring(1, sig.length() - 1);
                    }
                    if (!sig.equals()) {
                        .reportBug(new BugInstance(this"OC_OVERZEALOUS_CASTING", NORMAL_PRIORITY)
                                   .addClass(this)
                                   .addMethod(this)
                                   .addSourceLine(this));
                    }
                }
                 = .;
            break;
            
            case :
            default:
                 = .;
            break;
        }
    }
New to GrepCode? Check out our FAQ X