Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MigrateCollectionsSingletonList produces IllegalArgumentException #150

Closed
crehn opened this issue Dec 8, 2022 · 2 comments
Closed

MigrateCollectionsSingletonList produces IllegalArgumentException #150

crehn opened this issue Dec 8, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@crehn
Copy link

crehn commented Dec 8, 2022

Prerequisite:
Have something like this in your code:

    public enum FooEnum {
        FOO, BAR;

        @AllArgsConstructor
        public enum BarEnum {

            foobar(singletonList(FOO));

            private final List<FooEnum> expectedStates;
        }
    }

Then

mvn org.openrewrite.maven:rewrite-maven-plugin:4.37.0:run \
  -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-migrate-java:1.14.1 \
  -DactiveRecipes=org.openrewrite.java.migrate.util.MigrateCollectionsSingletonList

produces this error:

[WARNING] java.lang.IllegalArgumentException: Expected a template that would generate exactly one statement to replace one statement, but generated 2. Template:
List.of(__P__.<com.....FooEnum>/*__p0__*/p())
[ERROR] The recipe produced an error. Please report this to the recipe author.

Every recipe that uses this recipe indirectly also produces this error.

@traceyyoshima traceyyoshima added the bug Something isn't working label Dec 12, 2022
knutwannheden added a commit that referenced this issue Feb 18, 2023
…letonList`

The `JavaTemplate` engine can throw an `IllegalStateException` when there are missing type attributions. To guard against this in the presence of Lombok annotations, the recipe `MigrateCollectionsSingletonList` now uses `NoMissingTypes` as an additional single source applicability test.

Issue: #150
@knutwannheden
Copy link
Contributor

When I try to reproduce this one I get an IllegalStateException instead:

Caused by: java.lang.IllegalStateException: Unable to template a J.NewClass instance having a null clazz and constructor type.
	at org.openrewrite.java.internal.template.BlockStatementTemplateGenerator.template(BlockStatementTemplateGenerator.java:272)
	at org.openrewrite.java.internal.template.BlockStatementTemplateGenerator.lambda$template$0(BlockStatementTemplateGenerator.java:84)
	at io.micrometer.core.instrument.composite.CompositeTimer.record(CompositeTimer.java:65)
	at org.openrewrite.java.internal.template.BlockStatementTemplateGenerator.template(BlockStatementTemplateGenerator.java:71)
...

The reason being that due to the Lombok annotation @AllArgsConstructor OpenRewrite cannot find the referenced constructor in the source code.

@crehn We have another issue where we track the support for Lombok annotations: openrewrite/rewrite#1297. I suggest you subscribe to that issue for updates on this topic. In the meantime you can as a workaround maybe delombok the affected sources or migrate those manually.

@timtebeek So that the recipe no longer reports any exception, it now uses NoMissingTypes as an additional applicability test. I suggest we close this issue in favor of the general Lombok support issue.

@timtebeek
Copy link
Contributor

Appreciate you putting that guard in place @knutwannheden ; that resolved the immediate error in this case indeed & agree that it's better to have a single issue tracking Lombok support.

Hope you agree @crehn and can at least use the recipe for now! Until the next release you might want to have a look at using our snapshot versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

No branches or pull requests

4 participants