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
@@ -0,0 +1,65 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.util;

import lombok.Getter;
import org.openrewrite.*;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.JavaType.ShallowClass;
import org.openrewrite.java.tree.Space;

import static java.util.Collections.emptyList;

public class MigrateCollectionsEmptyList extends Recipe {
private static final MethodMatcher EMPTY_LIST = new MethodMatcher("java.util.Collections emptyList()");

@Getter
final String displayName = "Prefer `List.of()`";

@Getter
final String description = "Prefer `List.of()` instead of using `Collections.emptyList()` in Java 9 or higher.";

@Override
public JavaIsoVisitor<ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

if (EMPTY_LIST.matches(m)) {
maybeRemoveImport("java.util.Collections");
maybeAddImport("java.util.List");

JavaType.Class classType = ShallowClass.build("java.util.List");
JavaType.Method methodType = m.getMethodType().withName("of").withDeclaringType(classType);
m = m.withName(m.getName().withSimpleName("of").withType(methodType));
if (m.getSelect() instanceof J.Identifier) {
return m.withSelect(((J.Identifier) m.getSelect()).withSimpleName("List").withType(classType));
}
return m.withSelect(new J.Identifier(
Tree.randomId(), m.getPrefix(), m.getMarkers(), emptyList(), "List", classType, null))
.withPrefix(Space.EMPTY);
}

return m;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.util;

import lombok.Getter;
import org.openrewrite.*;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.JavaType.ShallowClass;
import org.openrewrite.java.tree.Space;

import static java.util.Collections.emptyList;

public class MigrateCollectionsEmptyMap extends Recipe {
private static final MethodMatcher EMPTY_MAP = new MethodMatcher("java.util.Collections emptyMap()");

@Getter
final String displayName = "Prefer `Map.of()`";

@Getter
final String description = "Prefer `Map.of()` instead of using `Collections.emptyMap()` in Java 9 or higher.";

@Override
public JavaIsoVisitor<ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

if (EMPTY_MAP.matches(m)) {
maybeRemoveImport("java.util.Collections");
maybeAddImport("java.util.Map");

JavaType.Class classType = ShallowClass.build("java.util.Map");
JavaType.Method methodType = m.getMethodType().withName("of").withDeclaringType(classType);
m = m.withName(m.getName().withSimpleName("of").withType(methodType));
if (m.getSelect() instanceof J.Identifier) {
return m.withSelect(((J.Identifier) m.getSelect()).withSimpleName("Map").withType(classType));
}
return m.withSelect(new J.Identifier(
Tree.randomId(), m.getPrefix(), m.getMarkers(), emptyList(), "Map", classType, null))
.withPrefix(Space.EMPTY);
}

return m;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.util;

import lombok.Getter;
import org.openrewrite.*;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.JavaType.ShallowClass;
import org.openrewrite.java.tree.Space;

import static java.util.Collections.emptyList;

public class MigrateCollectionsEmptySet extends Recipe {
private static final MethodMatcher EMPTY_SET = new MethodMatcher("java.util.Collections emptySet()");

@Getter
final String displayName = "Prefer `Set.of()`";

@Getter
final String description = "Prefer `Set.of()` instead of using `Collections.emptySet()` in Java 9 or higher.";

@Override
public JavaIsoVisitor<ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

if (EMPTY_SET.matches(m)) {
maybeRemoveImport("java.util.Collections");
maybeAddImport("java.util.Set");

JavaType.Class classType = ShallowClass.build("java.util.Set");
JavaType.Method methodType = m.getMethodType().withName("of").withDeclaringType(classType);
m = m.withName(m.getName().withSimpleName("of").withType(methodType));
if (m.getSelect() instanceof J.Identifier) {
return m.withSelect(((J.Identifier) m.getSelect()).withSimpleName("Set").withType(classType));
}
return m.withSelect(new J.Identifier(
Tree.randomId(), m.getPrefix(), m.getMarkers(), emptyList(), "Set", classType, null))
.withPrefix(Space.EMPTY);
}

return m;
}
};
}
}
57 changes: 57 additions & 0 deletions src/main/resources/META-INF/rewrite/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8809,6 +8809,63 @@ examples:
language: java
---
type: specs.openrewrite.org/v1beta/example
recipeName: org.openrewrite.java.migrate.util.MigrateCollectionsEmptyList
examples:
- description: '`MigrateCollectionsEmptyListTest#emptyList`'
sources:
- before: |
import java.util.*;

class Test {
List<String> list = Collections.emptyList();
}
after: |
import java.util.List;

class Test {
List<String> list = List.of();
}
language: java
---
type: specs.openrewrite.org/v1beta/example
recipeName: org.openrewrite.java.migrate.util.MigrateCollectionsEmptySet
examples:
- description: '`MigrateCollectionsEmptySetTest#emptySet`'
sources:
- before: |
import java.util.*;

class Test {
Set<String> set = Collections.emptySet();
}
after: |
import java.util.Set;

class Test {
Set<String> set = Set.of();
}
language: java
---
type: specs.openrewrite.org/v1beta/example
recipeName: org.openrewrite.java.migrate.util.MigrateCollectionsEmptyMap
examples:
- description: '`MigrateCollectionsEmptyMapTest#emptyMap`'
sources:
- before: |
import java.util.*;

class Test {
Map<String, String> map = Collections.emptyMap();
}
after: |
import java.util.Map;

class Test {
Map<String, String> map = Map.of();
}
language: java
---
type: specs.openrewrite.org/v1beta/example
recipeName: org.openrewrite.java.migrate.util.MigrateCollectionsSingletonList
examples:
- description: '`MigrateCollectionsSingletonListTest#singletonList`'
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/rewrite/java-util-apis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ description: Certain java util APIs have been introduced and are favored over pr
preconditions:
- org.openrewrite.Singleton
recipeList:
- org.openrewrite.java.migrate.util.MigrateCollectionsEmptyList
- org.openrewrite.java.migrate.util.MigrateCollectionsEmptyMap
- org.openrewrite.java.migrate.util.MigrateCollectionsEmptySet
- org.openrewrite.java.migrate.util.MigrateCollectionsSingletonList
- org.openrewrite.java.migrate.util.MigrateCollectionsSingletonMap
- org.openrewrite.java.migrate.util.MigrateCollectionsSingletonSet
Expand Down
Loading
Loading