Skip to content

NoGuavaImmutableListCopyOf rewrites ImmutableList.copyOf(Iterable) to invalid List.copyOf(Iterable) #1086

@protocol7

Description

@protocol7

What version of OpenRewrite are you using?

  • rewrite-migrate-java: 3.34.0
  • rewrite-core: 8.81.4
  • rewrite-java: 8.81.4

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

package com.helloworld;

import com.google.common.collect.ImmutableList;
import java.util.List;

class Main {
  List<String> copy(Iterable<String> values) {
    return ImmutableList.copyOf(values);
  }
}

Run org.openrewrite.java.migrate.guava.NoGuavaImmutableListCopyOf with Java 21 source/version markers.

What did you expect to see?

No change.

ImmutableList.copyOf(..) accepts an Iterable, but List.copyOf(..) only accepts a Collection, so this overload cannot be rewritten directly to List.copyOf(..).

What did you see instead?

The recipe rewrites the call to List.copyOf(values):

package com.helloworld;

import java.util.List;

class Main {
  List<String> copy(Iterable<String> values) {
    return List.copyOf(values);
  }
}

This rewritten code does not compile.

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

Downstream compilation fails with an error like:

- error: method copyOf in interface List<E#2> cannot be applied to given types;
    return List.copyOf(values);
               ^
  - required: Collection<? extends E#1>
  found: Iterable<String>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions