Skip to content

Iterables.filter(Iterable<T> unfiltered, com.google.common.base.Predicat) is not correctly handed by PreferJavaUtilPredicate recipe #883

@lredor

Description

@lredor
  • This issue follows the issue Sets.newHashSet(Iterables.filter... is not correctly handed by NoGuavaSetsNewHashSet recipe #881. The Predicate class from Guava is converted in java.util.Predicate even when it is used in a Iterables.filter method that is not converted.
    As a result, the following error appears after migration: The method filter(Iterable<T>, Predicate<? super T>) in the type Iterables is not applicable for the arguments (List<Exception>, Predicate<Exception>).
    I reproduced the issue with the simple class below.

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v6.21.1
  • Maven v3.9.5
  • org.openrewrite.recipe:rewrite-migrate-java v3.19.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.NoGuavaSetsNewHashSet -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.List;

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

class Test {
    public static void test() {
        List<Exception> myExceptions = new ArrayList<Exception>();
        Predicate<Exception> isTrue = new Predicate<Exception>() {

            @Override
            public boolean apply(Exception input) {
                return false;
            }
        };
        Iterable<Exception> iterable = Iterables.filter(myExceptions, isTrue);
    }
}

What did you expect to see?

As long as Iterables.filter(Iterable<T> unfiltered, com.google.common.base.Predicat) is not migrated, I believe that the original code should be preserved as-is.

What did you see instead?

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import com.google.common.collect.Iterables;

class Test {
    public static void test() {
        List<Exception> myExceptions = new ArrayList<Exception>();
        Predicate<Exception> isTrue = new Predicate<Exception>() {

            @Override
            public boolean test(Exception input) {
                return false;
            }
        };
        Iterable<Exception> iterable = Iterables.filter(myExceptions, isTrue);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingguavarecipeRecipe requested

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions