Deserialize property and field annotations

This commit is contained in:
Yan Zhulanow
2015-08-06 15:53:29 +03:00
parent f86fe6a8d5
commit 1b9dab47ec
7 changed files with 67 additions and 30 deletions
@@ -140,7 +140,7 @@ public class JvmSerializerExtension extends SerializerExtension {
fieldType = field.first;
fieldName = field.second;
isStaticInOuter = bindings.get(STATIC_FIELD_IN_OUTER_CLASS, property);
syntheticMethod = null;
syntheticMethod = bindings.get(SYNTHETIC_METHOD_FOR_PROPERTY, property);
}
else {
fieldType = null;
@@ -34,6 +34,9 @@ import org.jetbrains.kotlin.types.JetType
import java.util.ArrayList
import java.util.HashMap
import org.jetbrains.kotlin.serialization.deserialization.AnnotatedCallableKind.PROPERTY_SYNTHETIC_FUNCTION
import org.jetbrains.kotlin.serialization.deserialization.AnnotatedCallableKind.PROPERTY_FIELD
public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C : Any>(
storageManager: StorageManager,
private val kotlinClassFinder: KotlinClassFinder,
@@ -182,11 +185,14 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
val classProto = container.classProto!!
val classKind = Flags.CLASS_KIND[classProto.getFlags()]
val classId = nameResolver.getClassId(classProto.getFqName())
if (classKind == ProtoBuf.Class.Kind.CLASS_OBJECT && isStaticFieldInOuter(proto)) {
if (classKind == ProtoBuf.Class.Kind.CLASS_OBJECT && isStaticFieldInOuter(proto)
&& annotatedCallableKind != AnnotatedCallableKind.PROPERTY_SYNTHETIC_FUNCTION) {
// Backing fields of properties of a companion object are generated in the outer class
return kotlinClassFinder.findKotlinClass(classId.getOuterClassId())
}
else if (classKind == ProtoBuf.Class.Kind.TRAIT && annotatedCallableKind == AnnotatedCallableKind.PROPERTY) {
else if (classKind == ProtoBuf.Class.Kind.TRAIT &&
(annotatedCallableKind == PROPERTY_SYNTHETIC_FUNCTION || annotatedCallableKind == PROPERTY_FIELD)) {
if (proto.hasExtension(implClassName)) {
val parentPackageFqName = classId.getPackageFqName()
val tImplName = nameResolver.getName(proto.getExtension(implClassName))
@@ -282,6 +288,25 @@ private fun getCallableSignature(
kind: AnnotatedCallableKind
): MemberSignature? {
val deserializer = SignatureDeserializer(nameResolver)
fun getPropertySignature(handleField: Boolean = false, handleSynthetic: Boolean = false): MemberSignature? {
if (!proto.hasExtension(propertySignature)) return null
val propertySignature = proto.getExtension(propertySignature)
if (handleField && propertySignature.hasField()) {
val field = propertySignature.getField()
val type = deserializer.typeDescriptor(field.getType())
val name = nameResolver.getName(field.getName())
return MemberSignature.fromFieldNameAndDesc(name, type)
}
else if (handleSynthetic && propertySignature.hasSyntheticMethod()) {
return deserializer.methodSignature(propertySignature.getSyntheticMethod())
}
return null
}
when (kind) {
AnnotatedCallableKind.FUNCTION -> if (proto.hasExtension(methodSignature)) {
return deserializer.methodSignature(proto.getExtension(methodSignature))
@@ -292,19 +317,9 @@ private fun getCallableSignature(
AnnotatedCallableKind.PROPERTY_SETTER -> if (proto.hasExtension(propertySignature)) {
return deserializer.methodSignature(proto.getExtension(propertySignature).getSetter())
}
AnnotatedCallableKind.PROPERTY -> if (proto.hasExtension(propertySignature)) {
val propertySignature = proto.getExtension(propertySignature)
if (propertySignature.hasField()) {
val field = propertySignature.getField()
val type = deserializer.typeDescriptor(field.getType())
val name = nameResolver.getName(field.getName())
return MemberSignature.fromFieldNameAndDesc(name, type)
}
else if (propertySignature.hasSyntheticMethod()) {
return deserializer.methodSignature(propertySignature.getSyntheticMethod())
}
}
AnnotatedCallableKind.PROPERTY -> return getPropertySignature(true, true)
AnnotatedCallableKind.PROPERTY_FIELD -> return getPropertySignature(handleField = true)
AnnotatedCallableKind.PROPERTY_SYNTHETIC_FUNCTION -> return getPropertySignature(handleSynthetic = true)
}
return null
}
@@ -43,6 +43,7 @@ class BuiltInsAnnotationAndConstantLoader(
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<AnnotationDescriptor> {
if (kind == AnnotatedCallableKind.PROPERTY_FIELD) return emptyList()
val annotations = proto.getExtension(BuiltInsProtoBuf.callableAnnotation).orEmpty()
return annotations.map { proto -> deserializer.deserializeAnnotation(proto, nameResolver) }
}
@@ -19,6 +19,8 @@ package org.jetbrains.kotlin.serialization.deserialization;
public enum AnnotatedCallableKind {
FUNCTION,
PROPERTY,
PROPERTY_SYNTHETIC_FUNCTION,
PROPERTY_FIELD,
PROPERTY_GETTER,
PROPERTY_SETTER
}
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.serialization.deserialization
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget
import org.jetbrains.kotlin.descriptors.annotations.Annotations
@@ -31,7 +32,6 @@ import org.jetbrains.kotlin.serialization.ProtoBuf.Callable.CallableKind.VAL
import org.jetbrains.kotlin.serialization.ProtoBuf.Callable.CallableKind.VAR
import org.jetbrains.kotlin.serialization.deserialization.descriptors.*
import org.jetbrains.kotlin.utils.toReadOnlyList
import org.jetbrains.kotlin.utils.sure
public class MemberDeserializer(private val c: DeserializationContext) {
public fun loadCallable(proto: Callable): CallableMemberDescriptor {
@@ -48,7 +48,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
val property = DeserializedPropertyDescriptor(
c.containingDeclaration, null,
getAnnotations(proto, flags, AnnotatedCallableKind.PROPERTY),
getPropertyAnnotations(proto),
modality(Flags.MODALITY.get(flags)),
visibility(Flags.VISIBILITY.get(flags)),
Flags.CALLABLE_KIND.get(flags) == Callable.CallableKind.VAR,
@@ -185,22 +185,36 @@ public class MemberDeserializer(private val c: DeserializationContext) {
}
}
private fun getPropertyAnnotations(proto: Callable): Annotations {
return DeserializedAnnotationsWithPossibleTargets(c.storageManager) {
val annotations = arrayListOf<AnnotationWithTarget>()
val container = c.containingDeclaration.asProtoContainer()
fun loadAnnotations(kind: AnnotatedCallableKind, mapper: (AnnotationDescriptor) -> AnnotationWithTarget) {
annotations += c.components.annotationAndConstantLoader.loadCallableAnnotations(
container, proto, c.nameResolver, kind
).map(mapper)
}
loadAnnotations(AnnotatedCallableKind.PROPERTY_SYNTHETIC_FUNCTION) { AnnotationWithTarget(it, null) }
loadAnnotations(AnnotatedCallableKind.PROPERTY_FIELD) { AnnotationWithTarget(it, AnnotationUseSiteTarget.FIELD) }
annotations
}
}
private fun getReceiverParameterAnnotations(
proto: Callable,
kind: AnnotatedCallableKind,
receiverTargetedKind: AnnotatedCallableKind = kind
): Annotations {
return DeserializedAnnotationsWithPossibleTargets(c.storageManager) {
val annotations = arrayListOf<AnnotationWithTarget>()
val container = c.containingDeclaration.asProtoContainer()
if (proto.hasReceiverType()) {
annotations += c.components.annotationAndConstantLoader
val container = c.containingDeclaration.asProtoContainer()
c.components.annotationAndConstantLoader
.loadExtensionReceiverParameterAnnotations(container, proto, c.nameResolver, receiverTargetedKind)
.map { AnnotationWithTarget(it, AnnotationUseSiteTarget.RECEIVER) }
}
annotations
} else emptyList()
}
}
@@ -227,9 +241,6 @@ public class MemberDeserializer(private val c: DeserializationContext) {
kind: AnnotatedCallableKind,
valueParameter: Callable.ValueParameter
): Annotations {
if (!Flags.HAS_ANNOTATIONS.get(valueParameter.getFlags())) {
return Annotations.EMPTY
}
return DeserializedAnnotations(c.storageManager) {
c.components.annotationAndConstantLoader.loadValueParameterAnnotations(container, callable, c.nameResolver, kind, valueParameter)
}
@@ -240,4 +251,9 @@ public class MemberDeserializer(private val c: DeserializationContext) {
is DeserializedClassDescriptor -> ProtoContainer(classProto, null)
else -> error("Only members in classes or package fragments should be serialized: $this")
}
private companion object {
val CONSTRUCTOR_PARAMETER_ANNOTATION_MAPPER =
{ a: AnnotationDescriptor -> AnnotationWithTarget(a, AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER) }
}
}
@@ -37,8 +37,10 @@ public class AnnotationLoaderForKotlinJavaScriptStubBuilder() : AnnotationAndCon
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<ClassId> =
proto.getExtension(JsProtoBuf.callableAnnotation).orEmpty().map { nameResolver.getClassId(it.getId()) }
): List<ClassId> {
if (kind == AnnotatedCallableKind.PROPERTY_FIELD) return emptyList()
return proto.getExtension(JsProtoBuf.callableAnnotation).orEmpty().map { nameResolver.getClassId(it.getId()) }
}
override fun loadValueParameterAnnotations(
container: ProtoContainer,
@@ -42,6 +42,7 @@ class KotlinJavascriptAnnotationAndConstantLoader(
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<AnnotationDescriptor> {
if (kind == AnnotatedCallableKind.PROPERTY_FIELD) return emptyList()
val annotations = proto.getExtension(JsProtoBuf.callableAnnotation).orEmpty()
return annotations.map { proto -> deserializer.deserializeAnnotation(proto, nameResolver) }
}