Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * fb-contrib - Auxiliary detectors for Java programs
   * 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;
 
 import java.util.Map;
 
 
 
looks for non derivable method that declare parameters and then cast those parameters to more specific types in the method. This is misleading and dangerous as you are not documenting through parameter types what is necessary for these parameters to function correctly.
 
 {
 	enum State {SAW_NOTHING, SAW_LOAD, SAW_CHECKCAST}
     
     Map<IntegerStringparmSigs;
     Map<IntegerBugInfobugs;
     int loadedReg;
     String parmSig;
     State state;
     String castClass;
     int downwardBranchTarget;
    
    
constructs a PDP detector given the reporter to report bugs on

Parameters:
bugReporter the sync of bug reports
 
     public PoorlyDefinedParameter(BugReporter bugReporter) {
         this. = bugReporter;
     }
    
    
implements the visitor to see if the method has parameters

Parameters:
obj the context object of the currently parsed code block
 
     @Override
     public void visitCode(Code obj) {
         try {
             Method m = getMethod();
             if (m.isStatic() || m.isPrivate() || ..equals(m.getName())) {
                  = SignatureUtils.getParameterSignatures(m);
                 if ((.size() > 0) && prescreen(m)) {
                      = .;
                      = new HashMap<IntegerBugInfo>();
                      = -1;
                     super.visitCode(obj);
                     for (BugInfo bi : .values()) {
                         .reportBug(bi.bug);
                     }
                 }
             }
         } finally {
              = null;
         }
     }
    
    
    
looks for methods that contain a checkcast instruction

Parameters:
method the context object of the current method
Returns:
if the class does checkcast instructions
    private boolean prescreen(Method method) {
        BitSet bytecodeSet = getClassContext().getBytecodeSet(method);
        return (bytecodeSet != null) && (bytecodeSet.get(.));
    }
    
    
implements the visitor to look for check casts of parameters to more specific types
    @Override
    public void sawOpcode(int seen)
    {
    	if ( == -1) {
	        switch () {
	            case :
	                if (OpcodeUtils.isALoad(seen)) {
	                     = RegisterUtils.getALoadReg(thisseen);
	                     = .get(Integer.valueOf());
	                    if ( != null) {
	                         = .substring(1, .length() - 1);
	                         = .;
	                    }
	                }
	            break;
	            
	            case :
	                if (seen == ) {
	                     = getClassConstantOperand();
	                    if (!.equals()) {
	                         = .;
	                        return;
	                    }
	                } else if (seen == ) {
	                    // probably an if guard... assume the code is reasonable
	                    .remove(Integer.valueOf());
	                }
	                 = .;
	            break;
	            
	            case :
	                if ((seen == ) || (seen == ) || ((seen >= ) && seen <= )) {
	                    String parmName = null;
	                    LocalVariableTable lvt = getMethod().getLocalVariableTable();
	                    if (lvt != null) {
	                        LocalVariable lv = lvt.getLocalVariable(, 1);
	                        if (lv != null) {
	                            parmName = lv.getName();
	                        }
	                    }
	                    if (parmName == null) {
	                        parmName = "(" +  + ")";
	                    }
	                    
	                    BugInstance bug = new BugInstance(this..name(), )
	                               .addClass(this)
	                               .addMethod(this)
	                               .addSourceLine(this)
	                               .addString(parmName);
	                    Integer lr = Integer.valueOf();
	                    BugInfo bi = .get(lr);
	                    if (bi == null) {
	                        .put(lrnew BugInfo(bug));
	                    } else {
	                        //If there are casts to multiple different types, don't report it altho suspect
	                        if (!bi.castClass.equals()) {
	                            .remove(lr);
	                        }
	                    }
	                    
	                }
	                    
	                 = .;
	            break;
	        }
	        
	        {
	        	int insTarget = -1;
		        if (((seen >= ) && (seen <= )) || (seen == ) || (seen == )) {
		        	insTarget = getBranchTarget();
		        	if (insTarget < getPC())
		        		insTarget = -1;
		        } else if ((seen == ) || (seen == )) {
		        	insTarget = this.getDefaultSwitchOffset() + getPC();
		        } 
		        
		        if (insTarget > ) {
		        	 = insTarget;
		        }
	        }
	    } else {
    		 = .;
    		if (getPC() >= )
    			 = -1;
    	}
    }
    
    private static class BugInfo {
        String castClass;
        BugInstance bug;
        
        BugInfo(String castBugInstance bi) {
             = cast;
             = bi;
        }
    }
New to GrepCode? Check out our FAQ X