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

Fixed ChangeType for stackoverflow #3929

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zacscoding
Copy link

What's changed?

Check if classType is the same as classType.getOwningClass() in ChangeType::getTopLevelClassName()

What's your motivation?

Fixed ChangeType recipe to prevent stack over flow.

Any additional context

I used ChangeType recipe to change @ApiModelProperty annotation to @Schema and below is my sample kotlin code.

class MyClass {
  @ApiModelProperty("description")
  private val code: String? = null

  companion object {
    const val MY_CODE = "001"
  }
}

Above sample code is working well in the unit test code, but stack over flow error occurs if i run rewrite recipes.
More details, in the first call, classType is MyClass$Companion and classType.getOwningClass() is MyClass.
In the recursive calls, classType and classType.getOwningClass() is the MyClass repeatedly.

@timtebeek
Copy link
Contributor

Thanks for the proposed fix @zacscoding ! Seems like this is specific to Kotlin, so I'm doing a quick tag of @knutwannheden in case he'd like to solve this differently given the context above.

@zacscoding
Copy link
Author

Thanks @timtebeek , I look forward to the problem being resolved :)

@knutwannheden
Copy link
Contributor

@zacscoding As you are saying a unit test like the following passes without issues. I wonder what the problem is when run differently.

    @Test
    void companion() {
        rewriteRun(
          spec -> spec.recipe(new ChangeType("a.ApiModelProperty", "a.Schema", true)),
          kotlin(
            """
              package a

              annotation class ApiModelProperty
              """,
            SourceSpec::skip
          ),
          kotlin(
            """
              import a.ApiModelProperty

              class MyClass {
                @ApiModelProperty
                private val code: String? = null
                            
                companion object {
                  const val MY_CODE = "001"
                }
              }
              """,
            """
              import a.Schema

              class MyClass {
                @Schema
                private val code: String? = null
                            
                companion object {
                  const val MY_CODE = "001"
                }
              }
              """
          )
        );
    }

@zacscoding
Copy link
Author

zacscoding commented Jan 24, 2024

@knutwannheden Sorry for the late response. I just tried to execute rewriteRun command with our recipe jar like gradlew --init-script recipe.gradle rewriteRun :(

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
Projects
Status: Ready to Review
Development

Successfully merging this pull request may close these issues.

None yet

3 participants