@@ -7,6 +7,7 @@ package kotlinx.validation.api
77
88import kotlinx.metadata.jvm.*
99import kotlinx.validation.*
10+ import org.objectweb.asm.Opcodes
1011import org.objectweb.asm.tree.*
1112
1213@ExternalApi // Only name is part of the API, nothing else is used by stdlib
@@ -48,7 +49,8 @@ data class MethodBinarySignature(
4849 override val jvmMember : JvmMethodSignature ,
4950 override val isPublishedApi : Boolean ,
5051 override val access : AccessFlags ,
51- override val annotations : List <AnnotationNode >
52+ override val annotations : List <AnnotationNode >,
53+ private val alternateDefaultSignature : JvmMethodSignature ?
5254) : MemberBinarySignature {
5355 override val signature: String
5456 get() = " ${access.getModifierString()} fun $name $desc "
@@ -59,60 +61,51 @@ data class MethodBinarySignature(
5961 && ! isDummyDefaultConstructor()
6062
6163 override fun findMemberVisibility (classVisibility : ClassVisibility ? ): MemberVisibility ? {
62- return super .findMemberVisibility(classVisibility) ? : classVisibility?.let { alternateDefaultSignature(it.name)?.let (it::findMember) }
64+ return super .findMemberVisibility(classVisibility)
65+ ? : classVisibility?.let { alternateDefaultSignature?.let (it::findMember) }
6366 }
6467
65- /* *
66- * Checks whether the method is a $default counterpart of internal @PublishedApi method
67- */
68- public fun isPublishedApiWithDefaultArguments (
69- classVisibility : ClassVisibility ? ,
70- publishedApiSignatures : Set <JvmMethodSignature >
71- ): Boolean {
72- // Fast-path
73- findMemberVisibility(classVisibility)?.isInternal() ? : return false
74- val name = jvmMember.name
75- if (! name.endsWith(" \$ default" )) return false
76- // Leverage the knowledge about modified signature
77- val expectedPublishedApiCounterPart = JvmMethodSignature (
78- name.removeSuffix(" \$ default" ),
79- jvmMember.desc.replace( " ;ILjava/lang/Object;)" , " ;)" ))
80- return expectedPublishedApiCounterPart in publishedApiSignatures
81- }
68+ private fun isAccessOrAnnotationsMethod () =
69+ access.isSynthetic && (name.startsWith(" access\$ " ) || name.endsWith(" \$ annotations" ))
8270
83- private fun isAccessOrAnnotationsMethod () = access.isSynthetic && (name.startsWith(" access\$ " ) || name.endsWith(" \$ annotations" ))
84-
85- private fun isDummyDefaultConstructor () = access.isSynthetic && name == " <init>" && desc == " (Lkotlin/jvm/internal/DefaultConstructorMarker;)V"
86-
87- /* *
88- * Calculates the signature of this method without default parameters
89- *
90- * Returns `null` if this method isn't an entry point of a function
91- * or a constructor with default parameters.
92- * Returns an incorrect result, if there are more than 31 default parameters.
93- */
94- private fun alternateDefaultSignature (className : String ): JvmMethodSignature ? {
95- return when {
96- ! access.isSynthetic -> null
97- name == " <init>" && " ILkotlin/jvm/internal/DefaultConstructorMarker;" in desc ->
98- JvmMethodSignature (name, desc.replace(" ILkotlin/jvm/internal/DefaultConstructorMarker;" , " " ))
99- name.endsWith(" \$ default" ) && " ILjava/lang/Object;)" in desc ->
100- JvmMethodSignature (
101- name.removeSuffix(" \$ default" ),
102- desc.replace(" ILjava/lang/Object;)" , " )" ).replace(" (L$className ;" , " (" )
103- )
104- else -> null
105- }
71+ private fun isDummyDefaultConstructor () =
72+ access.isSynthetic && name == " <init>" && desc == " (Lkotlin/jvm/internal/DefaultConstructorMarker;)V"
73+ }
74+
75+ /* *
76+ * Calculates the signature of this method without default parameters
77+ *
78+ * Returns `null` if this method isn't an entry point of a function
79+ * or a constructor with default parameters.
80+ * Returns an incorrect result, if there are more than 31 default parameters.
81+ */
82+ internal fun MethodNode.alternateDefaultSignature (className : String ): JvmMethodSignature ? {
83+ return when {
84+ access and Opcodes .ACC_SYNTHETIC == 0 -> null
85+ name == " <init>" && " ILkotlin/jvm/internal/DefaultConstructorMarker;" in desc ->
86+ JvmMethodSignature (name, desc.replace(" ILkotlin/jvm/internal/DefaultConstructorMarker;" , " " ))
87+ name.endsWith(" \$ default" ) && " ILjava/lang/Object;)" in desc ->
88+ JvmMethodSignature (
89+ name.removeSuffix(" \$ default" ),
90+ desc.replace(" ILjava/lang/Object;)" , " )" ).replace(" (L$className ;" , " (" )
91+ )
92+ else -> null
10693 }
10794}
10895
109- fun MethodNode.toMethodBinarySignature (propertyAnnotations : List <AnnotationNode >) =
110- MethodBinarySignature (
96+ fun MethodNode.toMethodBinarySignature (
97+ extraAnnotations : List <AnnotationNode >,
98+ alternateDefaultSignature : JvmMethodSignature ?
99+ ): MethodBinarySignature {
100+ val allAnnotations = visibleAnnotations.orEmpty() + invisibleAnnotations.orEmpty() + extraAnnotations
101+ return MethodBinarySignature (
111102 JvmMethodSignature (name, desc),
112- isPublishedApi(),
103+ allAnnotations. isPublishedApi(),
113104 AccessFlags (access),
114- visibleAnnotations.orEmpty() + invisibleAnnotations.orEmpty() + propertyAnnotations
105+ allAnnotations,
106+ alternateDefaultSignature
115107 )
108+ }
116109
117110data class FieldBinarySignature (
118111 override val jvmMember : JvmFieldSignature ,
@@ -129,13 +122,15 @@ data class FieldBinarySignature(
129122 }
130123}
131124
132- fun FieldNode.toFieldBinarySignature () =
133- FieldBinarySignature (
125+ fun FieldNode.toFieldBinarySignature (extraAnnotations : List <AnnotationNode >): FieldBinarySignature {
126+ val allAnnotations = visibleAnnotations.orEmpty() + invisibleAnnotations.orEmpty() + extraAnnotations
127+ return FieldBinarySignature (
134128 JvmFieldSignature (name, desc),
135- isPublishedApi(),
129+ allAnnotations. isPublishedApi(),
136130 AccessFlags (access),
137- annotations(visibleAnnotations, invisibleAnnotations))
138-
131+ allAnnotations
132+ )
133+ }
139134private val MemberBinarySignature .kind: Int
140135 get() = when (this ) {
141136 is FieldBinarySignature -> 1
@@ -156,7 +151,9 @@ data class AccessFlags(val access: Int) {
156151 val isFinal: Boolean get() = isFinal(access)
157152 val isSynthetic: Boolean get() = isSynthetic(access)
158153
159- fun getModifiers (): List <String > = ACCESS_NAMES .entries.mapNotNull { if (access and it.key != 0 ) it.value else null }
154+ fun getModifiers (): List <String > =
155+ ACCESS_NAMES .entries.mapNotNull { if (access and it.key != 0 ) it.value else null }
156+
160157 fun getModifierString (): String = getModifiers().joinToString(" " )
161158}
162159
0 commit comments