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)) } }
|
this.referencedTypes.map { with(expressionTreeMapper) { JKTypeElementImpl(it.toJK(symbolProvider, Nullability.NotNull)) } }
|
||||||
|
|
||||||
val implTypes = this.implementsList?.mapTypes().orEmpty()
|
val implTypes = this.implementsList?.mapTypes().orEmpty()
|
||||||
val extTypes = this.extendsList?.mapTypes().orEmpty()
|
val extensionType = this.extendsList?.mapTypes().orEmpty()
|
||||||
return JKClassImpl(
|
return JKClassImpl(
|
||||||
JKNameIdentifierImpl(name!!),
|
JKNameIdentifierImpl(name!!),
|
||||||
JKInheritanceInfoImpl(extTypes + implTypes),
|
JKInheritanceInfoImpl(extensionType, implTypes),
|
||||||
classKind,
|
classKind,
|
||||||
typeParameterList?.toJK() ?: JKTypeParameterListImpl(),
|
typeParameterList?.toJK() ?: JKTypeParameterListImpl(),
|
||||||
createClassBody(),
|
createClassBody(),
|
||||||
|
|||||||
@@ -185,18 +185,10 @@ class NewCodeBuilder {
|
|||||||
val primaryConstructor = klass.primaryConstructor()
|
val primaryConstructor = klass.primaryConstructor()
|
||||||
primaryConstructor?.accept(this)
|
primaryConstructor?.accept(this)
|
||||||
|
|
||||||
if (klass.inheritance.inherit.isNotEmpty()) {
|
|
||||||
printer.printWithNoIndent(" : ")
|
|
||||||
|
|
||||||
val delegationCall = primaryConstructor?.delegationCall as? JKDelegationConstructorCall
|
if (klass.inheritance.present()) {
|
||||||
renderList(klass.inheritance.inherit) {
|
printer.printWithNoIndent(" : ")
|
||||||
it.accept(this)
|
klass.inheritance.accept(this)
|
||||||
if (delegationCall != null && delegationCall.isCallOfConstructorOf(it.type)) {
|
|
||||||
printer.par {
|
|
||||||
delegationCall.arguments.accept(this)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO should it be here?
|
//TODO should it be here?
|
||||||
@@ -205,6 +197,36 @@ class NewCodeBuilder {
|
|||||||
klass.classBody.accept(this)
|
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>) {
|
private fun renderEnumConstants(enumConstants: List<JKEnumConstant>) {
|
||||||
renderList(enumConstants) {
|
renderList(enumConstants) {
|
||||||
it.accept(this)
|
it.accept(this)
|
||||||
|
|||||||
+5
-5
@@ -28,15 +28,15 @@ class InsertDefaultPrimaryConstructorConversion(private val context: ConversionC
|
|||||||
|
|
||||||
element.classBody.declarations += constructor
|
element.classBody.declarations += constructor
|
||||||
|
|
||||||
val superClassSymbol = element.inheritance.inherit.map { it.type }
|
val superClassSymbol =
|
||||||
.filterIsInstance<JKClassType>()
|
(element.inheritance.extends.singleOrNull() as? JKClassType)?.classReference
|
||||||
.mapNotNull { (it.classReference as? JKClassSymbol) }
|
|
||||||
.firstOrNull { it.kind == JKClass.ClassKind.CLASS }
|
|
||||||
|
|
||||||
if (superClassSymbol is JKUniverseClassSymbol) {
|
if (superClassSymbol is JKUniverseClassSymbol) {
|
||||||
val superClass = recurse(superClassSymbol.target)
|
val superClass = recurse(superClassSymbol.target)
|
||||||
val superConstructor = context.symbolProvider.provideUniverseSymbol(
|
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())
|
constructor.delegationCall = JKDelegationConstructorCallImpl(superConstructor, JKSuperExpressionImpl(), JKExpressionListImpl())
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -40,7 +40,7 @@ class StaticsToCompanionExtractConversion : RecursiveApplicableConversionBase()
|
|||||||
|
|
||||||
return JKClassImpl(
|
return JKClassImpl(
|
||||||
JKNameIdentifierImpl(""),
|
JKNameIdentifierImpl(""),
|
||||||
JKInheritanceInfoImpl(emptyList()),
|
JKInheritanceInfoImpl(emptyList(), emptyList()),
|
||||||
JKClass.ClassKind.COMPANION,
|
JKClass.ClassKind.COMPANION,
|
||||||
JKTypeParameterListImpl(),
|
JKTypeParameterListImpl(),
|
||||||
JKClassBodyImpl(),
|
JKClassBodyImpl(),
|
||||||
|
|||||||
@@ -377,8 +377,12 @@ class JKLambdaExpressionImpl(
|
|||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitLambdaExpression(this, data)
|
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() {
|
class JKInheritanceInfoImpl(
|
||||||
override val inherit: List<JKTypeElement> by children(implements)
|
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)
|
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 {
|
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 {
|
interface JKAnnotationList : JKTreeElement {
|
||||||
var annotations: List<JKAnnotation>
|
var annotations: List<JKAnnotation>
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user