Skip to content

Commit

Permalink
UseCollectionInterfaces should not modify Groovy
Browse files Browse the repository at this point in the history
While this recipe might also be interesting for Groovy, it currently doesn't work properly yet and is thus being completely disabled for Groovy.

Fixes: openrewrite/rewrite#3777
  • Loading branch information
knutwannheden committed Dec 8, 2023
1 parent de9632f commit e179d01
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
*/
package org.openrewrite.staticanalysis;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.*;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
Expand Down Expand Up @@ -92,6 +90,12 @@ public Duration getEstimatedEffortPerOccurrence() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
// TODO: proper Groovy support requires some extra work
return super.isAcceptable(sourceFile, ctx) && !(sourceFile instanceof G.CompilationUnit);
}

@Override
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
Expand All @@ -107,8 +111,8 @@ public J visit(@Nullable Tree tree, ExecutionContext ctx) {
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, executionContext);
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
if ((m.hasModifier(J.Modifier.Type.Public) || m.hasModifier(J.Modifier.Type.Private) || m.getModifiers().isEmpty())
&& m.getReturnTypeExpression() != null) {
JavaType.FullyQualified originalType = TypeUtils.asFullyQualified(m.getReturnTypeExpression().getType());
Expand Down Expand Up @@ -147,8 +151,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
}

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext executionContext) {
J.VariableDeclarations mv = super.visitVariableDeclarations(multiVariable, executionContext);
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
J.VariableDeclarations mv = super.visitVariableDeclarations(multiVariable, ctx);
JavaType.FullyQualified originalType = TypeUtils.asFullyQualified(mv.getType());
if ((mv.hasModifier(J.Modifier.Type.Public) || mv.hasModifier(J.Modifier.Type.Private) || mv.getModifiers().isEmpty()) &&
originalType != null && rspecRulesReplaceTypeMap.containsKey(originalType.getFullyQualifiedName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.groovy.Assertions.groovy;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.javaVersion;

Expand Down Expand Up @@ -985,4 +985,22 @@ public void method() {
);
}
}

@Test
void groovyDefVariable() {
rewriteRun(
groovy(
//language=groovy
"""
library('other-library')
def myMap = [
myEntry: [[ key: value ]]
]
runPipeline(myMap: myMap)
"""
)
);
}
}

0 comments on commit e179d01

Please sign in to comment.