New J2K: Add inheritance and support while & type-casts in code builder
This commit is contained in:
committed by
Ilya Kirillov
parent
928647462e
commit
b702e207c0
@@ -76,7 +76,7 @@ class JavaToJKTreeBuilder(var symbolProvider: JKSymbolProvider) {
|
|||||||
|
|
||||||
fun PsiAssignmentExpression.toJK(): JKJavaAssignmentExpression {
|
fun PsiAssignmentExpression.toJK(): JKJavaAssignmentExpression {
|
||||||
return JKJavaAssignmentExpressionImpl(
|
return JKJavaAssignmentExpressionImpl(
|
||||||
lExpression.toJK() as? JKAssignableExpression ?: error("Its possible?"),
|
lExpression.toJK() as? JKAssignableExpression ?: error("Its possible? ${lExpression.toJK().prettyDebugPrintTree()}"),
|
||||||
rExpression.toJK(),
|
rExpression.toJK(),
|
||||||
operationSign.toJK()
|
operationSign.toJK()
|
||||||
)
|
)
|
||||||
@@ -238,7 +238,12 @@ class JavaToJKTreeBuilder(var symbolProvider: JKSymbolProvider) {
|
|||||||
isInterface -> JKClass.ClassKind.INTERFACE
|
isInterface -> JKClass.ClassKind.INTERFACE
|
||||||
else -> JKClass.ClassKind.CLASS
|
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 {
|
jkClassImpl.declarationList = children.mapNotNull {
|
||||||
ElementVisitor().apply { it.accept(this) }.resultElement as? JKDeclaration
|
ElementVisitor().apply { it.accept(this) }.resultElement as? JKDeclaration
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,6 +101,11 @@ class NewCodeBuilder {
|
|||||||
printer.print(classKindString(klass.classKind))
|
printer.print(classKindString(klass.classKind))
|
||||||
builder.append(" ")
|
builder.append(" ")
|
||||||
printer.printWithNoIndent(klass.name.value)
|
printer.printWithNoIndent(klass.name.value)
|
||||||
|
if (klass.inheritance.inherit.isNotEmpty()) {
|
||||||
|
printer.printWithNoIndent(" : ")
|
||||||
|
klass.inheritance.inherit.forEach { it.accept(this) }
|
||||||
|
}
|
||||||
|
|
||||||
if (klass.declarationList.isNotEmpty()) {
|
if (klass.declarationList.isNotEmpty()) {
|
||||||
printer.println(" {")
|
printer.println(" {")
|
||||||
printer.pushIndent()
|
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) {
|
override fun visitLocalVariable(localVariable: JKLocalVariable) {
|
||||||
if (localVariable.modifierList.modality == JKModalityModifier.Modality.FINAL) {
|
if (localVariable.modifierList.modality == JKModalityModifier.Modality.FINAL) {
|
||||||
printer.print("val")
|
printer.print("val")
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ class JKFileImpl : JKFile, JKBranchElementBase() {
|
|||||||
class JKClassImpl(
|
class JKClassImpl(
|
||||||
modifierList: JKModifierList,
|
modifierList: JKModifierList,
|
||||||
name: JKNameIdentifier,
|
name: JKNameIdentifier,
|
||||||
|
inheritance: JKInheritanceInfo,
|
||||||
override var classKind: JKClass.ClassKind
|
override var classKind: JKClass.ClassKind
|
||||||
) : JKClass, JKBranchElementBase() {
|
) : JKClass, JKBranchElementBase() {
|
||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitClass(this, data)
|
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 name by child(name)
|
||||||
override var modifierList by child(modifierList)
|
override var modifierList by child(modifierList)
|
||||||
override var declarationList by children<JKDeclaration>()
|
override var declarationList by children<JKDeclaration>()
|
||||||
|
override val inheritance by child(inheritance)
|
||||||
}
|
}
|
||||||
|
|
||||||
class JKNameIdentifierImpl(override val value: String) : JKNameIdentifier, JKElementBase() {}
|
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 val returnType by child(returnType)
|
||||||
override var parameters by children(parameters)
|
override var parameters by children(parameters)
|
||||||
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() {
|
||||||
|
override val inherit: List<JKTypeElement> by children(implements)
|
||||||
}
|
}
|
||||||
@@ -39,6 +39,9 @@ interface JKFile : JKTreeElement, JKBranchElement {
|
|||||||
|
|
||||||
interface JKClass : JKDeclaration, JKModifierListOwner {
|
interface JKClass : JKDeclaration, JKModifierListOwner {
|
||||||
val name: JKNameIdentifier
|
val name: JKNameIdentifier
|
||||||
|
|
||||||
|
val inheritance: JKInheritanceInfo
|
||||||
|
|
||||||
var declarationList: List<JKDeclaration>
|
var declarationList: List<JKDeclaration>
|
||||||
var classKind: ClassKind
|
var classKind: ClassKind
|
||||||
|
|
||||||
@@ -47,6 +50,10 @@ interface JKClass : JKDeclaration, JKModifierListOwner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface JKInheritanceInfo : JKTreeElement, JKBranchElement {
|
||||||
|
val inherit: List<JKTypeElement>
|
||||||
|
}
|
||||||
|
|
||||||
interface JKMethod : JKDeclaration, JKModifierListOwner {
|
interface JKMethod : JKDeclaration, JKModifierListOwner {
|
||||||
val name: JKNameIdentifier
|
val name: JKNameIdentifier
|
||||||
var parameters: List<JKParameter>
|
var parameters: List<JKParameter>
|
||||||
@@ -124,7 +131,7 @@ interface JKPrefixExpression : JKUnaryExpression
|
|||||||
|
|
||||||
interface JKPostfixExpression : JKUnaryExpression
|
interface JKPostfixExpression : JKUnaryExpression
|
||||||
|
|
||||||
interface JKQualifiedExpression : JKExpression {
|
interface JKQualifiedExpression : JKExpression, JKAssignableExpression {
|
||||||
val receiver: JKExpression
|
val receiver: JKExpression
|
||||||
val operator: JKQualifier
|
val operator: JKQualifier
|
||||||
val selector: JKExpression
|
val selector: JKExpression
|
||||||
|
|||||||
Reference in New Issue
Block a user