New J2K: Fix class inheritance info
This commit is contained in:
committed by
Ilya Kirillov
parent
ad58772453
commit
12fe096b82
@@ -291,10 +291,10 @@ class JavaToJKTreeBuilder(var symbolProvider: JKSymbolProvider) {
|
||||
this.referencedTypes.map { with(expressionTreeMapper) { JKTypeElementImpl(it.toJK(symbolProvider, Nullability.NotNull)) } }
|
||||
|
||||
val implTypes = this.implementsList?.mapTypes().orEmpty()
|
||||
val extTypes = this.extendsList?.mapTypes().orEmpty()
|
||||
val extensionType = this.extendsList?.mapTypes().orEmpty()
|
||||
return JKClassImpl(
|
||||
JKNameIdentifierImpl(name!!),
|
||||
JKInheritanceInfoImpl(extTypes + implTypes),
|
||||
JKInheritanceInfoImpl(extensionType, implTypes),
|
||||
classKind,
|
||||
typeParameterList?.toJK() ?: JKTypeParameterListImpl(),
|
||||
createClassBody(),
|
||||
|
||||
@@ -185,18 +185,10 @@ class NewCodeBuilder {
|
||||
val primaryConstructor = klass.primaryConstructor()
|
||||
primaryConstructor?.accept(this)
|
||||
|
||||
if (klass.inheritance.inherit.isNotEmpty()) {
|
||||
printer.printWithNoIndent(" : ")
|
||||
|
||||
val delegationCall = primaryConstructor?.delegationCall as? JKDelegationConstructorCall
|
||||
renderList(klass.inheritance.inherit) {
|
||||
it.accept(this)
|
||||
if (delegationCall != null && delegationCall.isCallOfConstructorOf(it.type)) {
|
||||
printer.par {
|
||||
delegationCall.arguments.accept(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (klass.inheritance.present()) {
|
||||
printer.printWithNoIndent(" : ")
|
||||
klass.inheritance.accept(this)
|
||||
}
|
||||
|
||||
//TODO should it be here?
|
||||
@@ -205,6 +197,36 @@ class NewCodeBuilder {
|
||||
klass.classBody.accept(this)
|
||||
}
|
||||
|
||||
override fun visitInheritanceInfo(inheritanceInfo: JKInheritanceInfo) {
|
||||
val parentClass = inheritanceInfo.parentOfType<JKClass>()!!
|
||||
val isInInterface = parentClass.classKind == JKClass.ClassKind.INTERFACE
|
||||
val extendTypes = inheritanceInfo.extends.map { it.type.updateNullability(Nullability.NotNull) }
|
||||
val implementTypes = inheritanceInfo.implements.map { it.type.updateNullability(Nullability.NotNull) }
|
||||
if (isInInterface) {
|
||||
renderList(extendTypes) { renderType(it) }
|
||||
} else {
|
||||
extendTypes.singleOrNull()?.also {
|
||||
renderType(it)
|
||||
val delegationCall =
|
||||
parentClass
|
||||
.primaryConstructor()
|
||||
?.delegationCall
|
||||
?.let { it as? JKDelegationConstructorCall }
|
||||
if (delegationCall != null) {
|
||||
printer.par { delegationCall.arguments.accept(this) }
|
||||
} else {
|
||||
printer.printWithNoIndent("()")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (implementTypes.isNotEmpty() && extendTypes.size == 1) {
|
||||
printer.printWithNoIndent(", ")
|
||||
}
|
||||
renderList(implementTypes) { renderType(it) }
|
||||
}
|
||||
|
||||
|
||||
private fun renderEnumConstants(enumConstants: List<JKEnumConstant>) {
|
||||
renderList(enumConstants) {
|
||||
it.accept(this)
|
||||
|
||||
+5
-5
@@ -28,15 +28,15 @@ class InsertDefaultPrimaryConstructorConversion(private val context: ConversionC
|
||||
|
||||
element.classBody.declarations += constructor
|
||||
|
||||
val superClassSymbol = element.inheritance.inherit.map { it.type }
|
||||
.filterIsInstance<JKClassType>()
|
||||
.mapNotNull { (it.classReference as? JKClassSymbol) }
|
||||
.firstOrNull { it.kind == JKClass.ClassKind.CLASS }
|
||||
val superClassSymbol =
|
||||
(element.inheritance.extends.singleOrNull() as? JKClassType)?.classReference
|
||||
|
||||
if (superClassSymbol is JKUniverseClassSymbol) {
|
||||
val superClass = recurse(superClassSymbol.target)
|
||||
val superConstructor = context.symbolProvider.provideUniverseSymbol(
|
||||
superClass.declarationList.single { it is JKKtConstructor && it.parameters.isEmpty() } as JKMethod
|
||||
superClass.declarationList.singleOrNull { it is JKKtConstructor && it.parameters.isEmpty() } as? JKMethod ?: return recurse(
|
||||
element
|
||||
)
|
||||
)
|
||||
constructor.delegationCall = JKDelegationConstructorCallImpl(superConstructor, JKSuperExpressionImpl(), JKExpressionListImpl())
|
||||
}
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ class StaticsToCompanionExtractConversion : RecursiveApplicableConversionBase()
|
||||
|
||||
return JKClassImpl(
|
||||
JKNameIdentifierImpl(""),
|
||||
JKInheritanceInfoImpl(emptyList()),
|
||||
JKInheritanceInfoImpl(emptyList(), emptyList()),
|
||||
JKClass.ClassKind.COMPANION,
|
||||
JKTypeParameterListImpl(),
|
||||
JKClassBodyImpl(),
|
||||
|
||||
@@ -377,8 +377,12 @@ class JKLambdaExpressionImpl(
|
||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitLambdaExpression(this, data)
|
||||
}
|
||||
|
||||
class JKInheritanceInfoImpl(implements: List<JKTypeElement>) : JKInheritanceInfo, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() {
|
||||
override val inherit: List<JKTypeElement> by children(implements)
|
||||
class JKInheritanceInfoImpl(
|
||||
extends: List<JKTypeElement>,
|
||||
implements: List<JKTypeElement>
|
||||
) : JKInheritanceInfo, JKBranchElementBase(), PsiOwner by PsiOwnerImpl() {
|
||||
override var extends: List<JKTypeElement> by children(extends)
|
||||
override var implements: List<JKTypeElement> by children(implements)
|
||||
|
||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitInheritanceInfo(this, data)
|
||||
}
|
||||
|
||||
@@ -68,9 +68,13 @@ val JKClass.declarationList: List<JKDeclaration>
|
||||
|
||||
|
||||
interface JKInheritanceInfo : JKTreeElement, JKBranchElement {
|
||||
val inherit: List<JKTypeElement>
|
||||
var extends: List<JKTypeElement>
|
||||
var implements: List<JKTypeElement>
|
||||
}
|
||||
|
||||
fun JKInheritanceInfo.present(): Boolean =
|
||||
extends.isNotEmpty() || implements.isNotEmpty()
|
||||
|
||||
interface JKAnnotationList : JKTreeElement {
|
||||
var annotations: List<JKAnnotation>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user