JvmField forbidden for delegated properties

This commit is contained in:
Mikhail Glukhikh
2016-01-15 15:31:14 +03:00
parent ec909d0775
commit a2bba7c8f1
4 changed files with 10 additions and 16 deletions
@@ -27,20 +27,7 @@ fun DeclarationDescriptor.hasJvmOverloadsAnnotation(): Boolean {
return annotations.findAnnotation(FqName("kotlin.jvm.JvmOverloads")) != null
}
fun DeclarationDescriptor.findJvmFieldAnnotation(): AnnotationDescriptor? {
val fqName = FqName("kotlin.jvm.JvmField")
val annotation = annotations.findAnnotation(fqName)
if (annotation != null) {
return annotation;
}
return annotations.getUseSiteTargetedAnnotations().asSequence().filter {
it.target == AnnotationUseSiteTarget.FIELD
}.map { it.annotation }.firstOrNull {
val descriptor = it.type.constructor.declarationDescriptor
descriptor is ClassDescriptor && fqName.toUnsafe() == DescriptorUtils.getFqName(descriptor)
}
}
fun DeclarationDescriptor.findJvmFieldAnnotation() = DescriptorUtils.getAnnotationByFqName(annotations, FqName("kotlin.jvm.JvmField"))
fun DeclarationDescriptor.hasJvmFieldAnnotation(): Boolean {
return findJvmFieldAnnotation() != null
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil
import org.jetbrains.kotlin.fileClasses.isInsideJvmMultifileClassFile
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DeclarationChecker
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
@@ -40,7 +41,8 @@ class JvmFieldApplicabilityChecker : DeclarationChecker {
LATEINIT("JvmField cannot be applied to lateinit property"),
CONST("JvmField cannot be applied to const property"),
INSIDE_COMPANION_OF_INTERFACE("JvmField cannot be applied to a property defined in companion object of interface"),
TOP_LEVEL_PROPERTY_OF_MULTIFILE_FACADE("JvmField cannot be applied to top level property of a file annotated with ${JvmFileClassUtil.JVM_MULTIFILE_CLASS_SHORT}")
TOP_LEVEL_PROPERTY_OF_MULTIFILE_FACADE("JvmField cannot be applied to top level property of a file annotated with ${JvmFileClassUtil.JVM_MULTIFILE_CLASS_SHORT}"),
DELEGATE("JvmField cannot be applied to delegated property")
}
override fun check(
@@ -54,6 +56,7 @@ class JvmFieldApplicabilityChecker : DeclarationChecker {
val problem = when {
// First two cases just prevent duplication of WRONG_ANNOTATION_TARGET
descriptor !is PropertyDescriptor -> return
declaration is KtProperty && declaration.hasDelegate() -> DELEGATE
!descriptor.hasBackingField(bindingContext) -> return
descriptor.isOverridable -> NOT_FINAL
Visibilities.isPrivate(descriptor.visibility) -> PRIVATE