Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2014 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
  * 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.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  org.apache.bcel.generic.Type;
looks for private or static methods that have parameters that aren't used. These parameters can be removed.
 public class UnusedParameter extends BytecodeScanningDetector {
     private static Set<StringIGNORE_METHODS = new HashSet<String>();
     static {
     private BugReporter bugReporter;
     private BitSet unusedParms;
     private Map<IntegerIntegerregToParm;
     private OpcodeStack stack;
constructs a UP detector given the reporter to report bugs on

bugReporter the sync of bug reports
     public UnusedParameter(BugReporter bugReporter) {
         this. = bugReporter;
implements the visitor to create parm bitset

classContext the context object of the currently parsed class
     public void visitClassContext(ClassContext classContext) {
         try {
              = new BitSet();
              = new HashMap<IntegerInteger>();
              = new OpcodeStack();
         } finally {
              = null;
              = null;
implements the visitor to clear the parm set, and check for potential methods

obj the context object of the currently parsed code block
     public void visitCode(Code obj) {
        Method m = getMethod();
        String methodName = m.getName();
        if (.contains(methodName))
        int accessFlags = m.getAccessFlags();
        if (((accessFlags & (Constants.ACC_STATIC|Constants.ACC_PRIVATE)) != 0) && ((accessFlags & Constants.ACC_SYNTHETIC) == 0)) {
            Type[] parmTypes = Type.getArgumentTypes(m.getSignature());
            if (parmTypes.length > 0) {
                int firstReg = 0;
                if ((accessFlags & Constants.ACC_STATIC) == 0)
                int reg = firstReg;
                for (int i = 0; i < parmTypes.length; ++i) {
                    .put(reg, Integer.valueOf(i+1));
                    String parmSig = parmTypes[i].getSignature();
                    reg += ("J".equals(parmSig) || "D".equals(parmSig)) ? 2 : 1;
                if (!.isEmpty()) {
                    LocalVariableTable lvt = m.getLocalVariableTable();
                    reg = .nextSetBit(firstReg);
                    while (reg >= 0) {
                        LocalVariable lv = (lvt != null) ? lvt.getLocalVariable(reg, 0) : null;
                        if (lv != null) {
                            String parmName = lv.getName();
                            .reportBug(new BugInstance(this"UP_UNUSED_PARAMETER", NORMAL_PRIORITY)
                                            .addString("Parameter " + .get(reg) + ": " + parmName));
                            reg = .nextSetBit(reg+1);
implements the visitor to look for usage of parmeter registers.

seen the opcode of the currently parsed instruction
    public void sawOpcode(int seen) {
        if (.isEmpty()) {
        try {
            switch (seen) {
                case ASTORE:
                case ASTORE_0:
                case ASTORE_1:
                case ASTORE_2:
                case ASTORE_3:
                case ISTORE:
                case ISTORE_0:
                case ISTORE_1:
                case ISTORE_2:
                case ISTORE_3:
                case LSTORE:
                case LSTORE_1:
                case LSTORE_2: 
                case LSTORE_3: 
                case FSTORE:
                case FSTORE_0:
                case FSTORE_1:
                case FSTORE_2:
                case FSTORE_3:
                case DSTORE:
                case DSTORE_1:
                case DSTORE_2:
                case DSTORE_3:
                case ALOAD:
                case ALOAD_0:
                case ALOAD_1:
                case ALOAD_2:
                case ALOAD_3: 
                case ILOAD:
                case ILOAD_0:
                case ILOAD_1:
                case ILOAD_2:
                case ILOAD_3:
                case LLOAD:
                case LLOAD_0:
                case LLOAD_1:
                case LLOAD_2:
                case LLOAD_3:
                case FLOAD:
                case FLOAD_0:
                case FLOAD_1:
                case FLOAD_2:
                case FLOAD_3:
                case DLOAD:
                case DLOAD_0:
                case DLOAD_1:
                case DLOAD_2:
                case DLOAD_3: {
                    int reg = getRegisterOperand();
                case ARETURN:
                case IRETURN:
                case LRETURN:
                case FRETURN:
                case DRETURN:{
                    if (.getStackDepth() > 0) {
                        OpcodeStack.Item item = .getStackItem(0);
                        int reg = item.getRegisterNumber();
                        if (reg >= 0)
        } finally {
New to GrepCode? Check out our FAQ X