<commit_before>/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2003,2004 University of Maryland
 * 
 * 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 edu.umd.cs.findbugs.detect;


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

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;
import edu.umd.cs.findbugs.ba.AnalysisContext;

public class FindReturnRef extends BytecodeScanningDetector {
	boolean check = false;
	boolean thisOnTOS = false;
	boolean fieldOnTOS = false;
	boolean publicClass = false;
	boolean staticMethod = false;
	boolean dangerousToStoreIntoField = false;
	String nameOnStack;
	String classNameOnStack;
	String sigOnStack;
	int parameterCount;
	//int r;
	int timesRead [] = new int[256];
	boolean fieldIsStatic;
	private BugReporter bugReporter;
	//private LocalVariableTable variableNames;

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

	@Override
		 public void visit(JavaClass obj) {
		publicClass = obj.isPublic();
		super.visit(obj);
	}

	@Override
		 public void visit(Method obj) {
		check = publicClass && (obj.getAccessFlags() & (ACC_PUBLIC)) != 0;
		if (!check) return;
		dangerousToStoreIntoField = false;
		staticMethod = (obj.getAccessFlags() & (ACC_STATIC)) != 0;
		//variableNames = obj.getLocalVariableTable();
		parameterCount = getNumberMethodArguments();
		/*
		System.out.println(betterMethodName);
		for(int i = 0; i < parameterCount; i++)
			System.out.println("parameter " + i + ": " + obj.getArgumentTypes()[i]);
		*/

		if (!staticMethod) parameterCount++;

		for (int i = 0; i < parameterCount; i++)
			timesRead[i] = 0;
		thisOnTOS = false;
		fieldOnTOS = false;
		super.visit(obj);
		thisOnTOS = false;
		fieldOnTOS = false;
	}


	@Override
		 public void visit(Code obj) {
		if (check) super.visit(obj);
	}

	@Override
		 public void sawOpcode(int seen) {
				
		if (!check) return;
		
		if (staticMethod && dangerousToStoreIntoField && seen == PUTSTATIC
				&& MutableStaticFields.mutableSignature(getSigConstantOperand())) {
			bugReporter.reportBug(new BugInstance(this, "EI_EXPOSE_STATIC_REP2", NORMAL_PRIORITY)
					.addClassAndMethod(this)
					.addField(getDottedClassConstantOperand(), getNameConstantOperand(), getSigConstantOperand(),
							true)
					.addSourceLine(this));
		}
		if (!staticMethod && dangerousToStoreIntoField && seen == PUTFIELD
				&& MutableStaticFields.mutableSignature(getSigConstantOperand())) {
			bugReporter.reportBug(new BugInstance(this, "EI_EXPOSE_REP2", NORMAL_PRIORITY)
					.addClassAndMethod(this)
					.addField(getDottedClassConstantOperand(), getNameConstantOperand(), getSigConstantOperand(),
							true)
					.addSourceLine(this));
		}
		dangerousToStoreIntoField = false;<commit_msg>bug fix for [ 1927503 ] Invalid EI_EXPOSE_REP2 warning on field Was annotation as a static field<commit_after>/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2003,2004 University of Maryland
 * 
 * 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 edu.umd.cs.findbugs.detect;


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

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;
import edu.umd.cs.findbugs.ba.AnalysisContext;

public class FindReturnRef extends BytecodeScanningDetector {
	boolean check = false;
	boolean thisOnTOS = false;
	boolean fieldOnTOS = false;
	boolean publicClass = false;
	boolean staticMethod = false;
	boolean dangerousToStoreIntoField = false;
	String nameOnStack;
	String classNameOnStack;
	String sigOnStack;
	int parameterCount;
	//int r;
	int timesRead [] = new int[256];
	boolean fieldIsStatic;
	private BugReporter bugReporter;
	//private LocalVariableTable variableNames;

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

	@Override
		 public void visit(JavaClass obj) {
		publicClass = obj.isPublic();
		super.visit(obj);
	}

	@Override
		 public void visit(Method obj) {
		check = publicClass && (obj.getAccessFlags() & (ACC_PUBLIC)) != 0;
		if (!check) return;
		dangerousToStoreIntoField = false;
		staticMethod = (obj.getAccessFlags() & (ACC_STATIC)) != 0;
		//variableNames = obj.getLocalVariableTable();
		parameterCount = getNumberMethodArguments();
		/*
		System.out.println(betterMethodName);
		for(int i = 0; i < parameterCount; i++)
			System.out.println("parameter " + i + ": " + obj.getArgumentTypes()[i]);
		*/

		if (!staticMethod) parameterCount++;

		for (int i = 0; i < parameterCount; i++)
			timesRead[i] = 0;
		thisOnTOS = false;
		fieldOnTOS = false;
		super.visit(obj);
		thisOnTOS = false;
		fieldOnTOS = false;
	}


	@Override
		 public void visit(Code obj) {
		if (check) super.visit(obj);
	}

	@Override
		 public void sawOpcode(int seen) {
				
		if (!check) return;
		
		if (staticMethod && dangerousToStoreIntoField && seen == PUTSTATIC
				&& MutableStaticFields.mutableSignature(getSigConstantOperand())) {
			bugReporter.reportBug(new BugInstance(this, "EI_EXPOSE_STATIC_REP2", NORMAL_PRIORITY)
					.addClassAndMethod(this)
					.addReferencedField(this)
					.addSourceLine(this));
		}
		if (!staticMethod && dangerousToStoreIntoField && seen == PUTFIELD
				&& MutableStaticFields.mutableSignature(getSigConstantOperand())) {
			bugReporter.reportBug(new BugInstance(this, "EI_EXPOSE_REP2", NORMAL_PRIORITY)
					.addClassAndMethod(this)
					.addReferencedField(this)
					.addSourceLine(this));
		}
		dangerousToStoreIntoField = false;
{'fraction_non_alphanumeric': 0.10063246661981728, 'fraction_numerical': 0.0106816584680253, 'mean_word_length': 4.436210847975554, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, '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': '5999497', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2172, 'n_words': 621}