<commit_before>/*
 * fb-contrib - Auxiliary detectors for Java programs
 * Copyright (C) 2005-2014 <NAME>
 * 
 * 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 org.apache.bcel.Repository;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.JavaClass;

import com.mebigfatguy.fbcontrib.collect.ImmutabilityType;
import com.mebigfatguy.fbcontrib.collect.MethodInfo;
import com.mebigfatguy.fbcontrib.collect.Statistics;

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;

@CustomUserValue
public class ModifyingUnmodifiableCollection extends BytecodeScanningDetector {

    private static Set<String> MODIFYING_METHODS = null;
    private static JavaClass LIST_CLASS = null;
    private static JavaClass SET_CLASS = null;
    private static JavaClass MAP_CLASS = null;
    
    static {
        try {
            MODIFYING_METHODS = new HashSet<String>();
            MODIFYING_METHODS.add("add(Ljava/lang/Object;)Z");
            MODIFYING_METHODS.add("remove(Ljava/lang/Object;)Z");
            MODIFYING_METHODS.add("addAll(Ljava/util/Collection;)Z");
            MODIFYING_METHODS.add("retainAll(Ljava/util/Collection;)Z");
            MODIFYING_METHODS.add("removeAll(Ljava/util/Collection;)Z");
            MODIFYING_METHODS.add("put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
            MODIFYING_METHODS.add("remove(Ljava/lang/Object;)Ljava/lang/Object;");
            MODIFYING_METHODS.add("putAll(Ljava/util/Map;)V;");
            
            LIST_CLASS = Repository.lookupClass("java.util.List");
            SET_CLASS = Repository.lookupClass("java.util.Set");
            MAP_CLASS = Repository.lookupClass("java.util.Map");
        } catch (ClassNotFoundException cnfe) {
        }
    }
    
    private BugReporter bugReporter;
    private OpcodeStack stack;
    private ImmutabilityType reportedType;
    
    public ModifyingUnmodifiableCollection(BugReporter reporter) {
        bugReporter = reporter;
    }
    
    @Override
    public void visitClassContext(ClassContext context) {
        try {
            stack = new OpcodeStack();
            super.visitClassContext(context);
        } finally {
            stack = null;
        }
    }
    
    @Override
    public void visitCode(Code obj) {
        stack.resetForMethodEntry(this);
        reportedType = ImmutabilityType.UNKNOWN;
        super.visitCode(obj);
    }
    
    public void sawOpcode(int seen) {
        
        if (reportedType == ImmutabilityType.IMMUTABLE) {
            return;
        }
        ImmutabilityType imType = null;

        try {
            stack.precomputation(this);
            
            switch (seen) {
                case INVOKESTATIC:
                case INVOKEINTERFACE:
                case INVOKESPECIAL:
                case INVOKEVIRTUAL: {
                    String className = getClassConstantOperand();
                    String methodName = getNameConstantOperand();
                    String signature = getSigConstantOperand();
                    
                    MethodInfo mi = Statistics.getStatistics().getMethodStatistics(className, methodName, signature);
                    imType = mi.getImmutabilityType();
                    
                    if ((seen == INVOKEINTERFACE) && MODIFYING_METHODS.contains(methodName + signature) && (isCollection(className))) {
                        if (stack.getStackDepth() > 0) {
                            OpcodeStack.Item item = stack.getStackItem(0);
                            ImmutabilityType type = (ImmutabilityType) item.getUserValue();
                            
                            if ((type == ImmutabilityType.IMMUTABLE) || ((type == ImmutabilityType.POSSIBLY_IMMUTABLE) && (reportedType != ImmutabilityType.POSSIBLY_IMMUTABLE))) {
                                bugReporter.reportBug(new BugInstance(this, "MUC_MODIFYING_UNMODIFIABLE_COLLECTION", (type == ImmutabilityType.IMMUTABLE) ? HIGH_PRIORITY : NORMAL_PRIORITY)
                                                          .addClass(this)
                                                          .addMethod(this)
                                                          .addSourceLine(this));
                                reportedType = type;
                            }
                        }
                    }
                }
                break;
            }

        } finally {
            stack.sawOpcode(this, seen);
            if (imType != null) {
                if (stack.getStackDepth() > 0) {
                    OpcodeStack.Item item = stack.getStackItem(0);
                    item.setUserValue(imType);
                }
            }
            
        }
    }
    <commit_msg>get MUC starting to find simplistic cases<commit_after>/*
 * fb-contrib - Auxiliary detectors for Java programs
 * Copyright (C) 2005-2014 <NAME>
 * 
 * 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.HashMap;
import java.util.Map;

import org.apache.bcel.Repository;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.JavaClass;

import com.mebigfatguy.fbcontrib.collect.ImmutabilityType;
import com.mebigfatguy.fbcontrib.collect.MethodInfo;
import com.mebigfatguy.fbcontrib.collect.Statistics;

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;

@CustomUserValue
public class ModifyingUnmodifiableCollection extends BytecodeScanningDetector {

    private static Map<String, Integer> MODIFYING_METHODS = null;
    private static JavaClass LIST_CLASS = null;
    private static JavaClass SET_CLASS = null;
    private static JavaClass MAP_CLASS = null;
    
    static {
        try {
            Integer one = Integer.valueOf(1);
            MODIFYING_METHODS = new HashMap<String, Integer>();
            MODIFYING_METHODS.put("add(Ljava/lang/Object;)Z", one);
            MODIFYING_METHODS.put("remove(Ljava/lang/Object;)Z", one);
            MODIFYING_METHODS.put("addAll(Ljava/util/Collection;)Z", one);
            MODIFYING_METHODS.put("retainAll(Ljava/util/Collection;)Z", one);
            MODIFYING_METHODS.put("removeAll(Ljava/util/Collection;)Z", one);
            MODIFYING_METHODS.put("put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", Integer.valueOf(2));
            MODIFYING_METHODS.put("remove(Ljava/lang/Object;)Ljava/lang/Object;", one);
            MODIFYING_METHODS.put("putAll(Ljava/util/Map;)V;", one);
            
            LIST_CLASS = Repository.lookupClass("java.util.List");
            SET_CLASS = Repository.lookupClass("java.util.Set");
            MAP_CLASS = Repository.lookupClass("java.util.Map");
        } catch (ClassNotFoundException cnfe) {
        }
    }
    
    private BugReporter bugReporter;
    private OpcodeStack stack;
    private ImmutabilityType reportedType;
    
    public ModifyingUnmodifiableCollection(BugReporter reporter) {
        bugReporter = reporter;
    }
    
    @Override
    public void visitClassContext(ClassContext context) {
        try {
            stack = new OpcodeStack();
            super.visitClassContext(context);
        } finally {
            stack = null;
        }
    }
    
    @Override
    public void visitCode(Code obj) {
        stack.resetForMethodEntry(this);
        reportedType = ImmutabilityType.UNKNOWN;
        super.visitCode(obj);
    }
    
    public void sawOpcode(int seen) {
        
        if (reportedType == ImmutabilityType.IMMUTABLE) {
            return;
        }
        ImmutabilityType imType = null;

        try {
            stack.precomputation(this);
            
            switch (seen) {
                case INVOKESTATIC:
                case INVOKEINTERFACE:
                case INVOKESPECIAL:
                case INVOKEVIRTUAL: {
                    String className = getClassConstantOperand();
                    String methodName = getNameConstantOperand();
                    String signature = getSigConstantOperand();
                    
                    MethodInfo mi = Statistics.getStatistics().getMethodStatistics(className, methodName, signature);
                    imType = mi.getImmutabilityType();
                    
                    if (seen == INVOKEINTERFACE) {
                        Integer collectionOffset = MODIFYING_METHODS.get(methodName + signature);
                        if (collectionOffset != null) {
                            if (stack.getStackDepth() > collectionOffset) {
                                OpcodeStack.Item item = stack.getStackItem(collectionOffset);
                                ImmutabilityType type = (ImmutabilityType) item.getUserValue();
                                
                                if ((type == ImmutabilityType.IMMUTABLE) || ((type == ImmutabilityType.POSSIBLY_IMMUTABLE) && (reportedType != ImmutabilityType.POSSIBLY_IMMUTABLE))) {
                                    bugReporter.reportBug(new BugInstance(this, "MUC_MODIFYING_UNMODIFIABLE_COLLECTION", (type == ImmutabilityType.IMMUTABLE) ? HIGH_PRIORITY : NORMAL_PRIORITY)
                                                              .addClass(this)
                                                              .addMethod(this)
                                                              .addSourceLine(this));
                                    reportedType = type;
                                }
                            }
                        }
                    }
                }
                break;
            }

        } finally {
            stack.sawOpcode(this, seen);
            if (imType != null) {
                if (stack.getStackDepth() > 0) {
                    OpcodeStack.Item item = stack.getStackItem(0);
                    item.setUserValue(imType);
                }
            }
            
        }
    }
    
{'fraction_non_alphanumeric': 0.08607251779057946, 'fraction_numerical': 0.004744154523890207, 'mean_word_length': 1.9564237415477086, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5630717', 'n_tokens_mistral': 3259, 'n_tokens_neox': 3121, 'n_words': 681}