fix: handle Kotlin enum constant subclass (isAnonymousClass==false) in TypeHandlerRegistry#3659
Open
wangshuai67 wants to merge 1 commit intomybatis:masterfrom
Open
Conversation
…batis#3658) In Kotlin, enum constants with overridden abstract methods generate a subclass where isAnonymousClass() returns false (unlike Java). This caused TypeHandlerRegistry to fail finding the correct TypeHandler for such enum constants. Fix by additionally checking clazz.getSuperclass().isEnum() as a fallback to detect Kotlin-style enum constant subclasses in both getSmartHandler and getJdbcHandlerMap.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Fixes #3658
In Kotlin, when an enum constant overrides an abstract method, the Kotlin compiler generates a subclass of the enum with the constant's behavior. Unlike Java's behavior, Class.isAnonymousClass() returns alse for these generated subclasses in Kotlin.
This causes TypeHandlerRegistry to fail when looking up TypeHandler for such Kotlin enum constants, because the registry uses isAnonymousClass() as the sole signal to detect "this is an enum constant subclass, look at the parent":
java // Before (line 307) - misses Kotlin enum constant subclasses Class<?> enumClass = clazz.isAnonymousClass() ? clazz.getSuperclass() : clazz;Reproduction (Kotlin)
`kotlin
enum class TestEnum {
TEST1 {
override fun test() = println("test1")
};
abstract fun test()
}
fun main() {
println(TestEnum.TEST1::class.java.isAnonymousClass) // false in Kotlin!
println(TestEnum.TEST1.javaClass.superclass == TestEnum::class.java) // true
}
`
When MyBatis tries to resolve a TypeHandler for TestEnum.TEST1, it gets the generated subclass (not TestEnum), fails to find a handler, and the mapping breaks.
Fix
getSmartHandler (line 307)
Add a secondary check: if clazz.getSuperclass().isEnum() is true, the class is an enum constant's body subclass regardless of isAnonymousClass().
java // After - handles both Java and Kotlin Class<?> enumClass = (clazz.isAnonymousClass() || (clazz.getSuperclass() != null && clazz.getSuperclass().isEnum())) ? clazz.getSuperclass() : clazz;getJdbcHandlerMap (line 331)
Add a branch to look up the handler map via the parent enum class when the class is a Kotlin-style enum subclass.
java } else if (clazz.getSuperclass() != null && clazz.getSuperclass().isEnum()) { jdbcHandlerMap = typeHandlerMap.get(clazz.getSuperclass()); }Notes