RuleTemporayFolderJUnitPlugin.java
/*******************************************************************************
* Copyright (c) 2021 Carsten Hammer.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Carsten Hammer
*******************************************************************************/
package org.sandbox.jdt.internal.corext.fix.helper;
import static org.sandbox.jdt.internal.corext.fix.helper.lib.JUnitConstants.*;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.corext.util.AnnotationUtils;
import org.sandbox.jdt.internal.common.AstProcessorBuilder;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.helper.lib.JunitHolder;
import org.sandbox.jdt.internal.corext.fix.helper.lib.TriggerPatternCleanupPlugin;
import org.sandbox.jdt.internal.corext.fix.helper.lib.TestNameRefactorer;
import org.sandbox.jdt.triggerpattern.api.CleanupPattern;
import org.sandbox.jdt.triggerpattern.api.Match;
import org.sandbox.jdt.triggerpattern.api.PatternKind;
/**
* Plugin to migrate JUnit 4 TemporaryFolder rule to JUnit 5 @TempDir.
*
* @since 1.3.0
*/
@CleanupPattern(value = "@Rule public TemporaryFolder $name", kind = PatternKind.FIELD, qualifiedType = ORG_JUNIT_RULES_TEMPORARY_FOLDER, cleanupId = "cleanup.junit.ruletemporaryfolder", description = "Migrate @Rule TemporaryFolder to @TempDir", displayName = "JUnit 4 @Rule TemporaryFolder \u2192 JUnit 5 @TempDir")
public class RuleTemporayFolderJUnitPlugin extends TriggerPatternCleanupPlugin {
@Override
protected JunitHolder createHolder(Match match) {
FieldDeclaration fieldDecl = (FieldDeclaration) match.getMatchedNode();
VariableDeclarationFragment fragment = (VariableDeclarationFragment) fieldDecl.fragments().get(0);
if (fragment.resolveBinding() == null) {
return null;
}
ITypeBinding binding = fragment.resolveBinding().getType();
if (binding == null || !ORG_JUNIT_RULES_TEMPORARY_FOLDER.equals(binding.getQualifiedName())) {
return null;
}
JunitHolder holder = new JunitHolder();
holder.setMinv(fieldDecl);
return holder;
}
@Override
protected void process2Rewrite(TextEditGroup group, ASTRewrite rewriter, AST ast, ImportRewrite importRewriter,
JunitHolder junitHolder) {
FieldDeclaration field = junitHolder.getFieldDeclaration();
rewriter.remove(field, group);
TypeDeclaration parentClass = ASTNodes.getParent(field, TypeDeclaration.class);
VariableDeclarationFragment originalFragment = (VariableDeclarationFragment) field.fragments().get(0);
String originalName = originalFragment.getName().getIdentifier();
// Add JUnit 5 imports and remove JUnit 4 imports
importRewriter.addImport(ORG_JUNIT_JUPITER_API_IO_TEMP_DIR);
importRewriter.addImport("java.nio.file.Path");
importRewriter.removeImport(ORG_JUNIT_RULE);
importRewriter.removeImport(ORG_JUNIT_RULES_TEMPORARY_FOLDER);
// Create new field: @TempDir Path fieldName;
VariableDeclarationFragment tempDirFragment = ast.newVariableDeclarationFragment();
tempDirFragment.setName(ast.newSimpleName(originalName));
FieldDeclaration tempDirField = ast.newFieldDeclaration(tempDirFragment);
tempDirField.setType(ast.newSimpleType(ast.newName("Path")));
MarkerAnnotation tempDirAnnotation = AnnotationUtils.createMarkerAnnotation(ast, "TempDir");
rewriter.getListRewrite(tempDirField, FieldDeclaration.MODIFIERS2_PROPERTY).insertFirst(tempDirAnnotation,
group);
rewriter.getListRewrite(parentClass, TypeDeclaration.BODY_DECLARATIONS_PROPERTY).insertFirst(tempDirField,
group);
// Transform method invocations in a single pass (also determines if Files
// import is needed)
final boolean[] needsFilesImport = { false };
for (MethodDeclaration method : parentClass.getMethods()) {
ReferenceHolder<String, Object> visitHolder = ReferenceHolder.create();
AstProcessorBuilder.with(visitHolder)
.onMethodInvocation((node, h) -> {
if (node.getExpression() == null) {
return true;
}
String expressionName = node.getExpression().toString();
if (!originalName.equals(expressionName)) {
return true;
}
String methodName = node.getName().getIdentifier();
// Handle newFile() and newFile(String)
if ("newFile".equals(methodName)) {
needsFilesImport[0] = true;
if (node.arguments().isEmpty()) {
// newFile() with no args -> Files.createTempFile(tempDir, "", null).toFile()
MethodInvocation createTempFileInvocation = ast.newMethodInvocation();
createTempFileInvocation.setExpression(ast.newName("Files"));
createTempFileInvocation.setName(ast.newSimpleName("createTempFile"));
createTempFileInvocation.arguments().add(ast.newSimpleName(originalName));
createTempFileInvocation.arguments().add(ast.newStringLiteral());
createTempFileInvocation.arguments().add(ast.newNullLiteral());
MethodInvocation toFileInvocation = ast.newMethodInvocation();
toFileInvocation.setExpression(createTempFileInvocation);
toFileInvocation.setName(ast.newSimpleName("toFile"));
rewriter.replace(node, toFileInvocation, group);
} else {
// newFile(String) -> Files.createFile(tempDir.resolve(...)).toFile()
MethodInvocation createFileInvocation = ast.newMethodInvocation();
createFileInvocation.setExpression(ast.newName("Files"));
createFileInvocation.setName(ast.newSimpleName("createFile"));
MethodInvocation resolveInvocation = ast.newMethodInvocation();
resolveInvocation.setExpression(ast.newSimpleName(originalName));
resolveInvocation.setName(ast.newSimpleName("resolve"));
// newFile only takes a single String argument
// Copy and transform TestName.getMethodName() calls in the argument
ASTNode originalArg = (ASTNode) node.arguments().get(0);
resolveInvocation.arguments()
.add(TestNameRefactorer.copyAndTransformTestNameReferences(originalArg, ast));
createFileInvocation.arguments().add(resolveInvocation);
MethodInvocation toFileInvocation = ast.newMethodInvocation();
toFileInvocation.setExpression(createFileInvocation);
toFileInvocation.setName(ast.newSimpleName("toFile"));
rewriter.replace(node, toFileInvocation, group);
}
}
// Handle newFolder() and newFolder(String...)
else if ("newFolder".equals(methodName)) {
needsFilesImport[0] = true;
if (node.arguments().isEmpty()) {
// newFolder() with no args -> Files.createTempDirectory(tempDir, "").toFile()
MethodInvocation createTempDirInvocation = ast.newMethodInvocation();
createTempDirInvocation.setExpression(ast.newName("Files"));
createTempDirInvocation.setName(ast.newSimpleName("createTempDirectory"));
createTempDirInvocation.arguments().add(ast.newSimpleName(originalName));
createTempDirInvocation.arguments().add(ast.newStringLiteral());
MethodInvocation toFileInvocation = ast.newMethodInvocation();
toFileInvocation.setExpression(createTempDirInvocation);
toFileInvocation.setName(ast.newSimpleName("toFile"));
rewriter.replace(node, toFileInvocation, group);
} else {
// newFolder(String...) ->
// Files.createDirectories(tempDir.resolve(...).resolve(...)).toFile()
// For multiple arguments, chain resolve() calls
MethodInvocation createDirInvocation = ast.newMethodInvocation();
createDirInvocation.setExpression(ast.newName("Files"));
createDirInvocation.setName(ast.newSimpleName("createDirectories"));
// Build chained resolve() calls for multiple arguments
// Start with tempFolder.resolve("a")
ASTNode firstArg = (ASTNode) node.arguments().get(0);
MethodInvocation chainedResolve = ast.newMethodInvocation();
chainedResolve.setExpression(ast.newSimpleName(originalName));
chainedResolve.setName(ast.newSimpleName("resolve"));
chainedResolve.arguments()
.add(TestNameRefactorer.copyAndTransformTestNameReferences(firstArg, ast));
// Chain additional resolve() calls for subsequent arguments
// .resolve("b").resolve("c")...
for (int i = 1; i < node.arguments().size(); i++) {
ASTNode arg = (ASTNode) node.arguments().get(i);
MethodInvocation nextResolve = ast.newMethodInvocation();
nextResolve.setExpression(chainedResolve);
nextResolve.setName(ast.newSimpleName("resolve"));
nextResolve.arguments()
.add(TestNameRefactorer.copyAndTransformTestNameReferences(arg, ast));
chainedResolve = nextResolve;
}
createDirInvocation.arguments().add(chainedResolve);
MethodInvocation toFileInvocation = ast.newMethodInvocation();
toFileInvocation.setExpression(createDirInvocation);
toFileInvocation.setName(ast.newSimpleName("toFile"));
rewriter.replace(node, toFileInvocation, group);
}
}
// Handle getRoot()
else if ("getRoot".equals(methodName)) {
// tempDir.toFile()
MethodInvocation toFileInvocation = ast.newMethodInvocation();
toFileInvocation.setExpression(ast.newSimpleName(originalName));
toFileInvocation.setName(ast.newSimpleName("toFile"));
rewriter.replace(node, toFileInvocation, group);
}
return true;
})
.build(method);
}
// Add Files import only if needed (determined during transformation)
if (needsFilesImport[0]) {
importRewriter.addImport("java.nio.file.Files");
}
}
@Override
public String getPreview(boolean afterRefactoring) {
if (afterRefactoring) {
return """
@TempDir
Path tempFolder;
@Test
public void test3() throws IOException{
File newFile = Files.createFile(tempFolder.resolve("myfile.txt")).toFile();
}
"""; //$NON-NLS-1$
}
return """
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Test
public void test3() throws IOException{
File newFile = tempFolder.newFile("myfile.txt");
}
"""; //$NON-NLS-1$
}
@Override
public String toString() {
return "RuleTemporaryFolder"; //$NON-NLS-1$
}
}