Skip to content

UseEnumSetOf rewrite breaks for varargs parameter #516

@mbruggmann

Description

@mbruggmann

What version of OpenRewrite are you using?

rewrite-migrate-java 2.20.0

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

I can reproduce the issue with the following testcase:

  @Test
  void enumSetOfVarargs() {
    rewriteRun(
        spec -> spec.recipe(new UseEnumSetOf()),
        java(
            """
            package com.helloworld;

            import java.util.concurrent.TimeUnit;
            import java.util.Set;

            public class Main {

              public Set<TimeUnit> method(final TimeUnit... units) {
                final Set<TimeUnit> asSet = Set.of(units);
                return asSet;
              }

            }"""));
  }
}

What did you expect to see?

Either to bail out and keep it as is.

Or alternatively we could translate the varargs parameter into a collection first, e.g.

public Set<TimeUnit> method(final TimeUnit... units) {
  final Set<TimeUnit> asSet = EnumSet.noneOf(TimeUnit.class);
  asSet.addAll(Arrays.asList(units));
  return asSet;
}

Note that the more convenient-looking EnumSet.copyOf(asList(units)) throws an exception for empty array.

What did you see instead?

public Set<TimeUnit> method(final TimeUnit... units) {
  final Set<TimeUnit> asSet = EnumSet.of(units);
  return asSet;
}

(doesn't compile)

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions