<commit_before>/*
 * fb-contrib - Auxiliary detectors for Java programs
 * Copyright (C) 2005-2015 <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.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.bcel.classfile.Code;

import com.mebigfatguy.fbcontrib.utils.BugType;
import com.mebigfatguy.fbcontrib.utils.RegisterUtils;
import com.mebigfatguy.fbcontrib.utils.TernaryPatcher;

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 creation of java.awt.Graphics object that do not have the
 * .dispose() method called on them when finished. These objects will be cleaned
 * up by the Garbage collector, bug given the likelyhood that large numbers of
 * these objects can be created in a short period of time, it is better to
 * dispose them as soon as possible
 */
@CustomUserValue
public class LingeringGraphicsObjects extends BytecodeScanningDetector {

    private static final Set<String> GRAPHICS_PRODUCERS = new HashSet<String>(2);

    static {
        GRAPHICS_PRODUCERS.add("java/awt/image/BufferedImage#getGraphics()Ljava/awt/Graphics;");
        GRAPHICS_PRODUCERS.add("java/awt/Graphics#create()Ljava/awt/Graphics;");
    }

    private final BugReporter bugReporter;
    private OpcodeStack stack;
    private Map<Integer, Integer> graphicsRegs; // reg->pc

    public LingeringGraphicsObjects(BugReporter bugReporter) {
        this.bugReporter = bugReporter;
    }

    @Override
    public void visitClassContext(ClassContext classContext) {
        try {
            stack = new OpcodeStack();
            graphicsRegs = new HashMap<Integer, Integer>(5);
            super.visitClassContext(classContext);
        } finally {
            stack = null;
            graphicsRegs = null;
        }
    }

    @Override
    public void visitCode(Code obj) {
        stack.resetForMethodEntry(this);
        graphicsRegs.clear();
        super.visitCode(obj);
        for (Integer pc : graphicsRegs.values()) {
            bugReporter.reportBug(new BugInstance(this, BugType.LGO_LINGERING_GRAPHICS_OBJECT.name(), NORMAL_PRIORITY).addClass(this).addMethod(this)
                    .addSourceLine(this, pc.intValue()));
        }

    }

    @Override
    public void sawOpcode(int seen) {
        Integer sawNewGraphicsAt = null;
        try {
            stack.precomputation(this);<commit_msg>javadoc<commit_after>/*
 * fb-contrib - Auxiliary detectors for Java programs
 * Copyright (C) 2005-2015 <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.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.bcel.classfile.Code;

import com.mebigfatguy.fbcontrib.utils.BugType;
import com.mebigfatguy.fbcontrib.utils.RegisterUtils;
import com.mebigfatguy.fbcontrib.utils.TernaryPatcher;

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 creation of java.awt.Graphics object that do not have the
 * .dispose() method called on them when finished. These objects will be cleaned
 * up by the Garbage collector, bug given the likelyhood that large numbers of
 * these objects can be created in a short period of time, it is better to
 * dispose them as soon as possible
 */
@CustomUserValue
public class LingeringGraphicsObjects extends BytecodeScanningDetector {

    private static final Set<String> GRAPHICS_PRODUCERS = new HashSet<String>(2);

    static {
        GRAPHICS_PRODUCERS.add("java/awt/image/BufferedImage#getGraphics()Ljava/awt/Graphics;");
        GRAPHICS_PRODUCERS.add("java/awt/Graphics#create()Ljava/awt/Graphics;");
    }

    private final BugReporter bugReporter;
    private OpcodeStack stack;
    private Map<Integer, Integer> graphicsRegs; // reg->pc

    public LingeringGraphicsObjects(BugReporter bugReporter) {
        this.bugReporter = bugReporter;
    }

    /**
     * overrides the visitor to set up the opcode stack
     * 
     * @param classContext the context object of the currently parsed class
     */
    @Override
    public void visitClassContext(ClassContext classContext) {
        try {
            stack = new OpcodeStack();
            graphicsRegs = new HashMap<Integer, Integer>(5);
            super.visitClassContext(classContext);
        } finally {
            stack = null;
            graphicsRegs = null;
        }
    }

    /**
     * overrides the visitor to check for registers that have been assigned Graphics objects that haven't been disposed
     * 
     * @param obj the code block of the currently parsed method
     */
    @Override
    public void visitCode(Code obj) {
        stack.resetForMethodEntry(this);
        graphicsRegs.clear();
        super.visitCode(obj);
        for (Integer pc : graphicsRegs.values()) {
            bugReporter.reportBug(new BugInstance(this, BugType.LGO_LINGERING_GRAPHICS_OBJECT.name(), NORMAL_PRIORITY).addClass(this).addMethod(this)
                    .addSourceLine(this, pc.intValue()));
        }

    }

    @Override
    public void sawOpcode(int seen) {
        Integer sawNewGraphicsAt = null;
        try {
            stack.precomputation(this);
{'fraction_non_alphanumeric': 0.08309739524348811, 'fraction_numerical': 0.007361268403171008, 'mean_word_length': 3.3638048177887585, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3872549', 'n_tokens_mistral': 2101, 'n_tokens_neox': 1992, 'n_words': 633}