Skip to content

Sets.newHashSet(Iterables.filter... is not correctly handed by NoGuavaSetsNewHashSet recipe #881

@lredor

Description

@lredor

After attending a conference at DevFest Nantes 2025, I tried the recipe NoGuava on the Sirius Desktop project. I encountered several errors. I will try to analyze some of them and track the corresponding issues. The first one concerns the NoGuavaSetsNewHashSet recipe. I reproduced it 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.collect.Iterables;
import com.google.common.collect.Sets;

class Test {
    public static void test() {
        final List<ClassCastException> result = new ArrayList<ClassCastException>();
        List<Exception> myExceptions = new ArrayList<Exception>();
        result.addAll(Sets.newHashSet(Iterables.filter(myExceptions, ClassCastException.class)));
    }
}

What did you expect to see?

I'm not sure if there is a straightforward way to replace Sets.newHashSet(Iterables.filter..., but at least, it could be ignored to preserve the original code.

Possible alternatives could be:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import com.google.common.collect.Iterables;

class Test {
    public static void test() {
        final List<ClassCastException> result = new ArrayList<ClassCastException>();
        List<Exception> myExceptions = new ArrayList<Exception>();
        Iterable<ClassCastException> iterable = Iterables.filter(myExceptions, ClassCastException.class);
        List<ClassCastException> tempList = new ArrayList<ClassCastException>();
        iterable.forEach(tempList::add);
        result.addAll(new HashSet<>(tempList));
    }
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import com.google.common.collect.Iterables;

class Test {
    public static void test() {
        final List<ClassCastException> result = new ArrayList<ClassCastException>();
        List<Exception> myExceptions = new ArrayList<Exception>();
        result.addAll(new HashSet<>(StreamSupport.stream(Iterables.filter(myExceptions, ClassCastException.class).spliterator(), false).collect(Collectors.toList())));
    }
}

What did you see instead?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import com.google.common.collect.Iterables;

class Test {
    public static void test() {
        final List<ClassCastException> result = new ArrayList<ClassCastException>();
        List<Exception> myExceptions = new ArrayList<Exception>();
        result.addAll(new HashSet<>(Arrays.asList(Iterables.filter(myExceptions, ClassCastException.class))));
    }
}

What is the full stack trace of any errors you encountered?

The following Java errors appear after the migration:

  • Cannot infer type arguments for HashSet<>
  • The method addAll(Collection<? extends ClassCastException>) in the type List<ClassCastException> is not applicable for the arguments (new HashSet<>(Arrays.asList(Iterables.filter(myExceptions, ClassCastException.class))))

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions