rewrite-migrate-java icon indicating copy to clipboard operation
rewrite-migrate-java copied to clipboard

Fix Failures in UseVar Recipe discovered by moderne.io

Open MBoegers opened this issue 2 years ago • 4 comments

What's changed?

What's your motivation?

To fix failures occurring in moderne.io runs, examples:

  • spring-cloud/spring-cloud-contract @ main: spring-cloud-contract-verifier/src/test/resources/contractsToCompile/contract_multipart.java java.lang.IllegalArgumentException: Unable to parse expression from JavaType Unknown
  • spring-projects/spring-data-commons @ main: src/test/java/org/springframework/data/domain/ManagedTypesUnitTests.java: Expected a template that would generate exactly one statement to replace one statement, but generated 2. Template: var typesSupplier = P.<org.springframework.data.domain.ManagedTypesUnitTests.1>/p1/p()
  • // spring-projects/spring-hateoas @ main src/test/java/org/springframework/hateoas/mediatype/html/HtmlInputTypeUnitTests.java: Expected a template that would generate exactly one statement to replace one statement, but generated 2. Template: var numbers = P.<java.util.stream.Stream<org.springframework.hateoas.mediatype.html.HtmlInputTypeUnitTests..>>/p1/p()

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

no, failures are not an option, here ;)

Any additional context

Checklist

  • [ ] I've added unit tests to cover both positive and negative cases
  • [ ] I've added the license header to any new files through ./gradlew licenseFormat
  • [ ] I've used the IntelliJ IDEA auto-formatter on affected files
  • [ ] I've updated the documentation (if applicable)

MBoegers avatar Sep 06 '23 07:09 MBoegers

I have problems to understand the failure 3. The test src/test/java/org/openrewrite/java/migrate/lang/UseVarKeywordTest.BugFixing#dusplicateTemplate reproduces failure 3.

The Problem seems to be that the used JavaTemplate var #{} = #{any()} in UseVarForGenericMethodInvocations.UseVarForGenericsVisitor which is applied in UseVarForGenericMethodInvocations.UseVarForGenericsVisitor#transformToVarL109 produces two statements and I am unable to see why or even understand which.

MBoegers avatar Sep 06 '23 09:09 MBoegers

Possibly you need to invoke contextSensitive() on the template builder. This is necessary when to template refers to any elements from the context into which it will be inserted.

knutwannheden avatar Sep 06 '23 09:09 knutwannheden

Looks like we already support two of the three new tests covered here; Managed to get the third closer by adding the .contextSensitive() as suggested, and fixed some small test issues. Not yet looked at what's needed for a proper fix to that last failing test. Would you like to continue exploring that @MBoegers ?

timtebeek avatar Sep 14 '23 10:09 timtebeek

I also played around with it but have no clue yet how to fix... I would love to have them fixed, because breaking some code bases is not a great option, but TBH I have no time to look into this until 07.October. Feel free to step in 😅

MBoegers avatar Sep 15 '23 16:09 MBoegers