0
# Member Signatures
1
2
System for representing JVM method and field signatures used in metadata processing and reflection operations. JVM member signatures provide a way to uniquely identify and reference methods and fields using JVM descriptor strings.
3
4
## Capabilities
5
6
### JvmMemberSignature Base Class
7
8
Base sealed class for all JVM member signature types.
9
10
```kotlin { .api }
11
/**
12
* Base sealed class representing JVM member signatures
13
*/
14
sealed class JvmMemberSignature {
15
/**
16
* The name of the member (method or field name)
17
*/
18
abstract val name: String
19
20
/**
21
* The JVM descriptor string for this member
22
* For methods: (parameter_types)return_type
23
* For fields: field_type
24
*/
25
abstract val descriptor: String
26
}
27
```
28
29
### JvmMethodSignature
30
31
Data class representing JVM method signatures.
32
33
```kotlin { .api }
34
/**
35
* Represents a JVM method signature with name and descriptor
36
* @param name The method name
37
* @param descriptor The JVM method descriptor (e.g., "(Ljava/lang/String;)V")
38
*/
39
data class JvmMethodSignature(
40
override val name: String,
41
override val descriptor: String
42
) : JvmMemberSignature() {
43
/**
44
* Returns string representation in format "name descriptor"
45
*/
46
override fun toString(): String
47
}
48
```
49
50
**Usage Examples:**
51
52
```kotlin
53
import kotlin.metadata.jvm.JvmMethodSignature
54
55
// Create method signatures
56
val mainMethod = JvmMethodSignature(
57
name = "main",
58
descriptor = "([Ljava/lang/String;)V"
59
)
60
61
val stringLength = JvmMethodSignature(
62
name = "length",
63
descriptor = "()I"
64
)
65
66
val substring = JvmMethodSignature(
67
name = "substring",
68
descriptor = "(II)Ljava/lang/String;"
69
)
70
71
// Access signature components
72
println("Method name: ${mainMethod.name}") // "main"
73
println("Method descriptor: ${mainMethod.descriptor}") // "([Ljava/lang/String;)V"
74
println("Full signature: $mainMethod") // "main ([Ljava/lang/String;)V"
75
76
// Compare signatures
77
val anotherMain = JvmMethodSignature("main", "([Ljava/lang/String;)V")
78
println(mainMethod == anotherMain) // true (data class equality)
79
```
80
81
### JvmFieldSignature
82
83
Data class representing JVM field signatures.
84
85
```kotlin { .api }
86
/**
87
* Represents a JVM field signature with name and descriptor
88
* @param name The field name
89
* @param descriptor The JVM field descriptor (e.g., "Ljava/lang/String;")
90
*/
91
data class JvmFieldSignature(
92
override val name: String,
93
override val descriptor: String
94
) : JvmMemberSignature() {
95
/**
96
* Returns string representation in format "name descriptor"
97
*/
98
override fun toString(): String
99
}
100
```
101
102
**Usage Examples:**
103
104
```kotlin
105
import kotlin.metadata.jvm.JvmFieldSignature
106
107
// Create field signatures
108
val stringField = JvmFieldSignature(
109
name = "value",
110
descriptor = "Ljava/lang/String;"
111
)
112
113
val intField = JvmFieldSignature(
114
name = "count",
115
descriptor = "I"
116
)
117
118
val booleanField = JvmFieldSignature(
119
name = "isActive",
120
descriptor = "Z"
121
)
122
123
// Access signature components
124
println("Field name: ${stringField.name}") // "value"
125
println("Field descriptor: ${stringField.descriptor}") // "Ljava/lang/String;"
126
println("Full signature: $stringField") // "value Ljava/lang/String;"
127
128
// Working with primitive types
129
val primitiveFields = listOf(
130
JvmFieldSignature("byteValue", "B"), // byte
131
JvmFieldSignature("shortValue", "S"), // short
132
JvmFieldSignature("intValue", "I"), // int
133
JvmFieldSignature("longValue", "J"), // long
134
JvmFieldSignature("floatValue", "F"), // float
135
JvmFieldSignature("doubleValue", "D"), // double
136
JvmFieldSignature("charValue", "C"), // char
137
JvmFieldSignature("booleanValue", "Z") // boolean
138
)
139
```
140
141
### Working with Signatures
142
143
Common operations and patterns when working with JVM member signatures.
144
145
```kotlin
146
// Pattern matching on signature types
147
fun processSignature(signature: JvmMemberSignature) {
148
when (signature) {
149
is JvmMethodSignature -> {
150
println("Processing method: ${signature.name}")
151
152
// Parse method descriptor for parameter types
153
if (signature.descriptor.startsWith("(")) {
154
val paramEnd = signature.descriptor.indexOf(')')
155
val paramTypes = signature.descriptor.substring(1, paramEnd)
156
val returnType = signature.descriptor.substring(paramEnd + 1)
157
158
println(" Parameters: $paramTypes")
159
println(" Return type: $returnType")
160
}
161
}
162
163
is JvmFieldSignature -> {
164
println("Processing field: ${signature.name}")
165
println(" Type: ${signature.descriptor}")
166
}
167
}
168
}
169
170
// Create signature collections
171
val methodSignatures = mutableSetOf<JvmMethodSignature>()
172
methodSignatures.add(JvmMethodSignature("toString", "()Ljava/lang/String;"))
173
methodSignatures.add(JvmMethodSignature("equals", "(Ljava/lang/Object;)Z"))
174
methodSignatures.add(JvmMethodSignature("hashCode", "()I"))
175
176
val fieldSignatures = mutableSetOf<JvmFieldSignature>()
177
fieldSignatures.add(JvmFieldSignature("serialVersionUID", "J"))
178
```
179
180
### JVM Descriptor Format
181
182
Understanding JVM descriptor format for methods and fields.
183
184
#### Method Descriptors
185
186
Method descriptors have the format `(parameter_types)return_type`:
187
188
```kotlin
189
// Examples of method descriptors
190
val methodExamples = mapOf(
191
"()V" to "No parameters, void return",
192
"(I)I" to "One int parameter, int return",
193
"(Ljava/lang/String;)V" to "String parameter, void return",
194
"(II)Ljava/lang/String;" to "Two int parameters, String return",
195
"([Ljava/lang/String;)V" to "String array parameter, void return",
196
"(Ljava/util/List;I)Ljava/util/Optional;" to "List and int parameters, Optional return"
197
)
198
199
// Create signatures with complex descriptors
200
val complexMethod = JvmMethodSignature(
201
name = "processData",
202
descriptor = "(Ljava/util/Map;[Ljava/lang/String;I)Ljava/util/List;"
203
)
204
```
205
206
#### Field Descriptors
207
208
Field descriptors are simpler, just the type descriptor:
209
210
```kotlin
211
// Examples of field descriptors
212
val fieldExamples = mapOf(
213
"I" to "int",
214
"Ljava/lang/String;" to "String",
215
"[I" to "int array",
216
"[[Ljava/lang/Object;" to "2D Object array",
217
"Ljava/util/List;" to "List (raw type)",
218
"Ljava/util/Map;" to "Map (raw type)"
219
)
220
221
// Create signatures with various field types
222
val arrayField = JvmFieldSignature("items", "[Ljava/lang/String;")
223
val mapField = JvmFieldSignature("properties", "Ljava/util/Map;")
224
```
225
226
### Integration with Metadata
227
228
Using member signatures in metadata processing contexts.
229
230
```kotlin
231
// Example of using signatures in metadata analysis
232
fun analyzeClassMetadata(metadata: KotlinClassMetadata.Class) {
233
val klass = metadata.kmClass
234
235
// Collect method signatures from class metadata
236
val methodSignatures = mutableListOf<JvmMethodSignature>()
237
238
klass.functions.forEach { kmFunction ->
239
// Convert KmFunction to JvmMethodSignature
240
// (This would require additional metadata processing)
241
val signature = JvmMethodSignature(
242
name = kmFunction.name,
243
descriptor = buildMethodDescriptor(kmFunction)
244
)
245
methodSignatures.add(signature)
246
}
247
248
// Collect field signatures from properties
249
val fieldSignatures = mutableListOf<JvmFieldSignature>()
250
251
klass.properties.forEach { kmProperty ->
252
// Convert KmProperty to JvmFieldSignature
253
val signature = JvmFieldSignature(
254
name = kmProperty.name,
255
descriptor = buildFieldDescriptor(kmProperty)
256
)
257
fieldSignatures.add(signature)
258
}
259
260
println("Found ${methodSignatures.size} methods and ${fieldSignatures.size} fields")
261
}
262
263
// Helper functions (implementation would depend on kotlinx-metadata-core)
264
// These are example placeholders - actual implementation would analyze KmFunction/KmProperty
265
// to generate proper JVM descriptors based on parameter and return types
266
fun buildMethodDescriptor(kmFunction: KmFunction): String {
267
// Example: analyze kmFunction.valueParameters and kmFunction.returnType
268
// to build descriptor like "(Ljava/lang/String;I)V"
269
return "(...)" // Implementation specific to kotlinx-metadata-core
270
}
271
272
fun buildFieldDescriptor(kmProperty: KmProperty): String {
273
// Example: analyze kmProperty.returnType to build descriptor like "Ljava/lang/String;"
274
return "..." // Implementation specific to kotlinx-metadata-core
275
}
276
```
277
278
## Use Cases
279
280
JVM member signatures are commonly used for:
281
282
- **Reflection operations**: Identifying methods and fields for dynamic invocation
283
- **Code analysis tools**: Tracking API changes and method usage
284
- **Serialization frameworks**: Mapping between Kotlin properties and JVM fields
285
- **Bytecode manipulation**: Precise method and field targeting in ASM or similar libraries
286
- **IDE tooling**: Method resolution and code completion features