@@ -21,34 +21,6 @@ fun Element.typeAnnotatedWith(className: String) =
21
21
it.annotationType.asElement().hasAnnotation(className)
22
22
}?.annotationType?.asElement() as ? TypeElement
23
23
24
- fun TypeName.javaToKotlinType (): TypeName = if (this is ParameterizedTypeName ) {
25
- (rawType.javaToKotlinType() as ClassName ).parameterizedBy(
26
- * typeArguments.map { it.javaToKotlinType() }.toTypedArray()
27
- )
28
- } else if (this is WildcardTypeName ) {
29
- if (inTypes.isNotEmpty()) {
30
- WildcardTypeName .consumerOf(inTypes.first().javaToKotlinType())
31
- } else {
32
- WildcardTypeName .producerOf(outTypes.first().javaToKotlinType())
33
- }
34
- } else {
35
- val s = toString()
36
- when (s) {
37
- " java.util.Map" -> ClassName .bestGuess(" kotlin.collections.Map" )
38
- " java.util.Set" -> ClassName .bestGuess(" kotlin.collections.Set" )
39
- " java.lang.String" -> ClassName .bestGuess(" kotlin.String" )
40
- else -> {
41
- val regex = Regex (" kotlin\\ .jvm\\ .functions\\ .(Function[0-9]+)" )
42
- val result = regex.matchEntire(s)
43
- if (result != null ) {
44
- ClassName .bestGuess(" kotlin.${result.groupValues[1 ]} " )
45
- } else {
46
- this
47
- }
48
- }
49
- }
50
- }
51
-
52
24
val TypeElement .metadata: KotlinClassMetadata ?
53
25
get() {
54
26
val meta = getAnnotation(Metadata ::class .java) ? : return null
@@ -168,101 +140,41 @@ private fun ExecutableElement.nameMatches(signature: JvmMemberSignature?): Boole
168
140
return simpleName.contentEquals(signature.name)
169
141
}
170
142
171
- fun DeclaredType.rawTypeName (kmType : KmType ? ): ClassName {
172
- val className =
173
- (kmType?.classifier as ? KmClassifier .Class )?.asClassName()
174
- ? : (this .asElement() as TypeElement ).asClassName()
175
- return className.kotlinClassName()
143
+ fun KmClassifier.asClassName () = when (this ) {
144
+ is KmClassifier .Class -> name.asClassName()
145
+ is KmClassifier .TypeAlias -> name.asClassName()
146
+ is KmClassifier .TypeParameter -> null
176
147
}
177
148
178
- fun KmClassifier.Class.asClassName () = ClassName .bestGuess(name.replace(' /' , ' .' ))
179
-
180
- fun ClassName.kotlinClassName (): ClassName {
181
- return when (canonicalName) {
182
- " java.util.Map" -> ClassName (" kotlin.collections" , " Map" )
183
- " java.util.Set" -> ClassName (" kotlin.collections" , " Set" )
184
- " java.lang.String" -> ClassName (" kotlin" , " String" )
185
- else -> this
149
+ fun kotlinx.metadata.ClassName.asClassName (): ClassName {
150
+ val split = lastIndexOf(' /' )
151
+ return if (split == - 1 ) {
152
+ ClassName (" " , this )
153
+ } else {
154
+ ClassName (substring(0 , split).replace(' /' , ' .' ), substring(split + 1 ))
186
155
}
187
156
}
188
157
189
158
fun TypeMirror.asTypeName (kmType : KmType ? ): TypeName {
190
- return accept(object : SimpleTypeVisitor7 <TypeName , Void ?>() {
191
- override fun visitPrimitive (t : PrimitiveType , p : Void ? ): TypeName {
192
- return when (t.kind) {
193
- TypeKind .BOOLEAN -> BOOLEAN
194
- TypeKind .BYTE -> BYTE
195
- TypeKind .SHORT -> SHORT
196
- TypeKind .INT -> INT
197
- TypeKind .LONG -> LONG
198
- TypeKind .CHAR -> CHAR
199
- TypeKind .FLOAT -> FLOAT
200
- TypeKind .DOUBLE -> DOUBLE
201
- else -> throw AssertionError ()
202
- }
203
- }
204
-
205
- override fun visitDeclared (t : DeclaredType , p : Void ? ): TypeName {
206
- val isNullable = kmType?.let { Flag .Type .IS_NULLABLE (it.flags) } == true
207
- val rawType: ClassName = t.rawTypeName(kmType).copy(nullable = isNullable) as ClassName
208
- val enclosingType = t.enclosingType
209
- val enclosing = if (enclosingType.kind != TypeKind .NONE &&
210
- Modifier .STATIC !in t.asElement().modifiers
211
- )
212
- enclosingType.accept(this , null ) else
213
- null
214
- if (t.typeArguments.isEmpty() && enclosing !is ParameterizedTypeName ) {
215
- return rawType
216
- }
217
-
218
- val typeArgumentNames = mutableListOf<TypeName >()
219
- for (i in 0 until t.typeArguments.size) {
220
- val typeArg = t.typeArguments[i]
221
- val kmArg = kmType?.arguments?.getOrNull(i)?.type
222
- typeArgumentNames + = typeArg.asTypeName(kmArg)
223
- }
224
-
225
- return if (enclosing is ParameterizedTypeName )
226
- enclosing.nestedClass(rawType.simpleName, typeArgumentNames) else
227
- rawType.parameterizedBy(typeArgumentNames)
159
+ if (kmType != null ) {
160
+ val typeName = kmType.asTypeName()
161
+ if (typeName != null ) {
162
+ return typeName
228
163
}
229
-
230
- override fun visitError (t : ErrorType , p : Void ? ): TypeName {
231
- return visitDeclared(t, p)
232
- }
233
-
234
- override fun visitArray (t : ArrayType , p : Void ? ): TypeName {
235
- return when (t.componentType.kind) {
236
- TypeKind .BOOLEAN -> BOOLEAN_ARRAY
237
- TypeKind .BYTE -> BYTE_ARRAY
238
- TypeKind .SHORT -> SHORT_ARRAY
239
- TypeKind .INT -> INT_ARRAY
240
- TypeKind .LONG -> LONG_ARRAY
241
- TypeKind .CHAR -> CHAR_ARRAY
242
- TypeKind .FLOAT -> FLOAT_ARRAY
243
- TypeKind .DOUBLE -> DOUBLE_ARRAY
244
- else -> ARRAY .parameterizedBy(t.componentType.asTypeName(kmType?.arguments?.getOrNull(0 )?.type))
245
- }
246
- }
247
-
248
- override fun visitTypeVariable (
249
- t : TypeVariable ,
250
- p : Void ?
251
- ): TypeName {
252
- return t.asTypeVariableName()
253
- }
254
-
255
- override fun visitWildcard (t : WildcardType , p : Void ? ): TypeName {
256
- return t.asWildcardTypeName()
257
- }
258
-
259
- override fun visitNoType (t : NoType , p : Void ? ): TypeName {
260
- if (t.kind == TypeKind .VOID ) return UNIT
261
- return super .visitUnknown(t, p)
262
- }
263
-
264
- override fun defaultAction (e : TypeMirror ? , p : Void ? ): TypeName {
265
- throw IllegalArgumentException (" Unexpected type mirror: " + e!! )
266
- }
267
- }, null )
164
+ }
165
+ return asTypeName()
268
166
}
167
+
168
+ fun KmType.asTypeName (): TypeName ? {
169
+ val abbreviatedType = abbreviatedType
170
+ if (abbreviatedType != null ) {
171
+ return abbreviatedType.asTypeName()
172
+ }
173
+ val isNullable = Flag .Type .IS_NULLABLE (flags)
174
+ val className = classifier.asClassName() ? : return null
175
+ return if (arguments.isEmpty()) {
176
+ className
177
+ } else {
178
+ className.parameterizedBy(arguments.map { it.type!! .asTypeName()!! })
179
+ }.copy(nullable = isNullable)
180
+ }
0 commit comments