jvm-abi-gen: Reformat files and add comments
This commit is contained in:
committed by
Alexander Udalov
parent
023db4f376
commit
fc6c2c9631
+33
-12
@@ -65,14 +65,17 @@ sealed class AbiClassInfo {
|
||||
class JvmAbiClassBuilderInterceptor : ClassBuilderInterceptorExtension {
|
||||
val abiClassInfo: MutableMap<String, AbiClassInfo> = mutableMapOf()
|
||||
|
||||
override fun interceptClassBuilderFactory(interceptedFactory: ClassBuilderFactory, bindingContext: BindingContext, diagnostics: DiagnosticSink): ClassBuilderFactory =
|
||||
object : DelegatingClassBuilderFactory(interceptedFactory) {
|
||||
override fun newClassBuilder(origin: JvmDeclarationOrigin): DelegatingClassBuilder {
|
||||
val descriptor = origin.descriptor as? ClassDescriptor
|
||||
val isPrivate = descriptor?.visibility?.let(DescriptorVisibilities::isPrivate) ?: false
|
||||
return AbiInfoClassBuilder(interceptedFactory.newClassBuilder(origin), isPrivate)
|
||||
}
|
||||
override fun interceptClassBuilderFactory(
|
||||
interceptedFactory: ClassBuilderFactory,
|
||||
bindingContext: BindingContext,
|
||||
diagnostics: DiagnosticSink
|
||||
): ClassBuilderFactory = object : DelegatingClassBuilderFactory(interceptedFactory) {
|
||||
override fun newClassBuilder(origin: JvmDeclarationOrigin): DelegatingClassBuilder {
|
||||
val descriptor = origin.descriptor as? ClassDescriptor
|
||||
val isPrivate = descriptor?.visibility?.let(DescriptorVisibilities::isPrivate) ?: false
|
||||
return AbiInfoClassBuilder(interceptedFactory.newClassBuilder(origin), isPrivate)
|
||||
}
|
||||
}
|
||||
|
||||
private inner class AbiInfoClassBuilder(
|
||||
private val delegate: ClassBuilder,
|
||||
@@ -86,8 +89,17 @@ class JvmAbiClassBuilderInterceptor : ClassBuilderInterceptorExtension {
|
||||
|
||||
override fun getDelegate(): ClassBuilder = delegate
|
||||
|
||||
override fun defineClass(origin: PsiElement?, version: Int, access: Int, name: String, signature: String?, superName: String, interfaces: Array<out String>) {
|
||||
override fun defineClass(
|
||||
origin: PsiElement?,
|
||||
version: Int,
|
||||
access: Int,
|
||||
name: String,
|
||||
signature: String?,
|
||||
superName: String,
|
||||
interfaces: Array<out String>
|
||||
) {
|
||||
// Always keep annotation classes
|
||||
// TODO: Investigate whether there are cases where we can remove annotation classes from the ABI.
|
||||
if (access and Opcodes.ACC_ANNOTATION != 0) {
|
||||
publicAbi = true
|
||||
}
|
||||
@@ -101,7 +113,14 @@ class JvmAbiClassBuilderInterceptor : ClassBuilderInterceptorExtension {
|
||||
super.visitOuterClass(owner, name, desc)
|
||||
}
|
||||
|
||||
override fun newMethod(origin: JvmDeclarationOrigin, access: Int, name: String, desc: String, signature: String?, exceptions: Array<out String>?): MethodVisitor {
|
||||
override fun newMethod(
|
||||
origin: JvmDeclarationOrigin,
|
||||
access: Int,
|
||||
name: String,
|
||||
desc: String,
|
||||
signature: String?,
|
||||
exceptions: Array<out String>?
|
||||
): MethodVisitor {
|
||||
if (publicAbi) {
|
||||
return super.newMethod(origin, access, name, desc, signature, exceptions)
|
||||
}
|
||||
@@ -121,8 +140,10 @@ class JvmAbiClassBuilderInterceptor : ClassBuilderInterceptorExtension {
|
||||
|
||||
// Remove private functions from the ABI jars
|
||||
val descriptor = origin.descriptor as? MemberDescriptor
|
||||
if (access and Opcodes.ACC_PRIVATE != 0 && descriptor?.visibility?.let(DescriptorVisibilities::isPrivate) == true
|
||||
|| name == "<clinit>" || name.startsWith("access\$") && access and Opcodes.ACC_SYNTHETIC != 0) {
|
||||
if (
|
||||
access and Opcodes.ACC_PRIVATE != 0 && descriptor?.visibility?.let(DescriptorVisibilities::isPrivate) == true
|
||||
|| name == "<clinit>" || name.startsWith("access\$") && access and Opcodes.ACC_SYNTHETIC != 0
|
||||
) {
|
||||
return super.newMethod(origin, access, name, desc, signature, exceptions)
|
||||
}
|
||||
|
||||
@@ -153,7 +174,7 @@ class JvmAbiClassBuilderInterceptor : ClassBuilderInterceptorExtension {
|
||||
|
||||
override fun done() {
|
||||
// Remove local or anonymous classes unless they are in the scope of an inline function and
|
||||
// strip non-inline methods from all other classe.
|
||||
// strip non-inline methods from all other classes.
|
||||
when {
|
||||
publicAbi ->
|
||||
abiClassInfo[internalName] = AbiClassInfo.Public
|
||||
|
||||
@@ -22,8 +22,12 @@ fun abiMetadataProcessor(annotationVisitor: AnnotationVisitor): AnnotationVisito
|
||||
kotlinClassHeaderVisitor { header ->
|
||||
// kotlinx-metadata only supports writing Kotlin metadata of version >= 1.4, so we need to
|
||||
// update the metadata version if we encounter older metadata annotations.
|
||||
val metadataVersion =
|
||||
if (header.metadataVersion.let { it.size >= 2 && it[0] >= 1 && it[1] >= 4 }) header.metadataVersion else intArrayOf(1, 4)
|
||||
val metadataVersion = header.metadataVersion.takeIf { v ->
|
||||
val major = v.getOrNull(0) ?: 0
|
||||
val minor = v.getOrNull(1) ?: 0
|
||||
major > 1 || major == 1 && minor >= 4
|
||||
} ?: intArrayOf(1, 4)
|
||||
|
||||
val newHeader = when (val metadata = KotlinClassMetadata.read(header)) {
|
||||
is KotlinClassMetadata.Class -> {
|
||||
val writer = KotlinClassMetadata.Class.Writer()
|
||||
@@ -70,30 +74,31 @@ private fun kotlinClassHeaderVisitor(body: (KotlinClassHeader) -> Unit): Annotat
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitArray(name: String): AnnotationVisitor {
|
||||
override fun visitArray(name: String): AnnotationVisitor? {
|
||||
val destination = when (name) {
|
||||
METADATA_DATA_FIELD_NAME -> data1
|
||||
METADATA_STRINGS_FIELD_NAME -> data2
|
||||
else -> error("Wrong array field in kotlin metadata: $name")
|
||||
else -> return null
|
||||
}
|
||||
return object : AnnotationVisitor(Opcodes.ASM7) {
|
||||
return object : AnnotationVisitor(Opcodes.API_VERSION) {
|
||||
override fun visit(name: String?, value: Any?) {
|
||||
destination += value as String
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun visitEnd() {
|
||||
body(KotlinClassHeader(
|
||||
kind,
|
||||
metadataVersion,
|
||||
data1.toTypedArray(),
|
||||
data2.toTypedArray(),
|
||||
extraString,
|
||||
packageName,
|
||||
extraInt
|
||||
))
|
||||
body(
|
||||
KotlinClassHeader(
|
||||
kind,
|
||||
metadataVersion,
|
||||
data1.toTypedArray(),
|
||||
data2.toTypedArray(),
|
||||
extraString,
|
||||
packageName,
|
||||
extraInt
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +163,8 @@ private class AbiKmClassVisitor(delegate: KmClassVisitor) : KmClassVisitor(deleg
|
||||
}
|
||||
|
||||
/**
|
||||
* Class metadata adapter which removes private functions, properties, and type aliases.
|
||||
* Class metadata adapter which removes private functions, properties, type aliases,
|
||||
* and local delegated properties.
|
||||
*/
|
||||
private class AbiKmPackageVisitor(delegate: KmPackageVisitor) : KmPackageVisitor(delegate) {
|
||||
override fun visitFunction(flags: Flags, name: String): KmFunctionVisitor? {
|
||||
|
||||
Reference in New Issue
Block a user