/*
 * 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.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.bcel.classfile.Code;

import com.mebigfatguy.fbcontrib.utils.BugType;

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.OpcodeStack.CustomUserValue;
import edu.umd.cs.findbugs.ba.ClassContext;

/**
 * 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.
 */
@CustomUserValue
public class NonProductiveMethodCall extends BytecodeScanningDetector {

    private static final Set<Pattern> IMMUTABLE_METHODS = new HashSet<Pattern>();

    static {
        IMMUTABLE_METHODS.add(Pattern.compile(".*@toString\\(\\)Ljava/lang/String;"));
        IMMUTABLE_METHODS.add(Pattern.compile("java/lang/.+@.+Value\\(\\)[BCDFIJSZ]"));
        IMMUTABLE_METHODS.add(Pattern.compile(".*@equals\\(Ljava/lang/Object;\\)Z"));
        IMMUTABLE_METHODS.add(Pattern.compile(".*@hashCode\\(\\)I"));
        IMMUTABLE_METHODS.add(Pattern.compile(".*@clone\\(\\).+"));
        IMMUTABLE_METHODS.add(Pattern.compile("java/util/.+@toArray\\(\\)\\[.+"));
    }

    private BugReporter bugReporter;
    private OpcodeStack stack;

    /**
     * constructs a NPMC detector given the reporter to report bugs on
     * 
     * @param bugReporter
     *            the sync of bug reports
     */
    public NonProductiveMethodCall(BugReporter bugReporter) {
        this.bugReporter = bugReporter;
    }

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

    /**
     * implements the visitor to reset the opcode stack
     *
     * @param obj
     *            the context object of the currently parsed code block
     */
    @Override
    public void visitCode(Code obj) {
        stack.resetForMethodEntry(this);
        super.visitCode(obj);
    }

    /**
     * implements the visitor to look for return values of common immutable
     * method calls, that are thrown away.
     *
     * @param seen
     *            the opcode of the currently parsed instruction
     */
    @Override
    public void sawOpcode(int seen) {
        String methodInfo = null;
        try {
            stack.precomputation(this);

            switch (seen) {
            case INVOKEVIRTUAL:
            case INVOKEINTERFACE:
            case INVOKESTATIC:
                String sig = getSigConstantOperand();
                if (!sig.endsWith("V")) {
                    methodInfo = getClassConstantOperand() + "@" + getNameConstantOperand() + getSigConstantOperand();
                }
                break;

            case POP:
            case POP2:
                if (stack.getStackDepth() > 0) {
                    OpcodeStack.Item item = stack.getStackItem(0);
                    String mInfo = (String) item.getUserValue();
                    if (mInfo != null) {
                        for (Pattern p : IMMUTABLE_METHODS) {
                            Matcher m = p.matcher(mInfo);

                            if (m.matches()) {
                                bugReporter.reportBug(new BugInstance(this, BugType.NPMC_NON_PRODUCTIVE_METHOD_CALL.name(), NORMAL_PRIORITY).addClass(this)
                                        .addMethod(this).addSourceLine(this).addString(mInfo));
                                break;
                            }
                        }
                    }
                }
                break;
            }

        } finally {
            stack.sawOpcode(this, seen);
            if (methodInfo != null) {
                if (stack.getStackDepth() > 0) {
                    OpcodeStack.Item item = stack.getStackItem(0);
                    item.setUserValue(methodInfo);
                }
            }
        }
    }
}

@@ -880,34 +880,8 @@
 t;%0A%0A
-import java.util.HashSet;%0A
 impo
@@ -898,16 +898,16 @@
 il.Set;%0A
+
 import j
@@ -1050,16 +1050,72 @@
 BugType;
+%0Aimport com.mebigfatguy.fbcontrib.utils.UnmodifiableSet;
 %0A%0Aimport
@@ -1757,76 +1757,48 @@
 S = 
-new HashSet%3CPattern%3E();%0A%0A    static %7B%0A        IMMUTABLE_METHODS.add(
+UnmodifiableSet.create(%0A                
 Patt
@@ -1847,18 +1847,17 @@
 tring;%22)
-);
+,
 %0A       
@@ -1857,38 +1857,24 @@
         
-IMMUTABLE_METHODS.add(
+        
 Pattern.
@@ -1920,18 +1920,17 @@
 FIJSZ%5D%22)
-);
+,
 %0A       
@@ -1930,38 +1930,24 @@
         
-IMMUTABLE_METHODS.add(
+        
 Pattern.
@@ -1991,18 +1991,17 @@
 t;%5C%5C)Z%22)
-);
+,
 %0A       
@@ -2001,38 +2001,24 @@
         
-IMMUTABLE_METHODS.add(
+        
 Pattern.
@@ -2046,18 +2046,17 @@
 %5C(%5C%5C)I%22)
-);
+,
 %0A       
@@ -2056,38 +2056,24 @@
         
-IMMUTABLE_METHODS.add(
+        
 Pattern.
@@ -2095,26 +2095,25 @@
 ne%5C%5C(%5C%5C).+%22)
-);
+,
 %0A        IMM
@@ -2113,30 +2113,16 @@
     
-IMMUTABLE_METHODS.add(
+        
 Patt
@@ -2167,24 +2167,23 @@
 )%5C%5C%5B.+%22)
-);
 %0A    
-%7D
+);
 %0A%0A    pr

simplify with UnmodifiableSet