/*
 * fb-contrib - Auxiliary detectors for Java programs
 * Copyright (C) 2005-2016 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.Set;

import org.apache.bcel.classfile.Code;

import com.mebigfatguy.fbcontrib.utils.BugType;
import com.mebigfatguy.fbcontrib.utils.UnmodifiableSet;
import com.mebigfatguy.fbcontrib.utils.Values;

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 calls to Arrays.asList where the parameter is a primitive array.
 * This does not produce a list that holds the primitive boxed value, but a list
 * of one item, the array itself.
 *
 */
public class ConfusingArrayAsList extends BytecodeScanningDetector {

    private static final Set<String> PRIMITIVE_ARRAYS = UnmodifiableSet.create(
        Values.SIG_ARRAY_OF_ARRAYS_PREFIX + Values.SIG_PRIMITIVE_BYTE,
        Values.SIG_ARRAY_OF_ARRAYS_PREFIX + Values.SIG_PRIMITIVE_CHAR,
        Values.SIG_ARRAY_OF_ARRAYS_PREFIX + Values.SIG_PRIMITIVE_SHORT,
        Values.SIG_ARRAY_OF_ARRAYS_PREFIX + Values.SIG_PRIMITIVE_INT,
        Values.SIG_ARRAY_OF_ARRAYS_PREFIX + Values.SIG_PRIMITIVE_LONG,
        Values.SIG_ARRAY_OF_ARRAYS_PREFIX + Values.SIG_PRIMITIVE_FLOAT,
        Values.SIG_ARRAY_OF_ARRAYS_PREFIX + Values.SIG_PRIMITIVE_DOUBLE,
        Values.SIG_ARRAY_OF_ARRAYS_PREFIX + Values.SIG_PRIMITIVE_BOOLEAN
    );

    private BugReporter bugReporter;
    private OpcodeStack stack;

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

    /**
     * implements the visitor to create and teardown the opcode stack
     *
     * @param classContext
     *            the context object of the currently parsed class
     */
    @Override
    public void visitClassContext(ClassContext classContext) {
        try {
            stack = new OpcodeStack();
            super.visitClassContext(classContext);
        } finally {
            stack = null;
        }
    }

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

    /**
     * implements the visitor to find calls to Arrays.asList with a primitive
     * array
     *
     * @param seen
     *            the currently visitor opcode
     */
    @Override
    public void sawOpcode(int seen) {
        try {
            stack.precomputation(this);

            if (seen == INVOKESTATIC) {
                String clsName = getClassConstantOperand();
                if ("java/util/Arrays".equals(clsName)) {
                    String methodName = getNameConstantOperand();
                    if ("asList".equals(methodName) && (stack.getStackDepth() >= 1)) {
                        OpcodeStack.Item item = stack.getStackItem(0);
                        String sig = item.getSignature();
                        if (PRIMITIVE_ARRAYS.contains(sig)) {
                            bugReporter.reportBug(new BugInstance(this, BugType.CAAL_CONFUSING_ARRAY_AS_LIST.name(), NORMAL_PRIORITY).addClass(this)
                                    .addMethod(this).addSourceLine(this));
                        }
                    }
                }
            }
        } finally {
            stack.sawOpcode(this, seen);
        }
    }
}

@@ -1393,19 +1393,16 @@
 e array.
-%0A *
  This do
@@ -1475,14 +1475,14 @@
 list
-%0A *
  of
+%0A *
  one
@@ -1662,25 +1662,16 @@
 .create(
-%0A        
 Values.S
@@ -1725,32 +1725,36 @@
 E_BYTE,%0A        
+    
 Values.SIG_ARRAY
@@ -1799,24 +1799,16 @@
 VE_CHAR,
-%0A       
  Values.
@@ -1864,32 +1864,36 @@
 _SHORT,%0A        
+  
+  
 Values.SIG_ARRAY
@@ -1937,24 +1937,16 @@
 IVE_INT,
-%0A       
  Values.
@@ -2001,16 +2001,20 @@
 E_LONG,%0A
+    
         
@@ -2076,24 +2076,16 @@
 E_FLOAT,
-%0A       
  Values.
@@ -2142,32 +2142,36 @@
 DOUBLE,%0A        
+  
+  
 Values.SIG_ARRAY
@@ -2218,21 +2218,16 @@
 _BOOLEAN
-%0A    
 );%0A%0A    
@@ -3345,23 +3345,16 @@
 rimitive
-%0A     *
  array%0A 
@@ -4038,24 +4038,187 @@
 ins(sig)) %7B%0A
+                            Object con = item.getConstant();%0A                            if (!(con instanceof Integer) %7C%7C (((Integer) con).intValue() %3C= 1)) %7B%0A    
             
@@ -4390,16 +4390,20 @@
         
+    
 .addMeth
@@ -4433,16 +4433,46 @@
 this));%0A
+                            %7D%0A
         

fix FP where two or more primitive arrays are passed to Arrays.asList