Skip to content

Predicates is not correctly handed by PreferJavaUtilPredicate recipe #884

@lredor

Description

@lredor

Predicates is not correctly handed by PreferJavaUtilPredicate recipe

  • 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 Predicates.and method that is not converted. As a result, the following error appears after migration: The method and(Predicate<? super T>, Predicate<? super T>) in the type Predicates is not applicable for the arguments (new Predicate<Object>(){}, new Predicate<Object>(){}).
    I reproduced the issue with the simple class below.

The problem/solution would be the same for methods or, equalTo and not.

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 com.google.common.base.Predicate;
import com.google.common.base.Predicates;

class Test {
    public static void test() {
        Predicate<Object> result = Predicates.and(new Predicate<Object>() {
            @Override
            public boolean apply(Object input) {
                return false;
            }
        }, new Predicate<Object>() {
            @Override
            public boolean apply(Object input) {
                return false;
            }
        });
    }
}

What did you expect to see?

The method Predicates.and can be replaced by java.util.function.Predicate.and(Predicate<? super T>).

import java.util.function.Predicate;

class Test {
    public static void test() {
        Predicate<Object> result = new Predicate<Object>() {
            @Override
            public boolean test(Object input) {
                return false;
            }
        }.and(new Predicate<Object>() {
            @Override
            public boolean test(Object input) {
                return false;
            }
        });
    }
}

What did you see instead?

import java.util.function.Predicate;

import com.google.common.base.Predicates;

class Test {
    public static void test() {
        Predicate<Object> result = Predicates.and(new Predicate<Object>() {
            @Override
            public boolean test(Object input) {
                return false;
            }
        }, new Predicate<Object>() {
            @Override
            public boolean test(Object input) {
                return false;
            }
        });
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingrecipeRecipe requested

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions