Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't use TempDirNonFinal on method parameters #484

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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