Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 636e15f

Browse files
committed
Kotlin: Split extractClass into extractClassSource, extractClassInstance
1 parent 9eadbea commit 636e15f

1 file changed

Lines changed: 63 additions & 34 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
219219

220220
fun extractDeclaration(declaration: IrDeclaration) {
221221
when (declaration) {
222-
is IrClass -> extractClass(declaration, listOf())
222+
is IrClass -> extractClassSource(declaration)
223223
is IrFunction -> extractFunction(declaration)
224224
is IrAnonymousInitializer -> {
225225
// Leaving this intentionally empty. init blocks are extracted during class extraction.
@@ -421,42 +421,21 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
421421
// If this is a generic type instantiation then it has no
422422
// source entity, so we need to extract it here
423423
if (typeArgs.isNotEmpty()) {
424-
extractClass(c, typeArgs)
424+
extractClassInstance(c, typeArgs)
425425
}
426426
// we don't have an "external dependencies" extractor yet,
427427
// so for now we extract thr source class for those too
428428
if (c.origin == IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB ||
429429
c.origin == IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB) {
430-
extractClass(c, listOf())
430+
extractClassSource(c)
431431
}
432432
})
433433
}
434434

435-
fun extractClass(c: IrClass, typeArgs: List<IrTypeArgument>): Label<out DbClassorinterface> {
436-
val id = addClassLabel(c, typeArgs)
437-
val pkg = c.packageFqName?.asString() ?: ""
438-
val cls = c.name.asString()
439-
val pkgId = extractPackage(pkg)
440-
if(c.kind == ClassKind.INTERFACE) {
441-
@Suppress("UNCHECKED_CAST")
442-
val interfaceId = id as Label<out DbInterface>
443-
tw.writeInterfaces(interfaceId, cls, pkgId, interfaceId)
444-
} else {
445-
@Suppress("UNCHECKED_CAST")
446-
val classId = id as Label<out DbClass>
447-
tw.writeClasses(classId, cls, pkgId, classId)
448-
449-
if (c.kind == ClassKind.ENUM_CLASS) {
450-
tw.writeIsEnumType(classId)
451-
}
452-
}
435+
fun extractClassCommon(c: IrClass, id: Label<out DbClassorinterface>) {
453436
val locId = tw.getLocation(c)
454437
tw.writeHasLocation(id, locId)
455438

456-
if (typeArgs.isEmpty()) {
457-
c.typeParameters.map { extractTypeParameter(it) }
458-
}
459-
460439
for(t in c.superTypes) {
461440
when(t) {
462441
is IrSimpleType -> {
@@ -475,20 +454,70 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
475454
}
476455
}
477456
}
457+
}
478458

479-
if (typeArgs.isNotEmpty()) {
480-
for ((idx, arg) in typeArgs.withIndex()) {
481-
val argId = getTypeArgumentLabel(arg, c)
482-
tw.writeTypeArgs(argId, idx, id)
459+
fun extractClassSource(c: IrClass): Label<out DbClassorinterface> {
460+
val id = useClassSource(c)
461+
val pkg = c.packageFqName?.asString() ?: ""
462+
val cls = c.name.asString()
463+
val pkgId = extractPackage(pkg)
464+
if(c.kind == ClassKind.INTERFACE) {
465+
@Suppress("UNCHECKED_CAST")
466+
val interfaceId = id as Label<out DbInterface>
467+
tw.writeInterfaces(interfaceId, cls, pkgId, interfaceId)
468+
} else {
469+
@Suppress("UNCHECKED_CAST")
470+
val classId = id as Label<out DbClass>
471+
tw.writeClasses(classId, cls, pkgId, classId)
472+
473+
if (c.kind == ClassKind.ENUM_CLASS) {
474+
tw.writeIsEnumType(classId)
483475
}
484-
tw.writeIsParameterized(id)
485-
val unbound = useClassSource(c)
486-
tw.writeErasure(id, unbound)
476+
}
477+
478+
extractClassCommon(c, id)
479+
c.typeParameters.map { extractTypeParameter(it) }
480+
c.declarations.map { extractDeclaration(it) }
481+
extractObjectInitializerFunction(c, id)
482+
483+
return id
484+
}
485+
486+
fun extractClassInstance(c: IrClass, typeArgs: List<IrTypeArgument>): Label<out DbClassorinterface> {
487+
if (typeArgs.isEmpty()) {
488+
logger.warnElement(Severity.ErrorSevere, "Instance without type arguments: " + c.name.asString(), c)
489+
}
490+
491+
val id = addClassLabel(c, typeArgs)
492+
val pkg = c.packageFqName?.asString() ?: ""
493+
val cls = c.name.asString()
494+
val pkgId = extractPackage(pkg)
495+
if(c.kind == ClassKind.INTERFACE) {
496+
@Suppress("UNCHECKED_CAST")
497+
val interfaceId = id as Label<out DbInterface>
498+
@Suppress("UNCHECKED_CAST")
499+
val sourceInterfaceId = useClassSource(c) as Label<out DbInterface>
500+
tw.writeInterfaces(interfaceId, cls, pkgId, sourceInterfaceId)
487501
} else {
488-
c.declarations.map { extractDeclaration(it) }
502+
@Suppress("UNCHECKED_CAST")
503+
val classId = id as Label<out DbClass>
504+
@Suppress("UNCHECKED_CAST")
505+
val sourceClassId = useClassSource(c) as Label<out DbClass>
506+
tw.writeClasses(classId, cls, pkgId, sourceClassId)
489507

490-
extractObjectInitializerFunction(c, id)
508+
if (c.kind == ClassKind.ENUM_CLASS) {
509+
tw.writeIsEnumType(classId)
510+
}
511+
}
512+
extractClassCommon(c, id)
513+
514+
for ((idx, arg) in typeArgs.withIndex()) {
515+
val argId = getTypeArgumentLabel(arg, c)
516+
tw.writeTypeArgs(argId, idx, id)
491517
}
518+
tw.writeIsParameterized(id)
519+
val unbound = useClassSource(c)
520+
tw.writeErasure(id, unbound)
492521

493522
return id
494523
}

0 commit comments

Comments
 (0)