@@ -7,6 +7,7 @@ package kotlinx.validation.api
7
7
8
8
import kotlinx.metadata.jvm.*
9
9
import kotlinx.validation.*
10
+ import org.objectweb.asm.Opcodes
10
11
import org.objectweb.asm.tree.*
11
12
12
13
@ExternalApi // Only name is part of the API, nothing else is used by stdlib
@@ -48,7 +49,8 @@ data class MethodBinarySignature(
48
49
override val jvmMember : JvmMethodSignature ,
49
50
override val isPublishedApi : Boolean ,
50
51
override val access : AccessFlags ,
51
- override val annotations : List <AnnotationNode >
52
+ override val annotations : List <AnnotationNode >,
53
+ private val alternateDefaultSignature : JvmMethodSignature ?
52
54
) : MemberBinarySignature {
53
55
override val signature: String
54
56
get() = " ${access.getModifierString()} fun $name $desc "
@@ -59,60 +61,51 @@ data class MethodBinarySignature(
59
61
&& ! isDummyDefaultConstructor()
60
62
61
63
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) }
63
66
}
64
67
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" ))
82
70
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
106
93
}
107
94
}
108
95
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 (
111
102
JvmMethodSignature (name, desc),
112
- isPublishedApi(),
103
+ allAnnotations. isPublishedApi(),
113
104
AccessFlags (access),
114
- visibleAnnotations.orEmpty() + invisibleAnnotations.orEmpty() + propertyAnnotations
105
+ allAnnotations,
106
+ alternateDefaultSignature
115
107
)
108
+ }
116
109
117
110
data class FieldBinarySignature (
118
111
override val jvmMember : JvmFieldSignature ,
@@ -129,13 +122,15 @@ data class FieldBinarySignature(
129
122
}
130
123
}
131
124
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 (
134
128
JvmFieldSignature (name, desc),
135
- isPublishedApi(),
129
+ allAnnotations. isPublishedApi(),
136
130
AccessFlags (access),
137
- annotations(visibleAnnotations, invisibleAnnotations))
138
-
131
+ allAnnotations
132
+ )
133
+ }
139
134
private val MemberBinarySignature .kind: Int
140
135
get() = when (this ) {
141
136
is FieldBinarySignature -> 1
@@ -156,7 +151,9 @@ data class AccessFlags(val access: Int) {
156
151
val isFinal: Boolean get() = isFinal(access)
157
152
val isSynthetic: Boolean get() = isSynthetic(access)
158
153
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
+
160
157
fun getModifierString (): String = getModifiers().joinToString(" " )
161
158
}
162
159
0 commit comments