Make ChangeStaticFieldToMethod work on Kotlin sources - #8352
Merged
Conversation
The replacement template is a fully qualified static method invocation (`StandardCharsets.UTF_8.name()`) that references nothing from the surrounding scope, but it was built `contextSensitive()`. That made the generated stub include the enclosing type printed as Java, which for Kotlin sources is not valid Java, so the stub compiled to nothing and the recipe threw "Failed to parse expression template" instead of making a change. Even where the stub did compile, the resulting `J.MethodInvocation` came back without type attribution. Reported as defect F on moderneinc/customer-requests#2924, where the error marker was attributed to `ReplaceConstantWithAnotherConstant`; that recipe uses no template and is fine on Kotlin, so tests are added for it as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was actually broken
The marker the customer saw was attributed to
org.openrewrite.java.ReplaceConstantWithAnotherConstant, but that recipe builds noJavaTemplateat all, so it cannot produce a "Failed to parse expression template" error. The template in the message —StandardCharsets.UTF_8.name()— comes fromorg.openrewrite.java.ChangeStaticFieldToMethod, as used byorg.openrewrite.apache.commons.lang3.UseStandardCharsetsto migrateCharEncoding.UTF_8. So F is another instance of the misattribution tracked as defect G.The fix
ChangeStaticFieldToMethodbuilt its replacement templatecontextSensitive(). The template is a fully qualified static method invocation that references nothing from the surrounding scope, so context sensitivity buys nothing — but it makesBlockStatementTemplateGeneratoremit the enclosing type printed as Java. For a Kotlin source that stub is not valid Java, it compiles to zero trees, andJavaTemplateParser#parseExpressionthrows.Dropping
.contextSensitive()fixes it. As a bonus, the resultingJ.MethodInvocationnow carries type attribution on Kotlin, which it did not before (Kotlin'sassertValidTypesflagged it even in the cases that did not throw).Tests
rewrite-kotlinChangeStaticFieldToMethodTest— a qualified field access, a statically imported one, and the reported shape (argument to a call inside avalin a generic function of a subclass with backtick-named members). All three fail onmain: the first two withLST contains missing or invalid type information, the third with the exactFailed to parse expression templatefrom the issue.rewrite-kotlinReplaceConstantWithAnotherConstantTest— added to pin down that this recipe is in fact Kotlin-clean, so the next report of this shape is not chased into the wrong file.rewrite-java-test's existingChangeStaticFieldToMethodTeststill passes unchanged.