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
  * 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.Set;
 import  org.apache.bcel.classfile.Code;
 import  edu.umd.cs.findbugs.BugInstance;
 import  edu.umd.cs.findbugs.BugReporter;
 import  edu.umd.cs.findbugs.BytecodeScanningDetector;
 import  edu.umd.cs.findbugs.OpcodeStack;

looks for common methods that are believed to be non mutating, where the value is discarded. Since the method makes no changes to the object, calling this method is useless. The method call can be removed.
 public class NonProductiveMethodCall extends BytecodeScanningDetector {
     private static final Set<PatternIMMUTABLE_METHODS = new HashSet<Pattern>();
     static {
     private BugReporter bugReporter;
     private OpcodeStack stack;

constructs a NPMC detector given the reporter to report bugs on

bugReporter the sync of bug reports
     public NonProductiveMethodCall(BugReporter bugReporter) {
         this. = bugReporter;

implements the visitor to set and clear the stack
     public void visitClassContext(ClassContext classContext) {
         try {
              = new OpcodeStack();
         } finally {
              = null;

implements the visitor to reset the opcode stack

obj the context object of the currently parsed code block
     public void visitCode(Code obj) {

implements the visitor to look for return values of common immutable method calls, that are thrown away.

seen the opcode of the currently parsed instruction
     public void sawOpcode(int seen) {
         String methodInfo = null;
         try {
             switch (seen) {
             case INVOKEVIRTUAL:
             case INVOKEINTERFACE:
             case INVOKESTATIC:
                 String sig = getSigConstantOperand();
                 if (!sig.endsWith("V")) {
                    methodInfo = getClassConstantOperand() + "@" + getNameConstantOperand() + getSigConstantOperand();
            case POP:
            case POP2:
                if (.getStackDepth() > 0) {
                    OpcodeStack.Item item = .getStackItem(0);
                    String mInfo = (Stringitem.getUserValue();
                    if (mInfo != null) {
                        for (Pattern p : ) {
                            Matcher m = p.matcher(mInfo);
                            if (m.matches()) {
                                .reportBug(new BugInstance(this"NPMC_NON_PRODUCTIVE_METHOD_CALL", NORMAL_PRIORITY)
        } finally {
            if (methodInfo != null) {
                if (.getStackDepth() > 0) {
                    OpcodeStack.Item item = .getStackItem(0);
New to GrepCode? Check out our FAQ X