Skip to content

Collections2.filter is not migrated, which creates a conflict with the PreferJavaUtilPredicate recipe #899

@lredor

Description

@lredor
  • This issue is similar to the issue Sets.filter is not migrated, which creates a conflict with the PreferJavaUtilPredicate recipe #897. The Predicate class from Guava is converted in java.util.Predicate even when it is used in a Collections2.filter method that is not converted. As a result, the following error appears after migration: The method filter(Collection<E>, Predicate<? super E>) in the type Collections2 is not applicable for the arguments (Collection<Object>, Predicate<Object>).

What version of OpenRewrite are you using?

I am using

  • Rewrite Maven Plugin v6.22.1
  • Maven v3.9.5
  • org.openrewrite.recipe:rewrite-migrate-java v3.20.0

How are you running OpenRewrite?

I used the Maven command line to launch the recipe on the Sirius Desktop repository :

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-migrate-java:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.migrate.guava.NoGuava -Drewrite.exportDatatables=true -f packaging/org.eclipse.sirius.parent/pom.xml

What is the smallest, simplest way to reproduce the problem?

              import java.util.ArrayList;
              import java.util.Collection;
              import java.util.Objects;

              import com.google.common.base.Predicate;
              import com.google.common.collect.Collections2;

              class Test {
                  public static Collection<Object> test() {
                      Collection<Object> collection = new ArrayList<>();
                      Predicate<Object> isNotNull = Objects::nonNull;
                      return Collections2.filter(collection, isNotNull);
                  }
              }

What did you expect to see?

The method Collections2.filter can be replaced by Collection.stream().filter(Predicate<? super T>).toList().

              import java.util.ArrayList;
              import java.util.Collection;
              import java.util.Objects;
			  import java.util.function.Predicate;

              class Test {
                  public static Collection<Object> test() {
                      Collection<Object> collection = new ArrayList<>();
                      Predicate<Object> isNotNull = Objects::nonNull;
                      return collection.stream().filter(isNotNull).toList();
                  }
              }

What did you see instead?

              import java.util.ArrayList;
              import java.util.Collection;
              import java.util.Objects;

              import com.google.common.collect.Collections2;

              class Test {
                  public static Collection<Object> test() {
                      Collection<Object> collection = new ArrayList<>();
                      Predicate<Object> isNotNull = Objects::nonNull;
                      return Collections2.filter(collection, isNotNull);
                  }
              }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingguava

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions