<commit_before>package edu.umd.cs.findbugs.detect;

import java.util.BitSet;
import java.util.Iterator;

import org.apache.bcel.classfile.LineNumberTable;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.ALOAD;
import org.apache.bcel.generic.ARETURN;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.MethodGen;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.Detector;
import edu.umd.cs.findbugs.SourceLineAnnotation;
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.DataflowAnalysisException;
import edu.umd.cs.findbugs.ba.Location;
import edu.umd.cs.findbugs.ba.npe.IsNullValue;
import edu.umd.cs.findbugs.ba.npe.IsNullValueDataflow;
import edu.umd.cs.findbugs.ba.npe.IsNullValueFrame;

public class LoadOfKnownNullValue implements Detector {

	private BugReporter bugReporter;

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

	public void visitClassContext(ClassContext classContext) {
		Method[] methodList = classContext.getJavaClass().getMethods();

		for (Method method : methodList) {
			if (method.getCode() == null)
				continue;

			try {
				analyzeMethod(classContext, method);
			} catch (CFGBuilderException e) {
				bugReporter.logError("Detector " + this.getClass().getName()
						+ " caught exception", e);
			} catch (DataflowAnalysisException e) {
				bugReporter.logError("Detector " + this.getClass().getName()
						+ " caught exception", e);
			}
		}
	}

	private void analyzeMethod(ClassContext classContext, Method method)
			throws CFGBuilderException, DataflowAnalysisException {
		BitSet lineMentionedMultipleTimes = ClassContext.linesMentionedMultipleTimes(method);
		BitSet linesWithLoadsOfNotDefinitelyNullValues = null;
		
		CFG cfg = classContext.getCFG(method);
		IsNullValueDataflow nullValueDataflow = classContext
				.getIsNullValueDataflow(method);
		MethodGen methodGen = classContext.getMethodGen(method);
		String sourceFile = classContext.getJavaClass().getSourceFileName();

		if (lineMentionedMultipleTimes.cardinality() > 0) {
			linesWithLoadsOfNotDefinitelyNullValues = new BitSet();
			LineNumberTable lineNumbers = method.getLineNumberTable();
			for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
				Location location = i.next();

				InstructionHandle handle = location.getHandle();
				Instruction ins = handle.getInstruction();
				if (!(ins instanceof ALOAD))
					continue;

				IsNullValueFrame frame = nullValueDataflow
						.getFactAtLocation(location);
				if (!frame.isValid()) {
					// This basic block is probably dead
					continue;
				}
				// System.out.println(handle.getPosition() + "\t" + ins.getName() +  "\t" + frame);
				
				ALOAD load = (ALOAD) ins;

				int index = load.getIndex();
				IsNullValue v = frame.getValue(index);
				if (!v.isDefinitelyNull()) {
					linesWithLoadsOfNotDefinitelyNullValues.set(lineNumbers.getSourceLine(handle.getPosition()));
				}
		}
		}
		// System.out.println(nullValueDataflow);
		for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
			Location location = i.next();

			InstructionHandle handle = location.getHandle();
			Instruction ins = handle.getInstruction();
			if (!(ins instanceof ALOAD))
				continue;

			IsNullValueFrame frame = nullValueDataflow
					.getFactAtLocation(location);
			if (!frame.isValid()) {<commit_msg>bug fix for yesterday's commit<commit_after>package edu.umd.cs.findbugs.detect;

import java.util.BitSet;
import java.util.Iterator;

import org.apache.bcel.classfile.LineNumberTable;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.ALOAD;
import org.apache.bcel.generic.ARETURN;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.MethodGen;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.Detector;
import edu.umd.cs.findbugs.SourceLineAnnotation;
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.DataflowAnalysisException;
import edu.umd.cs.findbugs.ba.Location;
import edu.umd.cs.findbugs.ba.npe.IsNullValue;
import edu.umd.cs.findbugs.ba.npe.IsNullValueDataflow;
import edu.umd.cs.findbugs.ba.npe.IsNullValueFrame;

public class LoadOfKnownNullValue implements Detector {

	private BugReporter bugReporter;

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

	public void visitClassContext(ClassContext classContext) {
		Method[] methodList = classContext.getJavaClass().getMethods();

		for (Method method : methodList) {
			if (method.getCode() == null)
				continue;

			try {
				analyzeMethod(classContext, method);
			} catch (CFGBuilderException e) {
				bugReporter.logError("Detector " + this.getClass().getName()
						+ " caught exception", e);
			} catch (DataflowAnalysisException e) {
				bugReporter.logError("Detector " + this.getClass().getName()
						+ " caught exception", e);
			}
		}
	}

	private void analyzeMethod(ClassContext classContext, Method method)
			throws CFGBuilderException, DataflowAnalysisException {
		BitSet lineMentionedMultipleTimes = ClassContext.linesMentionedMultipleTimes(method);
		BitSet linesWithLoadsOfNotDefinitelyNullValues = null;
		
		CFG cfg = classContext.getCFG(method);
		IsNullValueDataflow nullValueDataflow = classContext
				.getIsNullValueDataflow(method);
		MethodGen methodGen = classContext.getMethodGen(method);
		String sourceFile = classContext.getJavaClass().getSourceFileName();

		if (lineMentionedMultipleTimes.cardinality() > 0) {
			linesWithLoadsOfNotDefinitelyNullValues = new BitSet();
			LineNumberTable lineNumbers = method.getLineNumberTable();
			for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
				Location location = i.next();

				InstructionHandle handle = location.getHandle();
				Instruction ins = handle.getInstruction();
				if (!(ins instanceof ALOAD))
					continue;

				IsNullValueFrame frame = nullValueDataflow
						.getFactAtLocation(location);
				if (!frame.isValid()) {
					// This basic block is probably dead
					continue;
				}
				// System.out.println(handle.getPosition() + "\t" + ins.getName() +  "\t" + frame);
				
				ALOAD load = (ALOAD) ins;

				int index = load.getIndex();
				IsNullValue v = frame.getValue(index);
				if (!v.isDefinitelyNull()) {
					int sourceLine = lineNumbers.getSourceLine(handle.getPosition());
					if (sourceLine > 0)
					linesWithLoadsOfNotDefinitelyNullValues.set(sourceLine);
				}
		}
		}
		// System.out.println(nullValueDataflow);
		for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
			Location location = i.next();

			InstructionHandle handle = location.getHandle();
			Instruction ins = handle.getInstruction();
			if (!(ins instanceof ALOAD))
				continue;

			IsNullValueFrame frame = nullValueDataflow
					.getFactAtLocation(location);
			if (!frame.isValid()) {
{'fraction_non_alphanumeric': 0.1154797910365686, 'fraction_numerical': 0.0004124278251306021, 'mean_word_length': 4.583269378357636, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '5528614', 'n_tokens_mistral': 2602, 'n_tokens_neox': 2291, 'n_words': 418}