New J2K: Introduce JKFile
This commit is contained in:
committed by
Ilya Kirillov
parent
e4fc4b8218
commit
0ec2fb3d17
@@ -226,8 +226,9 @@ class JavaToJKTreeBuilder(var symbolProvider: JKSymbolProvider) {
|
|||||||
val psi = this
|
val psi = this
|
||||||
return JKClassImpl(with(modifierMapper) { modifierList.toJK() }, JKNameIdentifierImpl(name!!), classKind).also {
|
return JKClassImpl(with(modifierMapper) { modifierList.toJK() }, JKNameIdentifierImpl(name!!), classKind).also {
|
||||||
it.declarationList = psi.children.mapNotNull {
|
it.declarationList = psi.children.mapNotNull {
|
||||||
ElementVisitor(this@DeclarationMapper).apply { it.accept(this) }.resultElement as? JKDeclaration
|
ElementVisitor().apply { it.accept(this) }.resultElement as? JKDeclaration
|
||||||
}
|
}
|
||||||
|
backAnnotation[it] = this
|
||||||
(symbolProvider.provideDirectSymbol(psi) as? JKUniverseClassSymbol)?.run { target = it }
|
(symbolProvider.provideDirectSymbol(psi) as? JKUniverseClassSymbol)?.run { target = it }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -346,7 +347,7 @@ class JavaToJKTreeBuilder(var symbolProvider: JKSymbolProvider) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class ElementVisitor(val declarationMapper: DeclarationMapper) : JavaElementVisitor() {
|
private inner class ElementVisitor : JavaElementVisitor() {
|
||||||
|
|
||||||
var resultElement: JKTreeElement? = null
|
var resultElement: JKTreeElement? = null
|
||||||
|
|
||||||
@@ -363,14 +364,16 @@ class JavaToJKTreeBuilder(var symbolProvider: JKSymbolProvider) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitFile(file: PsiFile) {
|
override fun visitFile(file: PsiFile) {
|
||||||
file.acceptChildren(this)
|
resultElement = JKFileImpl().apply {
|
||||||
|
declarationList += file.children.mapNotNull { ElementVisitor().apply { it.accept(this) }.resultElement as? JKDeclaration }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun buildTree(psi: PsiElement): JKTreeElement? {
|
fun buildTree(psi: PsiElement): JKTreeElement? {
|
||||||
assert(psi.language.`is`(JavaLanguage.INSTANCE)) { "Unable to build JK Tree using Java Visitor for language ${psi.language}" }
|
assert(psi.language.`is`(JavaLanguage.INSTANCE)) { "Unable to build JK Tree using Java Visitor for language ${psi.language}" }
|
||||||
val elementVisitor = ElementVisitor(declarationMapper)
|
val elementVisitor = ElementVisitor()
|
||||||
psi.accept(elementVisitor)
|
psi.accept(elementVisitor)
|
||||||
return elementVisitor.resultElement
|
return elementVisitor.resultElement
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,10 @@ class NewCodeBuilder {
|
|||||||
printer.print("/* !!! Hit visitElement for element type: ${treeElement::class} !!! */")
|
printer.print("/* !!! Hit visitElement for element type: ${treeElement::class} !!! */")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun visitFile(file: JKFile) {
|
||||||
|
file.acceptChildren(this)
|
||||||
|
}
|
||||||
|
|
||||||
override fun visitModifierList(modifierList: JKModifierList) {
|
override fun visitModifierList(modifierList: JKModifierList) {
|
||||||
modifierList.modifiers.firstOrNull()?.accept(this)
|
modifierList.modifiers.firstOrNull()?.accept(this)
|
||||||
for (i in 1..modifierList.modifiers.lastIndex) {
|
for (i in 1..modifierList.modifiers.lastIndex) {
|
||||||
|
|||||||
@@ -23,6 +23,12 @@ import org.jetbrains.kotlin.j2k.tree.JKLiteralExpression.LiteralType.BOOLEAN
|
|||||||
import org.jetbrains.kotlin.j2k.tree.JKLiteralExpression.LiteralType.NULL
|
import org.jetbrains.kotlin.j2k.tree.JKLiteralExpression.LiteralType.NULL
|
||||||
import org.jetbrains.kotlin.j2k.tree.visitors.JKVisitor
|
import org.jetbrains.kotlin.j2k.tree.visitors.JKVisitor
|
||||||
|
|
||||||
|
class JKFileImpl : JKFile, JKBranchElementBase() {
|
||||||
|
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitFile(this, data)
|
||||||
|
|
||||||
|
override var declarationList by children<JKDeclaration>()
|
||||||
|
}
|
||||||
|
|
||||||
class JKClassImpl(
|
class JKClassImpl(
|
||||||
modifierList: JKModifierList,
|
modifierList: JKModifierList,
|
||||||
name: JKNameIdentifier,
|
name: JKNameIdentifier,
|
||||||
|
|||||||
@@ -33,6 +33,10 @@ interface JKTreeElement : JKElement {
|
|||||||
|
|
||||||
interface JKDeclaration : JKTreeElement
|
interface JKDeclaration : JKTreeElement
|
||||||
|
|
||||||
|
interface JKFile : JKTreeElement, JKBranchElement {
|
||||||
|
var declarationList: List<JKDeclaration>
|
||||||
|
}
|
||||||
|
|
||||||
interface JKClass : JKDeclaration, JKModifierListOwner {
|
interface JKClass : JKDeclaration, JKModifierListOwner {
|
||||||
val name: JKNameIdentifier
|
val name: JKNameIdentifier
|
||||||
var declarationList: List<JKDeclaration>
|
var declarationList: List<JKDeclaration>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import org.jetbrains.kotlin.j2k.tree.*
|
|||||||
interface JKVisitor<out R, in D> {
|
interface JKVisitor<out R, in D> {
|
||||||
fun visitTreeElement(treeElement: JKTreeElement, data: D): R
|
fun visitTreeElement(treeElement: JKTreeElement, data: D): R
|
||||||
fun visitDeclaration(declaration: JKDeclaration, data: D): R = visitTreeElement(declaration, data)
|
fun visitDeclaration(declaration: JKDeclaration, data: D): R = visitTreeElement(declaration, data)
|
||||||
|
fun visitFile(file: JKFile, data: D): R = visitTreeElement(file, data)
|
||||||
fun visitClass(klass: JKClass, data: D): R = visitDeclaration(klass, data)
|
fun visitClass(klass: JKClass, data: D): R = visitDeclaration(klass, data)
|
||||||
fun visitMethod(method: JKMethod, data: D): R = visitDeclaration(method, data)
|
fun visitMethod(method: JKMethod, data: D): R = visitDeclaration(method, data)
|
||||||
fun visitField(field: JKField, data: D): R = visitDeclaration(field, data)
|
fun visitField(field: JKField, data: D): R = visitDeclaration(field, data)
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ interface JKVisitorVoid : JKVisitor<Unit, Nothing?> {
|
|||||||
override fun visitTreeElement(treeElement: JKTreeElement, data: Nothing?) = visitTreeElement(treeElement)
|
override fun visitTreeElement(treeElement: JKTreeElement, data: Nothing?) = visitTreeElement(treeElement)
|
||||||
fun visitDeclaration(declaration: JKDeclaration) = visitTreeElement(declaration, null)
|
fun visitDeclaration(declaration: JKDeclaration) = visitTreeElement(declaration, null)
|
||||||
override fun visitDeclaration(declaration: JKDeclaration, data: Nothing?) = visitDeclaration(declaration)
|
override fun visitDeclaration(declaration: JKDeclaration, data: Nothing?) = visitDeclaration(declaration)
|
||||||
|
fun visitFile(file: JKFile) = visitTreeElement(file, null)
|
||||||
|
override fun visitFile(file: JKFile, data: Nothing?) = visitFile(file)
|
||||||
fun visitClass(klass: JKClass) = visitDeclaration(klass, null)
|
fun visitClass(klass: JKClass) = visitDeclaration(klass, null)
|
||||||
override fun visitClass(klass: JKClass, data: Nothing?) = visitClass(klass)
|
override fun visitClass(klass: JKClass, data: Nothing?) = visitClass(klass)
|
||||||
fun visitMethod(method: JKMethod) = visitDeclaration(method, null)
|
fun visitMethod(method: JKMethod) = visitDeclaration(method, null)
|
||||||
|
|||||||
Reference in New Issue
Block a user