Skip to content

Commit

Permalink
don't use TempDirNonFinal on method parameters (#484)
Browse files Browse the repository at this point in the history
Fixes #483.
  • Loading branch information
Bananeweizen authored Feb 20, 2024
1 parent 34937d4 commit 65053ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.java.testing.junit5;

import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
Expand All @@ -27,6 +28,7 @@
import org.openrewrite.java.tree.J.Modifier.Type;

import java.time.Duration;
import java.util.Iterator;

public class TempDirNonFinal extends Recipe {

Expand All @@ -53,7 +55,7 @@ private static class TempDirVisitor extends JavaIsoVisitor<ExecutionContext> {
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
J.VariableDeclarations varDecls = super.visitVariableDeclarations(multiVariable, ctx);
if (varDecls.getLeadingAnnotations().stream().anyMatch(TEMPDIR_ANNOTATION_MATCHER::matches)
&& varDecls.hasModifier(Type.Final)) {
&& varDecls.hasModifier(Type.Final) && isField(getCursor())) {
return maybeAutoFormat(varDecls, varDecls.withModifiers(ListUtils
.map(varDecls.getModifiers(), modifier -> modifier.getType() == Type.Final ? null : modifier)),
ctx, getCursor().getParentOrThrow());
Expand All @@ -62,6 +64,21 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
}
}

// copied from org.openrewrite.java.search.FindFieldsOfType.isField(Cursor), should probably become part of the API
private static boolean isField(Cursor cursor) {
Iterator<Object> path = cursor.getPath();
while (path.hasNext()) {
Object o = path.next();
if (o instanceof J.MethodDeclaration) {
return false;
}
if (o instanceof J.ClassDeclaration) {
return true;
}
}
return true;
}

@Override
public Duration getEstimatedEffortPerOccurrence() {
return Duration.ofMinutes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class MyTest {
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/241")
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/241, https://github.com/openrewrite/rewrite-testing-frameworks/issues/483")
void tempDirFileParameter() {
//language=java
rewriteRun(
Expand All @@ -109,7 +109,7 @@ void tempDirFileParameter() {
class MyTest {
@Test
void fileTest(@TempDir File tempDir) {
void fileTest(@TempDir final File tempDir) {
}
}
"""
Expand Down

0 comments on commit 65053ab

Please sign in to comment.