[refactoring] extract base class for annotation loaders
Annotation loaders for descriptors (AnnotationAndConstantLoaderImpl) and stubs (AnnotationLoaderForStubBuilderImpl) share the loading logic until mapping ProtoBuf.Annotations to output values. The shared logic has been extracted to the base class. KTIJ-26761 KTIJ-26961
This commit is contained in:
committed by
Space Team
parent
30d45039fb
commit
2d10877fda
+2
-82
@@ -8,93 +8,13 @@ package org.jetbrains.kotlin.analysis.decompiler.stub.file
|
||||
import org.jetbrains.kotlin.analysis.decompiler.stub.AnnotationWithArgs
|
||||
import org.jetbrains.kotlin.metadata.ProtoBuf
|
||||
import org.jetbrains.kotlin.metadata.deserialization.NameResolver
|
||||
import org.jetbrains.kotlin.protobuf.MessageLite
|
||||
import org.jetbrains.kotlin.psi.stubs.impl.createConstantValue
|
||||
import org.jetbrains.kotlin.serialization.SerializerExtensionProtocol
|
||||
import org.jetbrains.kotlin.serialization.deserialization.*
|
||||
|
||||
class AnnotationLoaderForStubBuilderImpl(
|
||||
private val protocol: SerializerExtensionProtocol
|
||||
) : AnnotationLoader<AnnotationWithArgs> {
|
||||
|
||||
override fun loadClassAnnotations(container: ProtoContainer.Class): List<AnnotationWithArgs> =
|
||||
container.classProto.getExtension(protocol.classAnnotation).orEmpty()
|
||||
.map { loadAnnotation(it, container.nameResolver) }
|
||||
|
||||
override fun loadCallableAnnotations(
|
||||
container: ProtoContainer,
|
||||
proto: MessageLite,
|
||||
kind: AnnotatedCallableKind
|
||||
): List<AnnotationWithArgs> {
|
||||
val annotations = when (proto) {
|
||||
is ProtoBuf.Constructor -> proto.getExtension(protocol.constructorAnnotation)
|
||||
is ProtoBuf.Function -> proto.getExtension(protocol.functionAnnotation)
|
||||
is ProtoBuf.Property -> when (kind) {
|
||||
AnnotatedCallableKind.PROPERTY -> proto.getExtension(protocol.propertyAnnotation)
|
||||
AnnotatedCallableKind.PROPERTY_GETTER -> proto.getExtension(protocol.propertyGetterAnnotation)
|
||||
AnnotatedCallableKind.PROPERTY_SETTER -> proto.getExtension(protocol.propertySetterAnnotation)
|
||||
else -> error("Unsupported callable kind with property proto")
|
||||
}
|
||||
else -> error("Unknown message: $proto")
|
||||
}.orEmpty()
|
||||
return annotations.map { loadAnnotation(it, container.nameResolver) }
|
||||
}
|
||||
|
||||
override fun loadPropertyBackingFieldAnnotations(container: ProtoContainer, proto: ProtoBuf.Property): List<AnnotationWithArgs> {
|
||||
val annotations = protocol.propertyBackingFieldAnnotation?.let { proto.getExtension(it) }.orEmpty()
|
||||
return annotations.map { annotationProto ->
|
||||
loadAnnotation(annotationProto, container.nameResolver)
|
||||
}
|
||||
}
|
||||
|
||||
override fun loadPropertyDelegateFieldAnnotations(container: ProtoContainer, proto: ProtoBuf.Property): List<AnnotationWithArgs> {
|
||||
val annotations = protocol.propertyDelegatedFieldAnnotation?.let {proto.getExtension(it) }.orEmpty()
|
||||
return annotations.map { annotationProto ->
|
||||
loadAnnotation(annotationProto, container.nameResolver)
|
||||
}
|
||||
}
|
||||
|
||||
override fun loadEnumEntryAnnotations(container: ProtoContainer, proto: ProtoBuf.EnumEntry): List<AnnotationWithArgs> =
|
||||
proto.getExtension(protocol.enumEntryAnnotation).orEmpty().map { loadAnnotation(it, container.nameResolver) }
|
||||
|
||||
override fun loadValueParameterAnnotations(
|
||||
container: ProtoContainer,
|
||||
callableProto: MessageLite,
|
||||
kind: AnnotatedCallableKind,
|
||||
parameterIndex: Int,
|
||||
proto: ProtoBuf.ValueParameter
|
||||
): List<AnnotationWithArgs> =
|
||||
proto.getExtension(protocol.parameterAnnotation).orEmpty().map { loadAnnotation(it, container.nameResolver) }
|
||||
|
||||
override fun loadExtensionReceiverParameterAnnotations(
|
||||
container: ProtoContainer,
|
||||
proto: MessageLite,
|
||||
kind: AnnotatedCallableKind
|
||||
): List<AnnotationWithArgs> {
|
||||
val annotations = when (proto) {
|
||||
is ProtoBuf.Function -> protocol.functionExtensionReceiverAnnotation?.let { proto.getExtension(it) }
|
||||
is ProtoBuf.Property -> when (kind) {
|
||||
AnnotatedCallableKind.PROPERTY, AnnotatedCallableKind.PROPERTY_GETTER, AnnotatedCallableKind.PROPERTY_SETTER -> {
|
||||
protocol.propertyExtensionReceiverAnnotation?.let { proto.getExtension(it) }
|
||||
}
|
||||
else -> error("Unsupported callable kind with property proto for receiver annotations: $kind")
|
||||
}
|
||||
else -> error("Unknown message: $proto")
|
||||
}.orEmpty()
|
||||
return annotations.map { annotationProto ->
|
||||
loadAnnotation(annotationProto, container.nameResolver)
|
||||
}
|
||||
}
|
||||
|
||||
override fun loadTypeAnnotations(
|
||||
proto: ProtoBuf.Type,
|
||||
nameResolver: NameResolver
|
||||
): List<AnnotationWithArgs> =
|
||||
proto.getExtension(protocol.typeAnnotation).orEmpty().map { loadAnnotation(it, nameResolver) }
|
||||
|
||||
override fun loadTypeParameterAnnotations(proto: ProtoBuf.TypeParameter, nameResolver: NameResolver): List<AnnotationWithArgs> =
|
||||
proto.getExtension(protocol.typeParameterAnnotation).orEmpty().map { loadAnnotation(it, nameResolver) }
|
||||
|
||||
protocol: SerializerExtensionProtocol,
|
||||
) : AbstractAnnotationLoader<AnnotationWithArgs>(protocol) {
|
||||
override fun loadAnnotation(proto: ProtoBuf.Annotation, nameResolver: NameResolver): AnnotationWithArgs {
|
||||
val valueMap = proto.argumentList.associate { nameResolver.getName(it.nameId) to createConstantValue(it.value, nameResolver) }
|
||||
return AnnotationWithArgs(nameResolver.getClassId(proto.id), valueMap)
|
||||
|
||||
Reference in New Issue
Block a user