Skip to content

com.google.common.base.Predicate.apply calls are not systematically replaced by java.util.function.Predicate.test method #886

@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, but the apply method calls are not systematically 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>(){}).
    The use case to reproduce needs several classes in different modules.

I think that the problem is more in ChangeMethodName recipe or in MethodMatcher class. But I'm tracing the problem I initially faced.

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?

package current.module.package;

import com.google.common.base.Predicate;
import current.module.another.package.Test3;
import different.module.package.Test4;

class Test {
    public static void test() {
        new Test().isDetached(new Object());
    }

    private boolean isDetached(Object object) {
        boolean detached = false;
        if (Test2.A_PREDICATE_DECLARE_IN_A_CLASS_IN_SAME_PACKAGE.apply(object)) {
            detached = true;
        }
        if (Test3.A_PREDICATE_DECLARE_IN_A_CLASS_IN_ANOTHER_PACKAGE.apply(object)) {
            detached = true;
        }
        if (Test4.A_PREDICATE_DECLARE_IN_A_CLASS_IN_ANOTHER_MODULE.apply(object)) {
            detached = true;
        }
        return detached;
    }
}
package current.module.package;

import com.google.common.base.Predicate;

public class Test2 {
    public static final Predicate<Object> A_PREDICATE_DECLARE_IN_A_CLASS_IN_SAME_PACKAGE = new Predicate<Object>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean apply(Object input) {
            return false;
        }

    };
}
package current.module.another.package;

import com.google.common.base.Predicate;

public class Test3 {
    public static final Predicate<Object> A_PREDICATE_DECLARE_IN_A_CLASS_IN_ANOTHER_PACKAGE = new Predicate<Object>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean apply(Object input) {
            return false;
        }

    };
}
package different.module.package;

import com.google.common.base.Predicate;

public class Test4 {
    public static final Predicate<Object> A_PREDICATE_DECLARE_IN_A_CLASS_IN_ANOTHER_MODULE = new Predicate<Object>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean apply(Object input) {
            return false;
        }
    };
}

What did you expect to see?

import current.module.another.package.Test3;
import different.module.package.Test4;

class Test {
    public static void test() {
        new Test().isDetached(new Object());
    }

    private boolean isDetached(Object object) {
        boolean detached = false;
        if (Test2.A_PREDICATE_DECLARE_IN_A_CLASS_IN_SAME_PACKAGE.test(object)) {
            detached = true;
        }
        if (Test3.A_PREDICATE_DECLARE_IN_A_CLASS_IN_ANOTHER_PACKAGE.test(object)) {
            detached = true;
        }
        if (Test4.A_PREDICATE_DECLARE_IN_A_CLASS_IN_ANOTHER_MODULE.test(object)) {
            detached = true;
        }
        return detached;
    }
}

What did you see instead?

The apply method in class Test4 was not renamed as expected.

import current.module.another.package.Test3;
import different.module.package.Test4;

class Test {
    public static void test() {
        new Test().isDetached(new Object());
    }

    private boolean isDetached(Object object) {
        boolean detached = false;
        if (Test2.A_PREDICATE_DECLARE_IN_A_CLASS_IN_SAME_PACKAGE.test(object)) {
            detached = true;
        }
        if (Test3.A_PREDICATE_DECLARE_IN_A_CLASS_IN_ANOTHER_PACKAGE.test(object)) {
            detached = true;
        }
        if (Test4.A_PREDICATE_DECLARE_IN_A_CLASS_IN_ANOTHER_MODULE.apply(object)) {
            detached = true;
        }
        return detached;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingguavaquestionFurther information is requested

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions