<commit_before>package de.simonscholz.junit4converter;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;

public class JUnit4Converter {

	private static final String OVERRIDE_ANNOTATION_NAME = "Override";
	private static final String TEST_ANNOTATION_QUALIFIED_NAME = "org.junit.Test";
	private static final String TEST_ANNOTATION_NAME = "Test";
	private static final String BEFORE_ANNOTATION_QUALIFIED_NAME = "org.junit.Before";
	private static final String BEFORE_ANNOTATION_NAME = "Before";
	private static final String AFTER_ANNOTATION_QUALIFIED_NAME = "org.junit.After";
	private static final String AFTER_ANNOTATION_NAME = "After";

	private static final String TEST_CASE_QUALIFIED_NAME = "junit.framework.TestCase";
	private static final String TEST_CASE_CLASSNAME = "TestCase";

	private static final String SET_UP_METHOD_NAME = "setUp";
	private static final String TEAR_DOWN_METHOD_NAME = "tearDown";

	private static final String TEST_METHOD_PREFIX = "test";

	private boolean modifiedDocument;

	public void convert(IJavaProject javaProject,
			IProgressMonitor progressMonitor) throws MalformedTreeException,
			BadLocationException, CoreException {
		IPackageFragment[] packageFragments = javaProject.getPackageFragments();
		progressMonitor.beginTask(
				"Converting JavaProject " + javaProject.getElementName(),
				packageFragments.length);
		AtomicInteger atomicInteger = new AtomicInteger();
		for (IPackageFragment packageFragment : packageFragments) {
			convert(packageFragment, progressMonitor);
			progressMonitor.worked(atomicInteger.incrementAndGet());
		}
	}

	public void convert(IPackageFragment packageFragment,
			IProgressMonitor progressMonitor) throws MalformedTreeException,
			BadLocationException, CoreException {
		ICompilationUnit[] compilationUnits = packageFragment
				.getCompilationUnits();
		progressMonitor.beginTask("Converting CompilationsUnits of package"
				+ packageFragment.getElementName(), compilationUnits.length);
		AtomicInteger atomicInteger = new AtomicInteger();
		for (ICompilationUnit compilationUnit : compilationUnits) {
			convert(compilationUnit, progressMonitor);
			progressMonitor.worked(atomicInteger.incrementAndGet());
		}
	}

	public void convert(ICompilationUnit cu, IProgressMonitor monitor) throws MalformedTreeException,
			BadLocationException, CoreException, BadLocationException {

		// parse compilation unit
		final ASTParser parser = ASTParser.newParser(AST.JLS8);
		parser.setSource(cu);
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
		parser.setResolveBindings(true);
		final CompilationUnit astRoot = (CompilationUnit) parser
				.createAST(null);
		// create a ASTRewrite
		final AST ast = astRoot.getAST();
		final ASTRewrite rewriter = ASTRewrite.create(ast);
		ImportRewrite importRewrite = ImportRewrite.create(astRoot, true);
		modifiedDocument = false;

		List types = astRoot.types();

		for (Object object : types) {
			if (object instanceof TypeDeclaration) {
				TypeDeclaration typeDeclaration = (TypeDeclaration) object;

				removeTestCaseSuperclass(rewriter, importRewrite,
						typeDeclaration);

				convertTestMethods(ast, rewriter, importRewrite,
						typeDeclaration);
			}
		}

		saveChanges(cu, monitor, rewriter, importRewrite);
	}

	protected void saveChanges(ICompilationUnit cu, IProgressMonitor monitor,
			final ASTRewrite rewriter, ImportRewrite importRewrite)
			throws CoreException, JavaModelException, BadLocationException {
		if (modifiedDocument) {
			TextEdit importEdits = importRewrite.rewriteImports(monitor);
			TextEdit edits = rewriter.rewriteAST();
			importEdits.addChild(edits);

			// apply the text edits to the compilation unit
			Document document = new Document(cu.getSource());
			importEdits.apply(document);
			// edits.apply(document);

			// this is the code for adding statements
			cu.getBuffer().setContents(document.get());
			cu.save(null, true);
		}
	}

	protected void convertTestMethods(final AST ast, final ASTRewrite rewriter,
			final ImportRewrite importRewrite, TypeDeclaration typeDeclaration) {
		MethodDeclaration[] methods = typeDeclaration.getMethods();
		for (MethodDeclaration methodDeclaration : methods) {
			SimpleName name = methodDeclaration.getName();
			String fullyQualifiedName = name.getFullyQualifiedName();
			if (fullyQualifiedName.toLowerCase().startsWith(
					TEST_METHOD_PREFIX)) {
				createMarkerAnnotation(ast, rewriter,
						methodDeclaration, TEST_ANNOTATION_NAME);
				importRewrite.addImport(TEST_ANNOTATION_QUALIFIED_NAME);
				methodDeclaration.accept(new StaticAssertImportVisitor(
						importRewrite));
				modifiedDocument = true;
			} else if (SET_UP_METHOD_NAME.equals(fullyQualifiedName)) {
				removeAnnotation(rewriter, methodDeclaration,
						OVERRIDE_ANNOTATION_NAME);
				createMarkerAnnotation(ast, rewriter,<commit_msg>Add static imports for asserts in every method not only in test methods<commit_after>package de.simonscholz.junit4converter;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;

public class JUnit4Converter {

	private static final String OVERRIDE_ANNOTATION_NAME = "Override";
	private static final String TEST_ANNOTATION_QUALIFIED_NAME = "org.junit.Test";
	private static final String TEST_ANNOTATION_NAME = "Test";
	private static final String BEFORE_ANNOTATION_QUALIFIED_NAME = "org.junit.Before";
	private static final String BEFORE_ANNOTATION_NAME = "Before";
	private static final String AFTER_ANNOTATION_QUALIFIED_NAME = "org.junit.After";
	private static final String AFTER_ANNOTATION_NAME = "After";

	private static final String TEST_CASE_QUALIFIED_NAME = "junit.framework.TestCase";
	private static final String TEST_CASE_CLASSNAME = "TestCase";

	private static final String SET_UP_METHOD_NAME = "setUp";
	private static final String TEAR_DOWN_METHOD_NAME = "tearDown";

	private static final String TEST_METHOD_PREFIX = "test";

	private boolean modifiedDocument;

	public void convert(IJavaProject javaProject,
			IProgressMonitor progressMonitor) throws MalformedTreeException,
			BadLocationException, CoreException {
		IPackageFragment[] packageFragments = javaProject.getPackageFragments();
		progressMonitor.beginTask(
				"Converting JavaProject " + javaProject.getElementName(),
				packageFragments.length);
		AtomicInteger atomicInteger = new AtomicInteger();
		for (IPackageFragment packageFragment : packageFragments) {
			convert(packageFragment, progressMonitor);
			progressMonitor.worked(atomicInteger.incrementAndGet());
		}
	}

	public void convert(IPackageFragment packageFragment,
			IProgressMonitor progressMonitor) throws MalformedTreeException,
			BadLocationException, CoreException {
		ICompilationUnit[] compilationUnits = packageFragment
				.getCompilationUnits();
		progressMonitor.beginTask("Converting CompilationsUnits of package"
				+ packageFragment.getElementName(), compilationUnits.length);
		AtomicInteger atomicInteger = new AtomicInteger();
		for (ICompilationUnit compilationUnit : compilationUnits) {
			convert(compilationUnit, progressMonitor);
			progressMonitor.worked(atomicInteger.incrementAndGet());
		}
	}

	public void convert(ICompilationUnit cu, IProgressMonitor monitor) throws MalformedTreeException,
			BadLocationException, CoreException, BadLocationException {

		// parse compilation unit
		final ASTParser parser = ASTParser.newParser(AST.JLS8);
		parser.setSource(cu);
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
		parser.setResolveBindings(true);
		final CompilationUnit astRoot = (CompilationUnit) parser
				.createAST(null);
		// create a ASTRewrite
		final AST ast = astRoot.getAST();
		final ASTRewrite rewriter = ASTRewrite.create(ast);
		ImportRewrite importRewrite = ImportRewrite.create(astRoot, true);
		modifiedDocument = false;

		List types = astRoot.types();

		for (Object object : types) {
			if (object instanceof TypeDeclaration) {
				TypeDeclaration typeDeclaration = (TypeDeclaration) object;

				removeTestCaseSuperclass(rewriter, importRewrite,
						typeDeclaration);

				convertTestMethods(ast, rewriter, importRewrite,
						typeDeclaration);
			}
		}

		saveChanges(cu, monitor, rewriter, importRewrite);
	}

	protected void saveChanges(ICompilationUnit cu, IProgressMonitor monitor,
			final ASTRewrite rewriter, ImportRewrite importRewrite)
			throws CoreException, JavaModelException, BadLocationException {
		if (modifiedDocument) {
			TextEdit importEdits = importRewrite.rewriteImports(monitor);
			TextEdit edits = rewriter.rewriteAST();
			importEdits.addChild(edits);

			// apply the text edits to the compilation unit
			Document document = new Document(cu.getSource());
			importEdits.apply(document);
			// edits.apply(document);

			// this is the code for adding statements
			cu.getBuffer().setContents(document.get());
			cu.save(null, true);
		}
	}

	protected void convertTestMethods(final AST ast, final ASTRewrite rewriter,
			final ImportRewrite importRewrite, TypeDeclaration typeDeclaration) {
		MethodDeclaration[] methods = typeDeclaration.getMethods();
		for (MethodDeclaration methodDeclaration : methods) {
			SimpleName name = methodDeclaration.getName();
			String fullyQualifiedName = name.getFullyQualifiedName();
			
			methodDeclaration.accept(new StaticAssertImportVisitor(
					importRewrite));
			
			if (fullyQualifiedName.toLowerCase().startsWith(
					TEST_METHOD_PREFIX)) {
				createMarkerAnnotation(ast, rewriter,
						methodDeclaration, TEST_ANNOTATION_NAME);
				importRewrite.addImport(TEST_ANNOTATION_QUALIFIED_NAME);
				modifiedDocument = true;
			} else if (SET_UP_METHOD_NAME.equals(fullyQualifiedName)) {
				removeAnnotation(rewriter, methodDeclaration,
						OVERRIDE_ANNOTATION_NAME);
				createMarkerAnnotation(ast, rewriter,
{'fraction_non_alphanumeric': 0.08837890625, 'fraction_numerical': 0.00048828125, 'mean_word_length': 5.748489840746842, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2281258', 'n_tokens_mistral': 3934, 'n_tokens_neox': 3494, 'n_words': 737}