Skip to content
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

Kotlin constructor invocation parsed as method invocation #622

Closed
reisners opened this issue Jan 2, 2025 — with Slack · 6 comments
Closed

Kotlin constructor invocation parsed as method invocation #622

reisners opened this issue Jan 2, 2025 — with Slack · 6 comments
Labels
bug Something isn't working parser-kotlin test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail

Comments

Copy link

reisners commented Jan 2, 2025

I'm trying to use OpenRewrite (bom version 2.23.1, rewrite-kotlin 1.23.1) to do some static analysis on our Kotlin and Java Spring Boot codebase.
I noticed the following issue: OpenRewrite does not correctly parse invocations of constructors of Kotlin classes defined in 3rd party dependencies or even Kotlin standard library, e.g. Pair. It seems to interpret the constructor invocation as a method invocation and complains about the unknown return type, as can be seen in the following test case:

    @Test
    fun constructorInvocation() {
        rewriteRun(
            org.openrewrite.kotlin.Assertions.kotlin(
                """
                    class A(val x: String)

                    val a = A("x")
                    val pair = kotlin.Pair(1, "y") 
              """
                    .trimIndent()
            )
        )
    }

While the locally declared A("x") is correctly identified as a constructor invocation, the last line errors with

java.lang.IllegalStateException: LST contains missing or invalid type information
Identifier->MethodInvocation->NamedVariable->VariableDeclarations->CompilationUnit
/*~~(Identifier type is missing or malformed)~~>*/kotlin

	at org.openrewrite.kotlin.Assertions.assertValidTypes(Assertions.java:234)

(the behavior does not change if I use import statements)

@timtebeek timtebeek added bug Something isn't working parser-kotlin test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail labels Jan 2, 2025
@timtebeek timtebeek moved this to Backlog in OpenRewrite Jan 2, 2025
@timtebeek
Copy link
Contributor

Thanks for the runnable example @reisners ! Did you already explore what the cause might be?

@reisners
Copy link
Author

reisners commented Jan 3, 2025

I did some debugging, but not yet to a conclusive result. Will keep looking

@reisners
Copy link
Author

reisners commented Jan 4, 2025

To begin with, I was setting up the test wrongly, using the JavaParser builder instead of the KotlinParser.Builder, and I wasn't adding 3rd party dependencies to either parser's classpath (although this is irrelevant for the example code, I noticed that the same error also occurred when invoking the constructor of a class from some other 3rd party dependency).
However, my contrived example was afflicted by another problem: I had added kotlin 2.1 dependencies to the recipe project, but rewrite-kotlin only supports kotlin 1.9, and the presence of kotlin-stdlib 2.1 on the classpath seems to confuse the Fir parser (in a way which I did not bother analyzing further).
The mechanism for adding artifacts to the classpath does not allow to select only kotlin-stdlib 2.1 in favor of kotlin-stdlib-common-1.9.10 because of the limitations of the pattern matching. Therefore I wasn't table to test a configuration with only kotlin-stdlib 2.1.0 on the classpath.
But with only kotlin-stdlib-common-1.9.10 (by switching the entire project to kotlin 1.9), the above test passes without error.
I'm closing this issue and looking forward to testing it again when rewrite-kotlin supports kotlin 2.1.

@reisners reisners closed this as completed Jan 4, 2025
@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenRewrite Jan 4, 2025
@timtebeek
Copy link
Contributor

Thanks for reporting your findings here! Indeed 2.1+ support is pending and tracked here; we're exploring a collaboration there.

@reisners
Copy link
Author

reisners commented Jan 5, 2025 via email

@timtebeek
Copy link
Contributor

Thanks a lot for the kind words! Indeed a lot of work and passion has been poured into the project, and we feel fortunate with where we stand and the support from the wider community. I continue to look forward to where this will go. It's a big shift! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser-kotlin test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail
Projects
Archived in project
Development

No branches or pull requests

2 participants