Support type use annotations on fields

This commit is contained in:
Victor Petukhov
2020-12-08 15:48:41 +03:00
parent b0debbe4c9
commit c91301d04c
2 changed files with 32 additions and 14 deletions
@@ -25,6 +25,35 @@ import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.org.objectweb.asm.*
import java.lang.reflect.Array
internal class AnnotationsCollectorFieldVisitor(
private val field: BinaryJavaField,
private val context: ClassifierResolutionContext,
private val signatureParser: BinaryClassSignatureParser,
) : FieldVisitor(ASM_API_VERSION_FOR_CLASS_READING) {
override fun visitAnnotation(desc: String, visible: Boolean) =
BinaryJavaAnnotation.addAnnotation(field, desc, context, signatureParser)
override fun visitTypeAnnotation(typeRef: Int, typePath: TypePath?, desc: String, visible: Boolean): AnnotationVisitor? {
val typeReference = TypeReference(typeRef)
if (typePath != null) {
val translatedPath = translatePath(typePath)
when (typeReference.sort) {
TypeReference.FIELD -> {
val targetType = computeTargetType(field.type, translatedPath)
return BinaryJavaAnnotation.addAnnotation(targetType as JavaPlainType, desc, context, signatureParser)
}
}
}
return when (typeReference.sort) {
TypeReference.FIELD -> BinaryJavaAnnotation.addAnnotation(field.type as JavaPlainType, desc, context, signatureParser)
else -> null
}
}
}
internal class AnnotationsAndParameterCollectorMethodVisitor(
private val member: BinaryJavaMethodBase,
private val context: ClassifierResolutionContext,
@@ -211,23 +211,12 @@ class BinaryJavaClass(
if (access.isSet(Opcodes.ACC_SYNTHETIC)) return null
val type = signatureParser.parseTypeString(StringCharacterIterator(signature ?: desc), context)
val processedValue = processValue(value, type)
val filed = BinaryJavaField(Name.identifier(name), access, this, access.isSet(Opcodes.ACC_ENUM), type, processedValue)
return BinaryJavaField(Name.identifier(name), access, this, access.isSet(Opcodes.ACC_ENUM), type, processedValue).run {
fields.add(this)
fields.add(filed)
object : FieldVisitor(ASM_API_VERSION_FOR_CLASS_READING) {
override fun visitAnnotation(desc: String, visible: Boolean) =
BinaryJavaAnnotation.addAnnotation(this@run, desc, context, signatureParser)
override fun visitTypeAnnotation(typeRef: Int, typePath: TypePath?, desc: String, visible: Boolean) =
if (typePath == null)
BinaryJavaAnnotation.addAnnotation(type as JavaPlainType, desc, context, signatureParser)
else
null
}
}
return AnnotationsCollectorFieldVisitor(filed, context, signatureParser)
}