openrewrite / rewrite-migrate-java

OpenRewrite recipes for migrating to newer versions of Java.
Apache License 2.0
111 stars 75 forks source link

`org.openrewrite.java.migrate.util.UseMapOf` fails on double brace init for non-string types #566

Closed blipper closed 1 month ago

blipper commented 1 month ago

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

class A {
   import static foo.BLAH;  // Some string 
    void foo() {
                  new HashMap<>() {{
                    put(BLAH, "foo");
                }}
    }
}

What did you expect to see?

import static foo.BLAH;  // Some string 
Map.of(BLAH, "foo")

The problem is that https://github.com/openrewrite/rewrite-migrate-java/blob/main/src/main/java/org/openrewrite/java/migrate/util/UseMapOf.java#L66 is using untyped which fails for non-trivial types

What did you see instead?

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

Caused by: java.lang.RuntimeException: Error while visiting tst/com/amazon/javelinprimingservice/creativemetadata/AmentumAdaptorTest.java: java.lang.IllegalArgumentException: Template parameter 1 cannot be used in an untyped template substitution. Instead of "#{}", indicate the template parameter's type with "#{any(java.lang.String)}".
  org.openrewrite.java.internal.template.Substitutions.substituteUntyped(Substitutions.java:206)
  org.openrewrite.java.internal.template.Substitutions.lambda$substitute$0(Substitutions.java:82)
  org.openrewrite.internal.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:98)
  org.openrewrite.internal.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:72)
  org.openrewrite.java.internal.template.Substitutions.substitute(Substitutions.java:54)
  org.openrewrite.java.JavaTemplate.apply(JavaTemplate.java:109)
  org.openrewrite.java.migrate.util.UseMapOf$1.visitNewClass(UseMapOf.java:79)
  org.openrewrite.java.migrate.util.UseMapOf$1.visitNewClass(UseMapOf.java:51)```
BramliAK commented 1 month ago

I did some testing and it works fine, can I have another case that shows the exception

blipper commented 1 month ago

Updated the test case sorry.