|
| 1 | +/* |
| 2 | + * Copyright 2026 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.util; |
| 17 | + |
| 18 | +import lombok.Getter; |
| 19 | +import org.openrewrite.*; |
| 20 | +import org.openrewrite.java.JavaIsoVisitor; |
| 21 | +import org.openrewrite.java.MethodMatcher; |
| 22 | +import org.openrewrite.java.tree.J; |
| 23 | +import org.openrewrite.java.tree.JavaType; |
| 24 | +import org.openrewrite.java.tree.JavaType.ShallowClass; |
| 25 | +import org.openrewrite.java.tree.Space; |
| 26 | + |
| 27 | +import static java.util.Collections.emptyList; |
| 28 | + |
| 29 | +public class MigrateCollectionsEmptyList extends Recipe { |
| 30 | + private static final MethodMatcher EMPTY_LIST = new MethodMatcher("java.util.Collections emptyList()"); |
| 31 | + |
| 32 | + @Getter |
| 33 | + final String displayName = "Prefer `List.of()`"; |
| 34 | + |
| 35 | + @Getter |
| 36 | + final String description = "Prefer `List.of()` instead of using `Collections.emptyList()` in Java 9 or higher."; |
| 37 | + |
| 38 | + @Override |
| 39 | + public JavaIsoVisitor<ExecutionContext> getVisitor() { |
| 40 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 41 | + |
| 42 | + @Override |
| 43 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
| 44 | + J.MethodInvocation m = super.visitMethodInvocation(method, ctx); |
| 45 | + |
| 46 | + if (EMPTY_LIST.matches(m)) { |
| 47 | + maybeRemoveImport("java.util.Collections"); |
| 48 | + maybeAddImport("java.util.List"); |
| 49 | + |
| 50 | + JavaType.Class classType = ShallowClass.build("java.util.List"); |
| 51 | + JavaType.Method methodType = m.getMethodType().withName("of").withDeclaringType(classType); |
| 52 | + m = m.withName(m.getName().withSimpleName("of").withType(methodType)); |
| 53 | + if (m.getSelect() instanceof J.Identifier) { |
| 54 | + return m.withSelect(((J.Identifier) m.getSelect()).withSimpleName("List").withType(classType)); |
| 55 | + } |
| 56 | + return m.withSelect(new J.Identifier( |
| 57 | + Tree.randomId(), m.getPrefix(), m.getMarkers(), emptyList(), "List", classType, null)) |
| 58 | + .withPrefix(Space.EMPTY); |
| 59 | + } |
| 60 | + |
| 61 | + return m; |
| 62 | + } |
| 63 | + }; |
| 64 | + } |
| 65 | +} |
0 commit comments