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 java.util.Set;
 
 import  org.apache.bcel.classfile.Method;
 import  org.apache.bcel.generic.Type;
 
 
 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 jdk method calls where a parameter expects a constant value, because the api was created before enums. Reports values that are not considered valid values, and may cause problems with use.
 
 public class InvalidConstantArgument extends BytecodeScanningDetector {
 
     private static final Map<PatternParameterInfo<?>> PATTERNS = new HashMap<PatternParameterInfo<?>>();
     static {
         .put(Pattern.compile("javax/swing/JOptionPane#showMessageDialog\\(Ljava/awt/Component;Ljava/lang/Object;Ljava/lang/String;I\\)V"), 
         .put(Pattern.compile("javax/swing/BorderFactory#createBevelBorder\\(I.*\\)Ljavax/swing/border/Border;"), 
                 new ParameterInfo<Integer>(0, true..));
         .put(Pattern.compile("javax/swing/BorderFactory#createEtchedBorder\\(I.*\\)Ljavax/swing/border/Border;"), 
                 new ParameterInfo<Integer>(0, true..));
         .put(Pattern.compile("javax/swing/JScrollBar#\\<init\\>\\(I.*\\)V"), 
                 new ParameterInfo<Integer>(0, true..));
         .put(Pattern.compile("java/lang/Thread#setPriority\\(I\\)V"), 
                 new ParameterInfo<Integer>(0, truenew Range<Integer>(..)));
 
     }
     
     private BugReporter bugReporter;
     private OpcodeStack stack;

    
constructs a ICA detector given the reporter to report bugs on

Parameters:
bugReporter the sync of bug reports
 
     public InvalidConstantArgument(BugReporter bugReporter) {
         this. = bugReporter;
     }
     
     @Override
     public void visitClassContext(ClassContext classContext) {
         try {
              = new OpcodeStack();
             super.visitClassContext(classContext); 
         } finally {
              = null;
         }
     }
     
     @Override
     public void visitMethod(Method obj) {
         .resetForMethodEntry(this);
         super.visitMethod(obj);
     }
     
     public void sawOpcode(int seen) {
         try {
             switch (seen) {
             case INVOKESPECIAL:
             case INVOKESTATIC:
             case INVOKEINTERFACE:
             case INVOKEVIRTUAL:
                 String sig = getSigConstantOperand();
                String mInfo = getClassConstantOperand() + "#" + getNameConstantOperand() + sig;
                for (Map.Entry<PatternParameterInfo<?>> entry : .entrySet()) {
                   Matcher m = entry.getKey().matcher(mInfo);
                   if (m.matches()) {
                       ParameterInfo<?> info = entry.getValue();
                       int parmOffset = info.fromStart ? Type.getArgumentTypes(sig).length - info.parameterOffset - 1: info.parameterOffset;
                       if (.getStackDepth() > parmOffset) {
                           OpcodeStack.Item item = .getStackItem(parmOffset);
                           
                           Comparable cons = (Comparableitem.getConstant();
                           if (!info.isValid(cons)) {
                               int badParm = 1 + (info.fromStart ? info.parameterOffset: Type.getArgumentTypes(sig).length - info.parameterOffset - 1);
                               .reportBug(new BugInstance(this"ICA_INVALID_CONSTANT_ARGUMENT", NORMAL_PRIORITY)
                                                           .addClass(this)
                                                           .addMethod(this)
                                                           .addSourceLine(this)
                                                           .addString("Parameter " + badParm));
                               break;
                           }
                       }
                   }
                }
                break;
            }
        } finally {
            .sawOpcode(thisseen);
        }
    }
    
    static class ParameterInfo<T extends Comparable> {
        private int parameterOffset;
        private boolean fromStart;
        private Set<T> validValues;
        private Range<T> range;
        
        public ParameterInfo(int offsetboolean start, T...values) {
             = offset;
             = start;
             = new HashSet<T>(Arrays.asList(values));
             = null;
        }
        
        public ParameterInfo(int offsetboolean startRange<T> rng) {
             = offset;
             = start;
             = null;
             = rng;
        }
        
        public boolean isValid(Comparable o) {
            if (o == null)
                return true;
            
            if ( != null)
                return .contains(o);
            
            return (o.compareTo(.getFrom()) >= 0) && (o.compareTo(.getTo()) <= 0);
        }
    }
    
    static class Range<T extends Comparable> {
        T from;
        T to;
        
        public Range(T f, T t) {
             = f;
             = t;
        }
        public T getFrom() {
            return ;
        }
        public T getTo() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X