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

import org.apache.bcel.Constants;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.FieldInstruction;
import org.apache.bcel.generic.GETFIELD;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.InstructionHandle;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;
import edu.umd.cs.findbugs.FieldAnnotation;
import edu.umd.cs.findbugs.SourceLineAnnotation;
import edu.umd.cs.findbugs.ba.BasicBlock;
import edu.umd.cs.findbugs.ba.CFG;
import edu.umd.cs.findbugs.ba.CFGBuilderException;
import edu.umd.cs.findbugs.ba.ClassContext;
import edu.umd.cs.findbugs.ba.Edge;
import edu.umd.cs.findbugs.ba.BasicBlock.InstructionIterator;

/**
 * finds fields that are used in a locals only fashion, specifically private fields
 * that are accessed first in each method with a store vs. a load.
 */
public class FieldCouldBeLocal extends BytecodeScanningDetector
{
	private final BugReporter bugReporter;
	private ClassContext clsContext;
	private Map<String, FieldInfo> localizableFields;
	private CFG cfg;
	private ConstantPoolGen cpg;
	private BitSet visitedBlocks;

    /**
     * constructs a FCBL detector given the reporter to report bugs on.

     * @param bugReporter the sync of bug reports
     */
	public FieldCouldBeLocal(BugReporter bugReporter) {
		this.bugReporter = bugReporter;
	}

	/**
	 * overrides the visitor to collect localizable fields, and then report those that
	 * survive all method checks.
	 *
	 * @param classContext the context object that holds the JavaClass parsed
	 */
	@Override
	public void visitClassContext(ClassContext classContext) {
		try {
	        localizableFields = new HashMap<String, FieldInfo>();
	        visitedBlocks = new BitSet();
			clsContext = classContext;
			JavaClass cls = classContext.getJavaClass();
			Field[] fields = cls.getFields();
			for (Field f : fields) {
				if ((!f.isStatic() && f.getName().indexOf('$') < 0) && f.isPrivate()) {
					FieldAnnotation fa = new FieldAnnotation(cls.getClassName(), f.getName(), f.getSignature(), false);
					localizableFields.put(f.getName(), new FieldInfo(fa));
				}
			}

			if (localizableFields.size() > 0) {
				super.visitClassContext(classContext);
				for (FieldInfo fi : localizableFields.values()) {
					FieldAnnotation fa = fi.getFieldAnnotation();
					SourceLineAnnotation sla = fi.getSrcLineAnnotation();
					BugInstance bug = new BugInstance(this, "FCBL_FIELD_COULD_BE_LOCAL", NORMAL_PRIORITY)
													.addClass(this)
													.addField(fa);
					if (sla != null)
						bug.addSourceLine(sla);
					bugReporter.reportBug(bug);
				}
			}
		} finally {
	        localizableFields = null;
	        visitedBlocks = null;
	        clsContext = null;
		}
	}

	/**
	 * overrides the visitor to navigate basic blocks looking for all first usages of fields, removing
	 * those that are read from first.
	 *
	 * @param obj the context object of the currently parsed method
	 */
	@Override
	public void visitMethod(Method obj) {
		if (localizableFields.isEmpty())
			return;

		try {
			cpg = new ConstantPoolGen(getConstantPool());
			cfg = clsContext.getCFG(obj);
			BasicBlock bb = cfg.getEntry();
			Set<String> uncheckedFields = new HashSet<String>(localizableFields.keySet());<commit_msg>fix for SF:3601264 - get the constant pool from the CFG, not the class when using CFG<commit_after>/*
 * fb-contrib - Auxiliary detectors for Java programs
 * Copyright (C) 2005-2013 <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.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.apache.bcel.Constants;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.FieldInstruction;
import org.apache.bcel.generic.GETFIELD;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.InstructionHandle;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;
import edu.umd.cs.findbugs.FieldAnnotation;
import edu.umd.cs.findbugs.SourceLineAnnotation;
import edu.umd.cs.findbugs.ba.BasicBlock;
import edu.umd.cs.findbugs.ba.CFG;
import edu.umd.cs.findbugs.ba.CFGBuilderException;
import edu.umd.cs.findbugs.ba.ClassContext;
import edu.umd.cs.findbugs.ba.Edge;
import edu.umd.cs.findbugs.ba.BasicBlock.InstructionIterator;

/**
 * finds fields that are used in a locals only fashion, specifically private fields
 * that are accessed first in each method with a store vs. a load.
 */
public class FieldCouldBeLocal extends BytecodeScanningDetector
{
	private final BugReporter bugReporter;
	private ClassContext clsContext;
	private Map<String, FieldInfo> localizableFields;
	private CFG cfg;
	private ConstantPoolGen cpg;
	private BitSet visitedBlocks;

    /**
     * constructs a FCBL detector given the reporter to report bugs on.

     * @param bugReporter the sync of bug reports
     */
	public FieldCouldBeLocal(BugReporter bugReporter) {
		this.bugReporter = bugReporter;
	}

	/**
	 * overrides the visitor to collect localizable fields, and then report those that
	 * survive all method checks.
	 *
	 * @param classContext the context object that holds the JavaClass parsed
	 */
	@Override
	public void visitClassContext(ClassContext classContext) {
		try {
	        localizableFields = new HashMap<String, FieldInfo>();
	        visitedBlocks = new BitSet();
			clsContext = classContext;
			JavaClass cls = classContext.getJavaClass();
			Field[] fields = cls.getFields();
			for (Field f : fields) {
				if ((!f.isStatic() && f.getName().indexOf('$') < 0) && f.isPrivate()) {
					FieldAnnotation fa = new FieldAnnotation(cls.getClassName(), f.getName(), f.getSignature(), false);
					localizableFields.put(f.getName(), new FieldInfo(fa));
				}
			}

			if (localizableFields.size() > 0) {
				super.visitClassContext(classContext);
				for (FieldInfo fi : localizableFields.values()) {
					FieldAnnotation fa = fi.getFieldAnnotation();
					SourceLineAnnotation sla = fi.getSrcLineAnnotation();
					BugInstance bug = new BugInstance(this, "FCBL_FIELD_COULD_BE_LOCAL", NORMAL_PRIORITY)
													.addClass(this)
													.addField(fa);
					if (sla != null)
						bug.addSourceLine(sla);
					bugReporter.reportBug(bug);
				}
			}
		} finally {
	        localizableFields = null;
	        visitedBlocks = null;
	        clsContext = null;
		}
	}

	/**
	 * overrides the visitor to navigate basic blocks looking for all first usages of fields, removing
	 * those that are read from first.
	 *
	 * @param obj the context object of the currently parsed method
	 */
	@Override
	public void visitMethod(Method obj) {
		if (localizableFields.isEmpty())
			return;

		try {

			cfg = clsContext.getCFG(obj);
			cpg = cfg.getMethodGen().getConstantPool();
			BasicBlock bb = cfg.getEntry();
			Set<String> uncheckedFields = new HashSet<String>(localizableFields.keySet());
{'fraction_non_alphanumeric': 0.09575967899360156, 'fraction_numerical': 0.00639843834725084, 'mean_word_length': 3.984864864864865, 'pattern_counts': {'":': 0, '<': 15, '<?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': '3225446', 'n_tokens_mistral': 3026, 'n_tokens_neox': 2722, 'n_words': 835}