|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://docs.moderne.io/licensing/moderne-source-available-license |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.migrate.lang; |
| 17 | + |
| 18 | +import lombok.EqualsAndHashCode; |
| 19 | +import lombok.Value; |
| 20 | +import org.jspecify.annotations.Nullable; |
| 21 | +import org.openrewrite.ExecutionContext; |
| 22 | +import org.openrewrite.Preconditions; |
| 23 | +import org.openrewrite.Recipe; |
| 24 | +import org.openrewrite.TreeVisitor; |
| 25 | +import org.openrewrite.java.JavaIsoVisitor; |
| 26 | +import org.openrewrite.java.JavaVisitor; |
| 27 | +import org.openrewrite.java.tree.Expression; |
| 28 | +import org.openrewrite.java.tree.J; |
| 29 | +import org.openrewrite.java.tree.JavaType; |
| 30 | +import org.openrewrite.java.tree.TypeUtils; |
| 31 | +import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker; |
| 32 | + |
| 33 | +import static java.util.Collections.singletonList; |
| 34 | + |
| 35 | +@Value |
| 36 | +@EqualsAndHashCode(callSuper = false) |
| 37 | +public class SwitchCaseEnumGuardToLabel extends Recipe { |
| 38 | + @Override |
| 39 | + public String getDisplayName() { |
| 40 | + return "Use switch cases labels for enums"; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String getDescription() { |
| 45 | + return "Use switch case labels when a guard is checking equality with an enum."; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 50 | + return Preconditions.check(Preconditions.not(new KotlinFileChecker<>()), new JavaIsoVisitor<ExecutionContext>() { |
| 51 | + |
| 52 | + @Override |
| 53 | + public J.Case visitCase(J.Case case_, ExecutionContext ctx) { |
| 54 | + J.Case visitedCase = super.visitCase(case_, ctx); |
| 55 | + |
| 56 | + J.VariableDeclarations.NamedVariable label = getCreatedLabelVariable(visitedCase); |
| 57 | + if (label == null) { |
| 58 | + return visitedCase; |
| 59 | + } |
| 60 | + |
| 61 | + JavaType type = label.getType(); |
| 62 | + if (type instanceof JavaType.Class && ((JavaType.Class) type).getKind() == JavaType.FullyQualified.Kind.Enum) { |
| 63 | + Expression guardedEnum = getGuardedEnum(visitedCase, label); |
| 64 | + if (guardedEnum != null) { |
| 65 | + J modifiedBody = enumReferencesToEnumValue(label.getSimpleName(), guardedEnum) |
| 66 | + .visit(visitedCase.getBody(), ctx); |
| 67 | + return visitedCase.withGuard(null) |
| 68 | + .withCaseLabels(singletonList(guardedEnum.withPrefix(visitedCase.getCaseLabels().get(0).getPrefix()))) |
| 69 | + .withBody(modifiedBody); |
| 70 | + } |
| 71 | + } |
| 72 | + return visitedCase; |
| 73 | + } |
| 74 | + |
| 75 | + private J.VariableDeclarations.@Nullable NamedVariable getCreatedLabelVariable(J.Case case_) { |
| 76 | + if (case_.getCaseLabels().size() != 1 || !(case_.getCaseLabels().get(0) instanceof J.VariableDeclarations)) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + J.VariableDeclarations decl = (J.VariableDeclarations) case_.getCaseLabels().get(0); |
| 80 | + if (decl.getVariables().size() != 1) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + return decl.getVariables().get(0); |
| 84 | + } |
| 85 | + |
| 86 | + private @Nullable Expression getGuardedEnum(J.Case case_, J.VariableDeclarations.NamedVariable label) { |
| 87 | + Expression guard = case_.getGuard(); |
| 88 | + if (guard == null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + Expression select = null; |
| 92 | + Expression equalTo = null; |
| 93 | + if (guard instanceof J.MethodInvocation) { |
| 94 | + J.MethodInvocation methodGuard = (J.MethodInvocation) guard; |
| 95 | + if ("equals".equals(methodGuard.getSimpleName()) && methodGuard.getArguments().size() == 1) { |
| 96 | + select = methodGuard.getSelect(); |
| 97 | + equalTo = methodGuard.getArguments().get(0); |
| 98 | + } |
| 99 | + } else if (guard instanceof J.Binary) { |
| 100 | + J.Binary binaryGuard = (J.Binary) guard; |
| 101 | + if (J.Binary.Type.Equal == binaryGuard.getOperator()) { |
| 102 | + select = binaryGuard.getLeft(); |
| 103 | + equalTo = binaryGuard.getRight(); |
| 104 | + } |
| 105 | + } |
| 106 | + if ((select instanceof J.FieldAccess || select instanceof J.Identifier) && equalTo instanceof J.Identifier && label.getName().getSimpleName().equals(((J.Identifier) equalTo).getSimpleName())) { |
| 107 | + return select; |
| 108 | + } else if ((equalTo instanceof J.FieldAccess || equalTo instanceof J.Identifier) && select instanceof J.Identifier && label.getName().getSimpleName().equals(((J.Identifier) select).getSimpleName())) { |
| 109 | + return equalTo; |
| 110 | + } |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + private JavaVisitor<ExecutionContext> enumReferencesToEnumValue(String name, Expression enumReference) { |
| 115 | + return new JavaVisitor<ExecutionContext>() { |
| 116 | + @Override |
| 117 | + public J visitIdentifier(J.Identifier ident, ExecutionContext ctx) { |
| 118 | + J.Identifier identifier = (J.Identifier) super.visitIdentifier(ident, ctx); |
| 119 | + if (identifier.getSimpleName().equals(name) && TypeUtils.isOfType(identifier.getType(), enumReference.getType())) { |
| 120 | + return enumReference.withPrefix(identifier.getPrefix()); |
| 121 | + } |
| 122 | + return identifier; |
| 123 | + } |
| 124 | + }; |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | +} |
0 commit comments