Skip to content
Open
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 @@ -74,7 +74,9 @@ private static boolean hasDimensionsAfterName(J.VariableDeclarations vd) {
* @return true if single variable definition with initialization and without var
*/
private static boolean isSingleVariableDefinition(J.VariableDeclarations vd) {
TypeTree typeExpression = vd.getTypeExpression();
if (!replaceableTypeExpression(vd.getTypeExpression())) {
return false;
}

boolean definesSingleVariable = vd.getVariables().size() == 1;
boolean isPureAssigment = JavaType.Primitive.Null == vd.getType();
Expand All @@ -90,8 +92,22 @@ private static boolean isSingleVariableDefinition(J.VariableDeclarations vd) {

initializer = initializer.unwrap();
boolean isNullAssigment = J.Literal.isLiteralValue(initializer, null);
boolean alreadyUseVar = typeExpression instanceof J.Identifier && "var".equals(((J.Identifier) typeExpression).getSimpleName());
return !isNullAssigment && !alreadyUseVar;
return !isNullAssigment;
}

/**
* Groovy parses {@code def x = ...} into an empty {@link J.Identifier} and prints {@code def}
* from a marker, so writing {@code var} there splices it onto the name ({@code def varx = ...}).
*/
private static boolean replaceableTypeExpression(@Nullable TypeTree typeExpression) {
if (typeExpression == null) {
return false;
}
if (typeExpression instanceof J.Identifier) {
String simpleName = ((J.Identifier) typeExpression).getSimpleName();
return !simpleName.isEmpty() && !"var".equals(simpleName);
}
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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 @@ -267,4 +268,19 @@ ArrayList<String> getList() {
)
);
}

@Test
void doNotSpliceVarIntoGroovyDef() {
rewriteRun(
//language=groovy
groovy(
"""
def stripVmExtension(File dir) {
def newFile = new File(dir, "child")
assert newFile != null
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;

import static org.openrewrite.groovy.Assertions.groovy;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.javaVersion;

Expand All @@ -33,6 +34,22 @@ public void defaults(RecipeSpec spec) {

@Nested
class NotApplicable {
@Test
void forGroovyDef() {
//language=groovy
rewriteRun(
groovy(
"""
class A {
void m() {
def count = 1
}
}
"""
)
);
}

@Test
void forShort() {
//language=java
Expand Down
Loading