Skip to content

Commit

Permalink
Add handling of static and instance initializer blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
MBoegers committed May 24, 2023
1 parent 8926e0c commit 208fc71
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 27 deletions.
30 changes: 28 additions & 2 deletions src/main/java/org/openrewrite/java/migrate/lang/UseVarKeyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,23 @@ protected TreeVisitor<?, ExecutionContext> getVisitor() {
private final JavaTemplate template = JavaTemplate.builder(this::getCursor, "var #{} = #{any()}")
.javaParser(JavaParser.fromJavaVersion()).build();


@Override
public J visitForEachControl(J.ForEachLoop.Control control, ExecutionContext executionContext) {
// für enhanced for-Loops wäre hier der korrekter punkt.
// es gelten dieselben Dinge für primitives, null und Generics
// ob wir am richtigen Ort sind muss nicht geprüft werden; ebenso single und pure
return super.visitForEachControl(control, executionContext);
}

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext executionContext) {
J.VariableDeclarations vd = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, executionContext);

boolean isOutsideMethod = !determineIfIsInsideMethod(this.getCursor());
boolean isMethodParameter = determineIfMethodParameter(vd, this.getCursor());
if (isOutsideMethod || isMethodParameter) return vd;
boolean isOutsideInitializer = !determineIfOutsideInitializer(this.getCursor(), false);
if ((isOutsideMethod && isOutsideInitializer) || isMethodParameter) return vd;

TypeTree typeExpression = vd.getTypeExpression();
boolean isByteVariable = typeExpression instanceof J.Primitive && BYTE_TYPE.equals(typeExpression.getType());
Expand All @@ -90,12 +100,28 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
boolean isNullAssigment = initializer instanceof J.Literal && isNull(((J.Literal) initializer).getValue());
boolean alreadyUseVar = typeExpression instanceof J.Identifier && "var".equals(((J.Identifier) typeExpression).getSimpleName());
boolean isGeneric = typeExpression instanceof J.ParameterizedType; // todo implement generics!
if (alreadyUseVar || isDeclarationOnly || isNullAssigment || isGeneric) return vd;
boolean useTernary = initializer instanceof J.Ternary;
if (alreadyUseVar || isDeclarationOnly || isNullAssigment || isGeneric || useTernary) return vd;

J.VariableDeclarations result = transformToVar(vd);
return result;
}

private boolean determineIfOutsideInitializer(Cursor cursor, boolean childWasBlock) {
Object currentStatement = cursor.getValue();

boolean isClassDeclaration = currentStatement instanceof J.ClassDeclaration;
boolean classFollowedByBlock = childWasBlock && isClassDeclaration;
if (classFollowedByBlock) return true;

Cursor parentStatement = cursor.getParent();
boolean cannotClimbUpFurther = isNull(parentStatement);
if (cannotClimbUpFurther) return false;

boolean isBlock = currentStatement instanceof J.Block;
return determineIfOutsideInitializer(parentStatement, isBlock);
}

private boolean determineIfMethodParameter(@NotNull J.VariableDeclarations vd, @NotNull Cursor cursor) {
J.MethodDeclaration methodDeclaration = cursor.firstEnclosing(J.MethodDeclaration.class);
return nonNull(methodDeclaration) && methodDeclaration.getParameters().contains(vd);
Expand Down
136 changes: 111 additions & 25 deletions src/test/java/org/openrewrite/java/migrate/lang/UseVarKeywordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,117 @@ void m() {

@Nested
class Objects {
@Test
void inMethodBody() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;
class A {
void m() {
Object o = new Object();
}
}
""", """
package com.example.app;
class A {
void m() {
var o = new Object();
}
}
"""),
10
)
);

@Nested
class Applicable {
@Test
void inMethodBody() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;
class A {
void m() {
Object o = new Object();
}
}
""", """
package com.example.app;
class A {
void m() {
var o = new Object();
}
}
"""),
10
)
);
}

@Test
@Disabled("Not yet implemented")
void withTernary() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;
class A {
void m() {
String o = true ? "isTrue" : "Test";
}
}
""", """
package com.example.app;
class A {
void m() {
var o = true ? "isTrue" : "Test";
}
}
"""),
10
)
);
}

@Test
void inStaticInitializer() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;
class A {
static {
Object o = new Object();
}
}
""", """
package com.example.app;
class A {
static {
var o = new Object();
}
}
"""),
10
)
);
}

@Test
void inInstanceInitializer() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;
class A {
{
Object o = new Object();
}
}
""", """
package com.example.app;
class A {
{
var o = new Object();
}
}
"""),
10
)
);
}
}

@Nested
Expand Down

0 comments on commit 208fc71

Please sign in to comment.