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

Fix NoSuchFieldError in newer Kotlin plugins #3422

Merged
merged 1 commit into from Aug 8, 2022

Conversation

madisp
Copy link
Contributor

@madisp madisp commented Aug 7, 2022

Uses an ugly reflection hack to access the KotlinTopLevelTypeAliasFqNameIndex in the newer Kotlin plugin on IDEA 2022.2 / Kotlin 1.7.x.

Fixes #3324

try {
val instanceField = KotlinTopLevelTypeAliasFqNameIndex::class.java.getField("INSTANCE")
val instance = instanceField.get(null)
if (instance is KotlinTopLevelTypeAliasFqNameIndex) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not we throw an exception in the else block in case instance is of any type other than KotlinTopLevelTypeAliasFqNameIndex? So if in the future it breaks again, we at least get a proper stacktrace

Copy link
Contributor Author

@madisp madisp Aug 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I think that case overall is highly unlikely: <T>.INSTANCE static field exists but is not of type T.

I guess to make it safer we could reflectively check for Companion + getInstance method though and then throw if neither INSTANCE field nor $Companion.getInstance is present?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

} catch (e: Exception) {
/* intentionally empty, fall back to getInstance() call in case of errors */
}
return KotlinTopLevelTypeAliasFqNameIndex.getInstance()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then this could be moved into the catch block

Copy link
Collaborator

@AlecKazakova AlecKazakova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

java.lang.NoSuchFieldError in SqlDelightReferenceContributor.kt:67
3 participants