New J2K: Add inheritance and support while & type-casts in code builder

This commit is contained in:
Simon Ogorodnik
2018-07-09 20:55:33 +03:00
committed by Ilya Kirillov
parent 928647462e
commit b702e207c0
4 changed files with 48 additions and 4 deletions
@@ -76,7 +76,7 @@ class JavaToJKTreeBuilder(var symbolProvider: JKSymbolProvider) {
fun PsiAssignmentExpression.toJK(): JKJavaAssignmentExpression {
return JKJavaAssignmentExpressionImpl(
lExpression.toJK() as? JKAssignableExpression ?: error("Its possible?"),
lExpression.toJK() as? JKAssignableExpression ?: error("Its possible? ${lExpression.toJK().prettyDebugPrintTree()}"),
rExpression.toJK(),
operationSign.toJK()
)
@@ -238,7 +238,12 @@ class JavaToJKTreeBuilder(var symbolProvider: JKSymbolProvider) {
isInterface -> JKClass.ClassKind.INTERFACE
else -> JKClass.ClassKind.CLASS
}
return JKClassImpl(with(modifierMapper) { modifierList.toJK() }, JKNameIdentifierImpl(name!!), classKind).also { jkClassImpl ->
val implTypes = this.implementsList?.referencedTypes?.map { with(expressionTreeMapper) { JKTypeElementImpl(it.toJK()) } }.orEmpty()
val extTypes = this.extendsList?.referencedTypes?.map { with(expressionTreeMapper) { JKTypeElementImpl(it.toJK()) } }.orEmpty()
return JKClassImpl(
with(modifierMapper) { modifierList.toJK() }, JKNameIdentifierImpl(name!!), JKInheritanceInfoImpl(extTypes + implTypes),
classKind
).also { jkClassImpl ->
jkClassImpl.declarationList = children.mapNotNull {
ElementVisitor().apply { it.accept(this) }.resultElement as? JKDeclaration
}
@@ -101,6 +101,11 @@ class NewCodeBuilder {
printer.print(classKindString(klass.classKind))
builder.append(" ")
printer.printWithNoIndent(klass.name.value)
if (klass.inheritance.inherit.isNotEmpty()) {
printer.printWithNoIndent(" : ")
klass.inheritance.inherit.forEach { it.accept(this) }
}
if (klass.declarationList.isNotEmpty()) {
printer.println(" {")
printer.pushIndent()
@@ -216,6 +221,28 @@ class NewCodeBuilder {
}
}
override fun visitTypeCastExpression(typeCastExpression: JKTypeCastExpression) {
typeCastExpression.expression.accept(this)
printer.printWithNoIndent(" as ")
typeCastExpression.type.accept(this)
}
override fun visitWhileStatement(whileStatement: JKWhileStatement) {
printer.print("while(")
whileStatement.condition.accept(this)
printer.printlnWithNoIndent(")", "{")
printer.pushIndent()
val body = whileStatement.body
if (body is JKBlockStatement) {
body.block.statements.forEach { it.accept(this) }
} else {
body.accept(this)
}
printer.popIndent()
printer.println("}")
}
override fun visitLocalVariable(localVariable: JKLocalVariable) {
if (localVariable.modifierList.modality == JKModalityModifier.Modality.FINAL) {
printer.print("val")
@@ -32,6 +32,7 @@ class JKFileImpl : JKFile, JKBranchElementBase() {
class JKClassImpl(
modifierList: JKModifierList,
name: JKNameIdentifier,
inheritance: JKInheritanceInfo,
override var classKind: JKClass.ClassKind
) : JKClass, JKBranchElementBase() {
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitClass(this, data)
@@ -39,7 +40,7 @@ class JKClassImpl(
override var name by child(name)
override var modifierList by child(modifierList)
override var declarationList by children<JKDeclaration>()
override val inheritance by child(inheritance)
}
class JKNameIdentifierImpl(override val value: String) : JKNameIdentifier, JKElementBase() {}
@@ -317,4 +318,8 @@ class JKLambdaExpressionImpl(parameters: List<JKParameter>, returnType: JKTypeEl
override val returnType by child(returnType)
override var parameters by children(parameters)
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitLambdaExpression(this, data)
}
class JKInheritanceInfoImpl(implements: List<JKTypeElement>) : JKInheritanceInfo, JKBranchElementBase() {
override val inherit: List<JKTypeElement> by children(implements)
}
@@ -39,6 +39,9 @@ interface JKFile : JKTreeElement, JKBranchElement {
interface JKClass : JKDeclaration, JKModifierListOwner {
val name: JKNameIdentifier
val inheritance: JKInheritanceInfo
var declarationList: List<JKDeclaration>
var classKind: ClassKind
@@ -47,6 +50,10 @@ interface JKClass : JKDeclaration, JKModifierListOwner {
}
}
interface JKInheritanceInfo : JKTreeElement, JKBranchElement {
val inherit: List<JKTypeElement>
}
interface JKMethod : JKDeclaration, JKModifierListOwner {
val name: JKNameIdentifier
var parameters: List<JKParameter>
@@ -124,7 +131,7 @@ interface JKPrefixExpression : JKUnaryExpression
interface JKPostfixExpression : JKUnaryExpression
interface JKQualifiedExpression : JKExpression {
interface JKQualifiedExpression : JKExpression, JKAssignableExpression {
val receiver: JKExpression
val operator: JKQualifier
val selector: JKExpression