Michael Marshall
06/07/2021, 2:02 AMbindingContext
okay, but getType
is always null
. What should I be doing to get the class of the object declaration?
override fun visitObjectDeclaration(declaration: KtObjectDeclaration) {
super.visitObjectDeclaration(declaration)
if (bindingContext == BindingContext.EMPTY) return
if (bindingContext.getType(declaration) is Serializable) {
report(
CodeSmell(
issue = issue,
entity = Entity.from(declaration),
message = "Do not use Java Serialization for Kotlin objects."
)
)
}
}
(declaration.resolve() as? ClassDescriptor)?.defaultType
resolve() method not found
2.
(declaration as KtCallableDeclaration).createTypeBindingForReturnType(bindingContext)?.type
cast fails
3.
declaration.kotlinType(bindingContext)
always nullgammax
06/07/2021, 9:35 AMdeclaration.superTypeListEntries.any { it.text == "Serializable" }
Michael Marshall
06/07/2021, 9:41 AMis Serializable
checkgammax
06/07/2021, 10:08 AMbindingContext[BindingContext.CLASS, declaration]?.defaultType
Michael Marshall
06/07/2021, 10:29 AMis Serializable
check, I think it's a SimpleTypeImpl
rather than the actual class, or something lazy?gammax
06/07/2021, 11:05 AMis Serializable
check is most likely a bad approach. I believe you should reach a fully qualified type in some form and then do a string comparison with java.io.Serializable
.
As for the documentation, that’s Kotlin PSI & the compiler API. Really bad documented 😞 That’s a known issue on JetBrains table.Michael Marshall
06/07/2021, 11:07 AMisSerializable
then I'll need to recurse up the whole class hierarchy won't I?gammax
06/07/2021, 11:55 AMthen I’ll need to recurse up the whole class hierarchy won’t I?Mmm not really. I believe that with type resolution, you should be able to get all the implemented interfaces, also the one of your superclasses..
Michael Marshall
06/07/2021, 2:34 PMprivate val KtObjectDeclaration.isJavaSerializable
get() = bindingContext[BindingContext.CLASS, this]
?.defaultType
?.supertypes()
?.any { it.fqNameOrNull()?.asString() == "java.io.Serializable" } == true
gammax
06/07/2021, 3:21 PM