New J2K: Added list child into JKBranchElement. Added attach and detach method to JKElement

This commit is contained in:
Dimach
2018-05-18 17:39:23 +03:00
committed by Ilya Kirillov
parent f691fb457e
commit 83f9054a7e
5 changed files with 75 additions and 62 deletions
@@ -16,44 +16,59 @@
package org.jetbrains.kotlin.j2k.tree.impl
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.j2k.ast.Nullability
import org.jetbrains.kotlin.j2k.tree.*
import org.jetbrains.kotlin.j2k.tree.visitors.JKVisitor
import com.intellij.psi.PsiElement
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
private class JKChild<T : JKTreeElement>(val value: Int) : ReadWriteProperty<JKMutableBranchElement, T> {
override operator fun getValue(thisRef: JKMutableBranchElement, property: KProperty<*>): T {
class JKChild<T : JKElement>(val value: Int) : ReadWriteProperty<JKBranchElementBase, T> {
override operator fun getValue(thisRef: JKBranchElementBase, property: KProperty<*>): T {
return thisRef.children[value] as T
}
override operator fun setValue(thisRef: JKMutableBranchElement, property: KProperty<*>, value: T) {
thisRef.children[this.value].parent = null
value.parent = thisRef
override operator fun setValue(thisRef: JKBranchElementBase, property: KProperty<*>, value: T) {
(thisRef.children[this.value] as T).detach()
value.attach(thisRef)
thisRef.children[this.value] = value
}
}
class JKListChild<T : JKTreeElement>() : ReadWriteProperty<JKMutableBranchElement, List<T>> {
override operator fun getValue(thisRef: JKMutableBranchElement, property: KProperty<*>): List<T> {
class JKListChild<T : JKElement>(val value: Int) : ReadWriteProperty<JKBranchElementBase, List<T>> {
override operator fun getValue(thisRef: JKBranchElementBase, property: KProperty<*>): List<T> {
return thisRef.children.toList() as List<T>
}
override operator fun setValue(thisRef: JKMutableBranchElement, property: KProperty<*>, value: List<T>) {
thisRef.children.forEach { it.parent = null }
value.forEach { it.parent = thisRef }
thisRef.children.clear()
thisRef.children.addAll(value)
override operator fun setValue(thisRef: JKBranchElementBase, property: KProperty<*>, value: List<T>) {
(thisRef.children[this.value] as List<T>).forEach { it.detach() }
value.forEach { it.attach(thisRef) }
thisRef.children[this.value] = value
}
}
abstract class JKElementBase : JKTreeElement {
override var parent: JKElement? = null
fun <D : JKTreeElement> D.setParent(p: JKTreeElement): D {
parent = p
return this
final override fun detach() {
val prevParent = parent
assert(prevParent != null)
parent = null
onDetach(prevParent!!)
}
open fun onDetach(from: JKElement) {
}
final override fun attach(to: JKElement) {
assert(parent == null)
parent = to
onAttach()
}
open fun onAttach() {
}
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitElement(this, data)
@@ -61,46 +76,35 @@ abstract class JKElementBase : JKTreeElement {
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {}
}
interface JKMutableBranchElement : JKBranchElement {
override val children: MutableList<JKTreeElement>
}
abstract class JKBranchElementBase : JKElementBase(), JKMutableBranchElement {
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {
children.forEach { it.accept(visitor, data) }
}
abstract class JKBranchElementBase : JKElementBase(), JKBranchElement {
protected var childNum = 0
protected fun <T : JKTreeElement, U : T> child(v: U): ReadWriteProperty<JKMutableBranchElement, T> {
protected fun <T : JKTreeElement, U : T> child(v: U): ReadWriteProperty<JKBranchElementBase, T> {
children.add(childNum, v)
return JKChild(childNum++)
}
override val children: MutableList<JKTreeElement> = mutableListOf()
}
abstract class JKElementListBase: JKElementBase(), JKMutableBranchElement {
override val children: MutableList<JKTreeElement> = mutableListOf()
protected inline fun <reified T : JKTreeElement> children(): JKListChild<T> {
return JKListChild()
protected inline fun <reified T : JKTreeElement> child(): ReadWriteProperty<JKBranchElementBase, List<T>> {
children[childNum] = listOf<T>()
return JKListChild(childNum++)
}
protected inline fun <reified T : JKTreeElement> children(v: List<T>): JKListChild<T> {
children.addAll(v)
return JKListChild()
protected inline fun <reified T : JKTreeElement> child(v: List<T>): ReadWriteProperty<JKBranchElementBase, List<T>> {
children[childNum] = v
return JKListChild(childNum++)
}
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {
children.forEach { it.accept(visitor, data) }
children.forEach {
if (it is JKTreeElement)
it.accept(visitor, data)
else
(it as? List<JKTreeElement>)?.forEach { it.accept(visitor, data) }
}
}
}
class JKUDeclarationListImpl : JKElementListBase(), JKUDeclarationList {
override var declarations: List<JKUniverseDeclaration> by children()
override val children: MutableList<Any> = mutableListOf()
}
class JKClassImpl(modifierList: JKModifierList, name: JKNameIdentifier, override var classKind: JKClass.ClassKind) :
JKUniverseClass,
JKBranchElementBase() {
@@ -109,7 +113,7 @@ class JKClassImpl(modifierList: JKModifierList, name: JKNameIdentifier, override
override var modifierList by child(modifierList)
override val valid: Boolean = true
override val declarationList by child(JKUDeclarationListImpl())
override var declarationList by child<JKUniverseDeclaration>()
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitUniverseClass(this, data)
}
@@ -117,8 +121,8 @@ class JKClassImpl(modifierList: JKModifierList, name: JKNameIdentifier, override
class JKNameIdentifierImpl(override val value: String) : JKNameIdentifier, JKElementBase() {}
class JKModifierListImpl(modifiers: List<JKModifier> = emptyList()) : JKModifierList, JKElementListBase() {
override var modifiers: List<JKModifier> by children(modifiers)
class JKModifierListImpl(modifiers: List<JKModifier> = emptyList()) : JKModifierList, JKBranchElementBase() {
override var modifiers: List<JKModifier> by child(modifiers)
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitModifierList(this, data)
}
@@ -127,8 +131,8 @@ class JKValueArgumentImpl(type: JKType, override val name: String) : JKValueArgu
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitValueArgument(this, data)
}
class JKBlockImpl(statements: List<JKStatement> = emptyList()) : JKBlock, JKElementListBase() {
override var statements by children(statements)
class JKBlockImpl(statements: List<JKStatement> = emptyList()) : JKBlock, JKBranchElementBase() {
override var statements by child(statements)
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitBlock(this, data)
}
@@ -150,8 +154,8 @@ class JKPostfixExpressionImpl(expression: JKExpression, override var operator: J
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitPostfixExpression(this, data)
}
class JKExpressionListImpl(expressions: List<JKExpression> = emptyList()) : JKExpressionList, JKElementListBase() {
override var expressions by children(expressions)
class JKExpressionListImpl(expressions: List<JKExpression> = emptyList()) : JKExpressionList, JKBranchElementBase() {
override var expressions by child(expressions)
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitExpressionList(this, data)
}
@@ -187,7 +191,8 @@ class JKParenthesizedExpressionImpl(override var expression: JKExpression) : JKP
}
class JKTypeCastExpressionImpl(override var expression: JKExpression, override var type: JKType) : JKTypeCastExpression, JKBranchElementBase() {
class JKTypeCastExpressionImpl(override var expression: JKExpression, override var type: JKType) : JKTypeCastExpression,
JKBranchElementBase() {
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitTypeCastExpression(this, data)
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {
@@ -18,6 +18,8 @@ package org.jetbrains.kotlin.j2k.tree
import org.jetbrains.kotlin.j2k.ast.Nullability
import org.jetbrains.kotlin.j2k.tree.visitors.JKVisitor
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
interface JKTreeElement : JKElement {
fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R
@@ -36,7 +38,7 @@ interface JKUDeclarationList : JKDeclarationList {
}
interface JKUniverseClass : JKUniverseDeclaration, JKClass {
override val declarationList: JKUDeclarationList
override var declarationList: List<JKUniverseDeclaration>
}
interface JKModifier : JKTreeElement
@@ -16,16 +16,20 @@
package org.jetbrains.kotlin.j2k.tree
interface JKOperator: JKTreeElement
interface JKOperator : JKTreeElement
interface JKQualifier
interface JKElement {
var parent: JKElement?
val parent: JKElement?
fun detach()
fun attach(to: JKElement)
}
interface JKBranchElement : JKElement {
val children: List<JKTreeElement>
val children: List<Any>
}
interface JKModifierListOwner {
@@ -40,7 +44,7 @@ interface JKDeclaration : JKElement, JKReferenceTarget
interface JKClass : JKDeclaration, JKModifierListOwner {
val name: JKNameIdentifier
val declarationList: JKDeclarationList
val declarationList: List<JKDeclaration>
var classKind: ClassKind
enum class ClassKind {
@@ -14,4 +14,8 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.j2k.tree.multiverse
package org.jetbrains.kotlin.j2k.tree.multiverse
import org.jetbrains.kotlin.j2k.tree.JKDeclaration
interface JKMultiverseDeclaration : JKDeclaration
@@ -29,14 +29,14 @@ abstract class JKMultiverseElementBase : JKElement {
}
}
class JKMultiverseField(override var name: JKNameIdentifier) : JKField, JKMultiverseElementBase() {
class JKMultiverseField(override var name: JKNameIdentifier) : JKField, JKMultiverseDeclaration, JKMultiverseElementBase() {
override var modifierList: JKModifierList = JKModifierListImpl()
override var type: JKType = JKJavaPrimitiveTypeImpl.BOOLEAN //TODO
override val valid: Boolean
get() = true
}
class JKMultiverseMethod(override var name: JKNameIdentifier) : JKMethod, JKMultiverseElementBase() {
class JKMultiverseMethod(override var name: JKNameIdentifier) : JKMethod, JKMultiverseDeclaration, JKMultiverseElementBase() {
override val returnType: JKType
get() = TODO("not implemented")
override var modifierList: JKModifierList = JKModifierListImpl()
@@ -46,11 +46,9 @@ class JKMultiverseMethod(override var name: JKNameIdentifier) : JKMethod, JKMult
}
class JKMultiverseClass(
override var name: JKNameIdentifier, override val declarationList: JKMDeclarationList,
override var name: JKNameIdentifier, override val declarationList: List<JKMultiverseDeclaration>,
override var classKind: JKClass.ClassKind, override var modifierList: JKModifierList
) : JKClass, JKMultiverseElementBase() {
override val valid: Boolean
get() = true
}
class JKMDeclarationList(override val declarations: List<JKDeclaration>) : JKMultiverseElementBase(), JKDeclarationList
}