Skip to content

Conversion of Predicates.equalTo may cause issues - the generic type is not always correctly inferred #903

@lredor

Description

@lredor

What version of OpenRewrite are you using?

I am using:

  • Rewrite Maven Plugin v6.23.0
  • Maven v3.9.5
  • org.openrewrite.recipe:rewrite-migrate-java v3.21.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:6.23.0-SNAPSHOT:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-migrate-java:3.21.0-SNAPSHOT -Drewrite.activeRecipes=org.openrewrite.java.migrate.guava.NoGuava -Drewrite.exportDatatables=true -Dmaven.repo.local=./.repositoryMaven -f packaging/org.eclipse.sirius.parent/pom.xml

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

import com.google.common.base.Predicates;
import com.google.common.base.Predicate;

import java.util.Collection;

class Test {
    public static void test(Collection<String> aCollection, Predicate<Collection<String>> anotherPredicate) {
        Predicate<Collection<String>> combined = Predicates.and(Predicates.equalTo(aCollection), anotherPredicate);
    }
}

What did you expect to see?

  • Since PR Convert Guava Predicates.equalTo and .not #887, the result produces the following compilation error: 'and(java.util.function.Predicate<? super java.lang.Object>)' in 'java.util.function.Predicate' cannot be applied to '(java.util.function.Predicate<java.util.Collection<java.lang.String>>)'.
    It seems that the isEqual expression must be stored in a variable to compile correctly. I'm not sure that extracting the variable is the most elegant solution, but it does fix the issue.
import java.util.function.Predicate;

import java.util.Collection;

class Test {
    public static void testResult(Collection<String> aCollection, Predicate<Collection<String>> anotherPredicate) {
        Predicate<Collection<String>> isEqualPredicate = Predicate.isEqual(aCollection);
        Predicate<Collection<String>> combined = isEqualPredicate.and(anotherPredicate);
    }
}

What did you see instead?

import java.util.function.Predicate;

import java.util.Collection;

class Test {
    public static void testResult(Collection<String> aCollection, Predicate<Collection<String>> anotherPredicate) {
        Predicate<Collection<String>> combined = Predicate.isEqual(aCollection).and(anotherPredicate);
    }
}

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