-
Notifications
You must be signed in to change notification settings - Fork 519
Kotlin: remap builtins on Java-origin method/field types #7428
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -535,7 +535,7 @@ fun method() { | |
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) { | ||
| if ("println".equals(method.getSimpleName())) { | ||
| assertThat(method.getMethodType().toString()).isEqualTo("kotlin.io.ConsoleKt{name=println,return=void,parameters=[kotlin.Any]}"); | ||
| assertThat(method.getMethodType().toString()).isEqualTo("kotlin.io.ConsoleKt{name=println,return=void,parameters=[java.lang.Object]}"); | ||
| found.set(true); | ||
| } | ||
| return super.visitMethodInvocation(method, atomicBoolean); | ||
|
|
@@ -547,6 +547,39 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Atomi | |
| ); | ||
| } | ||
|
|
||
| @Issue("https://github.com/openrewrite/rewrite/issues/7427") | ||
| @Test | ||
| void constructorParameterTypesRemapKotlinBuiltins() { | ||
| // A Kotlin constructor taking `String` / `Throwable` must surface parameter types | ||
| // as the Java-parser's `java.lang.String` / `java.lang.Throwable`, not the Kotlin | ||
| // builtins, so `MethodMatcher` patterns written against the Java FQN match. | ||
| rewriteRun( | ||
| kotlin( | ||
| """ | ||
| fun example(cause: Throwable): IllegalArgumentException { | ||
| return IllegalArgumentException("message", cause) | ||
| } | ||
| """, | ||
| spec -> spec.afterRecipe(cu -> { | ||
| var matcher = new MethodMatcher("java.lang.IllegalArgumentException <constructor>(String, Throwable)"); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem we saw was specifically with MethodMatcher not being KotlinTypeUtils aware, and hence seeing Kotlin types here instead of their Java equivalents. |
||
| var found = new AtomicBoolean(false); | ||
| new KotlinIsoVisitor<AtomicBoolean>() { | ||
| @Override | ||
| public J.NewClass visitNewClass(J.NewClass newClass, AtomicBoolean atomicBoolean) { | ||
| var paramTypes = newClass.getConstructorType().getParameterTypes(); | ||
| assertThat(paramTypes.get(0).toString()).isEqualTo("java.lang.String"); | ||
| assertThat(paramTypes.get(1).toString()).isEqualTo("java.lang.Throwable"); | ||
| assertThat(matcher.matches(newClass)).isTrue(); | ||
| atomicBoolean.set(true); | ||
| return super.visitNewClass(newClass, atomicBoolean); | ||
| } | ||
| }.visit(cu, found); | ||
| assertThat(found.get()).isTrue(); | ||
| }) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void whenExpression() { | ||
| rewriteRun( | ||
|
|
@@ -658,13 +691,13 @@ public J.Binary visitBinary(J.Binary binary, AtomicBoolean b) { | |
|
|
||
| @CsvSource(value = { | ||
| // Method type on overload with no named arguments. | ||
| "foo(\"\", 1, true)~org.example.openRewriteFile0Kt{name=foo,return=void,parameters=[kotlin.String,int,boolean]}", | ||
| "foo(\"\", 1, true)~org.example.openRewriteFile0Kt{name=foo,return=void,parameters=[java.lang.String,int,boolean]}", | ||
| // Method type on overload with named arguments. | ||
| "foo(b = 1)~org.example.openRewriteFile0Kt{name=foo,return=void,parameters=[int,boolean]}", | ||
| // Method type when named arguments are declared out of order. | ||
| "foo(trailingLambda = {}, noDefault = true, c = true, b = 1)~org.example.openRewriteFile0Kt{name=foo,return=void,parameters=[kotlin.String,int,boolean,boolean,kotlin.Function0<void>]}", | ||
| "foo(trailingLambda = {}, noDefault = true, c = true, b = 1)~org.example.openRewriteFile0Kt{name=foo,return=void,parameters=[java.lang.String,int,boolean,boolean,kotlin.Function0<void>]}", | ||
| // Method type with trailing lambda | ||
| "foo(b = 1, noDefault = true) {}~org.example.openRewriteFile0Kt{name=foo,return=void,parameters=[kotlin.String,int,boolean,boolean,kotlin.Function0<void>]}" | ||
| "foo(b = 1, noDefault = true) {}~org.example.openRewriteFile0Kt{name=foo,return=void,parameters=[java.lang.String,int,boolean,boolean,kotlin.Function0<void>]}" | ||
| }, delimiter = '~') | ||
| @ParameterizedTest | ||
| void methodInvocationWithDefaults(String invocation, String methodType) { | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where I'm a bit doubtful; do we really want to map all Kotlin types to their Java equivalents, even for packages like kotlin.io ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose not all Kotlin types have a direct Java equivalent. E.g.
Nothing?So I guess the answer would be no? For consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scoped to Java-origin classes (FirJavaClass) in 97295cd. The motivating Jackson case still works; Kotlin-declared signatures like
kotlin.io.ConsoleKt.println(kotlin.Any)are now preserved. JDK classes go through Kotlin's classfile loader as a different FIR type, so they're out of scope here — that'd need a Kotlin-awareMethodMatcher.