Skip to content

Guava ImmutableMap recipe fails when variable type is ImmutableMap #256

@shawware

Description

@shawware

What version of OpenRewrite are you using?

I am using:

  • OpenRewrite v8.1.8
  • Maven plugin v5.2.6
  • rewrite-migrate-java v2.0.6

How are you running OpenRewrite?

I am using the Maven plugin and my project is a single module project.

<plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>5.2.6</version>
    <configuration>
        <checkstyleDetectionEnabled>false</checkstyleDetectionEnabled>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-migrate-java</artifactId>
            <version>2.0.6</version>
        </dependency>
    </dependencies>
</plugin>

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

package org.example;

import com.google.common.collect.ImmutableMap;

public class ImmutableCollectionDemo {
    private void poorVariableType() {
        ImmutableMap<String, String> map = ImmutableMap.of("alpha", "beta");
    }
}

This code is a little unusual give that the type of map is ImmutableMap, rather than Map.

What did you expect to see?

package org.example;

import java.util.Map;

public class ImmutableCollectionDemo {
    private void poorVariableType() {
        Map<String, String> map = Map.of("alpha", "beta");
    }
}

Both uses of ImmutableMap converted for consistency.
(Alternatively, we could play it safe and not change anything in this instance.)

What did you see instead?

package org.example;

import java.util.Map;

import com.google.common.collect.ImmutableMap;

public class ImmutableCollectionDemo {
    private void poorVariableType() {
        ImmutableMap<String, String> map = Map.of("alpha", "beta");
    }
}

Only the second use of ImmutableMap is converted leading to a type mis-match and compilation error.

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

The generated code does not compile due to the mismatch of types:

no instance(s) of type variable(s) K, V exist so that Map<K, V> conforms to ImmutableMap<String, String>

Any other information

I tested the equivalent for ImmutableList and ImmutableSet and no change to the code was made by either recipe. So perhaps we should be consistent for ImmutableMap.

Are you interested in contributing a fix to OpenRewrite?

Yes, if possible. Not sure how useful I'll be as I'm not familiar with your code base, nor have I written my own recipe (yet).

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