Skip to content
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 @@ -26,6 +26,7 @@
import org.openrewrite.internal.RecipeRunException;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Marker;

Expand Down Expand Up @@ -184,6 +185,7 @@ public J visitSwitch(J.Switch switch_, ExecutionContext ctx) {
} else {
generatedIf = ifElseIfEnum.apply(getCursor(), switch_.getCoordinates().replace(), tree, enumIdentToFieldAccessString(cases[0].getPattern()), tree, enumIdentToFieldAccessString(cases[1].getPattern()));
}
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
} else {
if (cases[1] == null) {
if (isDefault(cases[0])) {
Expand Down Expand Up @@ -250,7 +252,7 @@ private boolean switchesOnEnum(J.Switch switch_) {
}

private String enumIdentToFieldAccessString(Expression casePattern) {
String caseType = requireNonNull(TypeUtils.asFullyQualified(casePattern.getType())).getClassName();
String caseType = requireNonNull(TypeUtils.asFullyQualified(casePattern.getType())).getFullyQualifiedName();
if (casePattern instanceof J.FieldAccess) {
// may be a field access in Groovy
return caseType + "." + ((J.FieldAccess) casePattern).getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,52 @@ void someMethod() {
);
}

@Test
void importsOnEnumImplied() {
//noinspection EnhancedSwitchMigration
rewriteRun(
//language=java
java(
"""
import java.time.LocalDate;

class Test {
void test(LocalDate date) {
switch(date.getDayOfWeek()) {
case MONDAY:
someMethod();
break;
default:
someMethod();
break;
}
}

void someMethod() {
}
}
""",
"""
import java.time.DayOfWeek;
import java.time.LocalDate;

class Test {
void test(LocalDate date) {
if (date.getDayOfWeek() == DayOfWeek.MONDAY) {
someMethod();
} else {
someMethod();
}
}

void someMethod() {
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1701")
@Test
void removeBreaksFromCaseBody() {
Expand Down