Skip to content

Commit 65053ab

Browse files
authored
don't use TempDirNonFinal on method parameters (#484)
Fixes #483.
1 parent 34937d4 commit 65053ab

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/main/java/org/openrewrite/java/testing/junit5/TempDirNonFinal.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.testing.junit5;
1717

18+
import org.openrewrite.Cursor;
1819
import org.openrewrite.ExecutionContext;
1920
import org.openrewrite.Preconditions;
2021
import org.openrewrite.Recipe;
@@ -27,6 +28,7 @@
2728
import org.openrewrite.java.tree.J.Modifier.Type;
2829

2930
import java.time.Duration;
31+
import java.util.Iterator;
3032

3133
public class TempDirNonFinal extends Recipe {
3234

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

67+
// copied from org.openrewrite.java.search.FindFieldsOfType.isField(Cursor), should probably become part of the API
68+
private static boolean isField(Cursor cursor) {
69+
Iterator<Object> path = cursor.getPath();
70+
while (path.hasNext()) {
71+
Object o = path.next();
72+
if (o instanceof J.MethodDeclaration) {
73+
return false;
74+
}
75+
if (o instanceof J.ClassDeclaration) {
76+
return true;
77+
}
78+
}
79+
return true;
80+
}
81+
6582
@Override
6683
public Duration getEstimatedEffortPerOccurrence() {
6784
return Duration.ofMinutes(1);

src/test/java/org/openrewrite/java/testing/junit5/TempDirNonFinalTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class MyTest {
9595
}
9696

9797
@Test
98-
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/241")
98+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/241, https://github.com/openrewrite/rewrite-testing-frameworks/issues/483")
9999
void tempDirFileParameter() {
100100
//language=java
101101
rewriteRun(
@@ -109,7 +109,7 @@ void tempDirFileParameter() {
109109
110110
class MyTest {
111111
@Test
112-
void fileTest(@TempDir File tempDir) {
112+
void fileTest(@TempDir final File tempDir) {
113113
}
114114
}
115115
"""

0 commit comments

Comments
 (0)