Parcelable: produce error on "CREATOR" companion object

This commit is contained in:
Yan Zhulanow
2017-09-08 22:07:12 +03:00
committed by Yan Zhulanow
parent d9f99971bb
commit 26af128694
2 changed files with 19 additions and 3 deletions
@@ -40,8 +40,9 @@ import org.jetbrains.kotlin.resolve.jvm.annotations.findJvmFieldAnnotation
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.isError
private val ANDROID_PARCELABLE_CLASS_FQNAME = FqName("android.os.Parcelable")
internal val ANDROID_PARCEL_CLASS_FQNAME = FqName("android.os.Parcel")
val ANDROID_PARCELABLE_CLASS_FQNAME = FqName("android.os.Parcelable")
val ANDROID_PARCELABLE_CREATOR_CLASS_FQNAME = FqName("android.os.Parcelable.Creator")
val ANDROID_PARCEL_CLASS_FQNAME = FqName("android.os.Parcel")
class ParcelableDeclarationChecker : SimpleDeclarationChecker {
private companion object {
@@ -132,6 +133,13 @@ class ParcelableDeclarationChecker : SimpleDeclarationChecker {
return
}
for (companion in declaration.companionObjects) {
if (companion.name == "CREATOR") {
val reportElement = companion.nameIdentifier ?: companion
diagnosticHolder.reportFromPlugin(ErrorsAndroid.CREATOR_DEFINITION_IS_NOT_ALLOWED.on(reportElement), DefaultErrorMessagesAndroid)
}
}
val sealedOrAbstract = declaration.modifierList?.let { it.getModifier(KtTokens.ABSTRACT_KEYWORD) ?: it.getModifier(KtTokens.SEALED_KEYWORD) }
if (sealedOrAbstract != null) {
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_SHOULD_BE_INSTANTIABLE.on(sealedOrAbstract), DefaultErrorMessagesAndroid)
@@ -8,9 +8,17 @@ import android.os.Parcel
class A(val a: String) : Parcelable {
companion object {
@JvmField
val <error descr="[PLUGIN_ERROR] CREATOR_DEFINITION_IS_NOT_ALLOWED: 'CREATOR' definition is not allowed. Use 'Parceler' companion object instead.">CREATOR</error> = object : Parcelable.Creator<A> {
val <error descr="[CREATOR_DEFINITION_IS_NOT_ALLOWED] 'CREATOR' definition is not allowed. Use 'Parceler' companion object instead.">CREATOR</error> = object : Parcelable.Creator<A> {
override fun createFromParcel(source: Parcel): A = A("")
override fun newArray(size: Int) = arrayOfNulls<A>(size)
}
}
}
@Parcelize
class B(val b: String) : Parcelable {
companion object <error descr="[CREATOR_DEFINITION_IS_NOT_ALLOWED] 'CREATOR' definition is not allowed. Use 'Parceler' companion object instead.">CREATOR</error> : Parcelable.Creator<B> {
override fun createFromParcel(source: Parcel): B = B("")
override fun newArray(size: Int) = arrayOfNulls<B>(size)
}
}