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

Serializer is retrieved as if it is a named companion object when companion object is private #1743

Closed
Whathecode opened this issue Oct 26, 2021 · 1 comment

Comments

@Whathecode
Copy link
Contributor

Describe the bug
Not really a bug, just an observation based on looking at the codebase. I can do a PR later if relevant.

Companion objects are retrieved through the "Companion" object (unless it is a named companion object), but this will fail in case the companion object isn't public. isAccessible isn't set to true.

However, this will work at runtime, since the fallback to retrieve the serializer through the $serializer class will be used instead.

To Reproduce

@Serializable
class HasPrivateCompanion( val output: String )
{
    private companion object
}

Expected behavior
Not really certain. I suppose the main reason for attempting to look up the serializer through the Companion object is to speed up this process, assuming that it is significantly slower than retrieving it through $serializer. I presume lookup through Companion would still be preferred even if it is private. This should be fairly straightforward with isAccessible.

Alternatively, it does seem like more code can be obfuscated if the serializer is retrieved through it's companion object. It is by updating the ProGuard rules that I found out about this. This would thus be another reason to make sure private companions be accessed, otherwise a custom rule for the class with the private companion needs to be added.

Environment

  • Kotlin version: 1.4.31
  • Library version: 1.1.0
  • Kotlin platforms: JVM
  • Gradle version: 7.0.2
@Whathecode
Copy link
Contributor Author

My bad. Totally overlooked that isAccessible is in fact set to true: https://github.com/Kotlin/kotlinx.serialization/blob/master/core/jvmMain/src/kotlinx/serialization/internal/Platform.kt#L113

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

No branches or pull requests

1 participant