Skip to content

Commit

Permalink
Update UseDiamondOperator to ignore NewClass initializers for variabl…
Browse files Browse the repository at this point in the history
…es having a null or unknown type. (issue #1297)
  • Loading branch information
pway99 committed May 27, 2022
1 parent edee0d4 commit 3592bfc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaVarKeyword;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

import java.time.Duration;
Expand Down Expand Up @@ -60,6 +57,14 @@ protected TreeVisitor<?, ExecutionContext> getVisitor() {

return new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext executionContext) {
if (multiVariable.getType() == null || multiVariable.getType() instanceof JavaType.Unknown) {
return multiVariable;
}
return super.visitVariableDeclarations(multiVariable, executionContext);
}

@Override
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext executionContext) {
J.NewClass n = super.visitNewClass(newClass, executionContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.java.cleanup

import org.junit.jupiter.api.Test
import org.openrewrite.Issue
import org.openrewrite.Recipe
import org.openrewrite.java.JavaParser
import org.openrewrite.java.JavaRecipeTest
Expand Down Expand Up @@ -54,6 +55,23 @@ interface UseDiamondOperatorTest: JavaRecipeTest {
"""
)

@Issue("https://github.com/openrewrite/rewrite/issues/1297")
@Test
fun `do not use diamond operators for variables having null or unknown types`(jp: JavaParser) = assertUnchanged(
jp,
before = """
import lombok.val;
import java.util.ArrayList;
class Test<X, Y> {
void test() {
val ls = new ArrayList<String>();
UnknownThing o = new UnknownThing<String>();
}
}
"""
)

@Test
fun noLeftSide(jp: JavaParser) = assertUnchanged(
parser = jp,
Expand Down

0 comments on commit 3592bfc

Please sign in to comment.