You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MethodMatcher patterns that reference Java built-in types (e.g. String, Throwable) do not match equivalent Kotlin constructor/method invocations, because KotlinTypeMapping.methodInvocationType records parameter types as kotlin.String / kotlin.Throwable instead of the remapped java.lang.String / java.lang.Throwable.
...returns false on Kotlin, even though the equivalent Java source matches fine. This breaks any recipe that relies on MethodMatcher / ReorderMethodArguments / etc. for Kotlin sources when the signature references Java built-ins.
Downstream impact example: https://github.com/moderneinc/customer-requests/issues/2235 — the Jackson 2→3 migration's argument-reordering for JsonGenerationException → StreamWriteException does not fire on Kotlin code and we had to add a downstream workaround in rewrite-jackson (ReorderJsonGenerationExceptionArguments) that accepts both kotlin.String and java.lang.String (and the Throwable equivalents).
Root cause
In rewrite-kotlin/src/main/java/org/openrewrite/kotlin/KotlinTypeMapping.kt, the methodInvocationType function (around line 866–1010) builds parameter types like this (around line 992):
for (p in function.valueParameters) {
val t = type(p.returnTypeRef)
paramTypes.add(t) // <-- no remapKotlinBuiltin applied
}
...but not to method parameter types in methodInvocationType, nor (likely) to the method return type or to field types. A comprehensive pass is probably needed.
Proposed fix
Apply remapKotlinBuiltin to each parameter type before adding it to paramTypes in methodInvocationType. Audit the rest of that function (return type, thrown exceptions, receiver type) for the same gap. Also audit variableType and related functions for the same class of bug on field/property types.
How to verify the fix
Add a test in rewrite-kotlin that parses a Kotlin constructor call taking a String / Throwable and asserts the resulting JavaType.Method.parameterTypes contain java.lang.String / java.lang.Throwable rather than kotlin.String / kotlin.Throwable.
End-to-end, once this lands and propagates:
In rewrite-jackson, revert the workaround recipe ReorderJsonGenerationExceptionArguments back to the three standard ReorderMethodArguments entries in src/main/resources/META-INF/rewrite/jackson-2-3.yml.
The two Kotlin tests (reorderThreeArgsKotlin, reorderTwoArgsKotlin) in ReorderStreamWriteExceptionArgumentsTest should still pass.
Summary
MethodMatcherpatterns that reference Java built-in types (e.g.String,Throwable) do not match equivalent Kotlin constructor/method invocations, becauseKotlinTypeMapping.methodInvocationTyperecords parameter types askotlin.String/kotlin.Throwableinstead of the remappedjava.lang.String/java.lang.Throwable.remapKotlinBuiltinin multiple places, but missed method parameter types.Reproduction
Given Kotlin source:
J.NewClassnode'sconstructorTypeparameter types are:So a
MethodMatcherlike:...returns
falseon Kotlin, even though the equivalent Java source matches fine. This breaks any recipe that relies onMethodMatcher/ReorderMethodArguments/ etc. for Kotlin sources when the signature references Java built-ins.JsonGenerationException→StreamWriteExceptiondoes not fire on Kotlin code and we had to add a downstream workaround in rewrite-jackson (ReorderJsonGenerationExceptionArguments) that accepts bothkotlin.Stringandjava.lang.String(and theThrowableequivalents).Root cause
In
rewrite-kotlin/src/main/java/org/openrewrite/kotlin/KotlinTypeMapping.kt, themethodInvocationTypefunction (around line 866–1010) builds parameter types like this (around line 992):remapKotlinBuiltinis already applied at Align Kotlin type model with Java parser output #7364 in:Supertypes (line ~485)
Interfaces
Generic bounds (lines ~332, ~334, ~1205, ~1207)
Annotations (lines ~1704, ~1722)
...but not to method parameter types in
methodInvocationType, nor (likely) to the method return type or to field types. A comprehensive pass is probably needed.Proposed fix
Apply
remapKotlinBuiltinto each parameter type before adding it toparamTypesinmethodInvocationType. Audit the rest of that function (return type, thrown exceptions, receiver type) for the same gap. Also auditvariableTypeand related functions for the same class of bug on field/property types.How to verify the fix
Add a test in rewrite-kotlin that parses a Kotlin constructor call taking a
String/Throwableand asserts the resultingJavaType.Method.parameterTypescontainjava.lang.String/java.lang.Throwablerather thankotlin.String/kotlin.Throwable.End-to-end, once this lands and propagates:
ReorderJsonGenerationExceptionArgumentsback to the three standardReorderMethodArgumentsentries insrc/main/resources/META-INF/rewrite/jackson-2-3.yml.reorderThreeArgsKotlin,reorderTwoArgsKotlin) inReorderStreamWriteExceptionArgumentsTestshould still pass.Context for whoever picks this up
KotlinTypeUtils.remapKotlinBuiltin— just needs to be invoked in more places.rewrite-jackson/src/main/java/org/openrewrite/java/jackson/ReorderJsonGenerationExceptionArguments.javaisString/isThrowablehelpers there accept bothkotlin.*andjava.lang.*FQNs precisely to paper over this bug.