New J2K: Create init declaration from primary constructor in PrimaryConstructorDetectConversion
This commit is contained in:
committed by
Ilya Kirillov
parent
8ceca44b10
commit
d02be5ae94
-1
@@ -19,7 +19,6 @@ class InsertDefaultPrimaryConstructorConversion(private val context: ConversionC
|
||||
val constructor = JKKtPrimaryConstructorImpl(
|
||||
JKNameIdentifierImpl(element.name.value),
|
||||
emptyList(),
|
||||
JKBodyStub,
|
||||
JKStubExpressionImpl(),
|
||||
emptyList(),
|
||||
Visibility.PUBLIC,
|
||||
|
||||
+15
-2
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.j2k.conversions
|
||||
|
||||
import org.jetbrains.kotlin.j2k.ConversionContext
|
||||
import org.jetbrains.kotlin.j2k.tree.*
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.JKKtInitDeclarationImpl
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.JKKtPrimaryConstructorImpl
|
||||
|
||||
class PrimaryConstructorDetectConversion(private val context: ConversionContext) : RecursiveApplicableConversionBase() {
|
||||
@@ -17,6 +18,13 @@ class PrimaryConstructorDetectConversion(private val context: ConversionContext)
|
||||
return recurse(element)
|
||||
}
|
||||
|
||||
private fun <T> List<T>.replace(element: T, replacer: T): List<T> {
|
||||
val mutableList = toMutableList()
|
||||
val index = indexOf(element)
|
||||
mutableList[index] = replacer
|
||||
return mutableList
|
||||
}
|
||||
|
||||
private fun processClass(element: JKClass) {
|
||||
val constructors = element.declarationList.filterIsInstance<JKKtConstructor>()
|
||||
if (constructors.any { it is JKKtPrimaryConstructor }) return
|
||||
@@ -24,15 +32,20 @@ class PrimaryConstructorDetectConversion(private val context: ConversionContext)
|
||||
val delegationCall = primaryConstructorCandidate.delegationCall as? JKDelegationConstructorCall
|
||||
if (delegationCall?.expression is JKThisExpression) return
|
||||
|
||||
element.classBody.declarations -= primaryConstructorCandidate
|
||||
|
||||
primaryConstructorCandidate.invalidate()
|
||||
if (primaryConstructorCandidate.block.statements.isNotEmpty()) {
|
||||
val initDeclaration = JKKtInitDeclarationImpl(primaryConstructorCandidate.block)
|
||||
element.classBody.declarations =
|
||||
element.classBody.declarations.replace(primaryConstructorCandidate, initDeclaration)
|
||||
} else {
|
||||
element.classBody.declarations -= primaryConstructorCandidate
|
||||
}
|
||||
|
||||
val primaryConstructor =
|
||||
JKKtPrimaryConstructorImpl(
|
||||
primaryConstructorCandidate.name,
|
||||
primaryConstructorCandidate.parameters,
|
||||
primaryConstructorCandidate.block,
|
||||
primaryConstructorCandidate.delegationCall,
|
||||
primaryConstructorCandidate.extraModifiers,
|
||||
primaryConstructorCandidate.visibility,
|
||||
|
||||
@@ -192,7 +192,7 @@ class JKKtPrimaryConstructorImpl(
|
||||
|
||||
override var name: JKNameIdentifier by child(name)
|
||||
override var parameters: List<JKParameter> by children(parameters)
|
||||
override var block: JKBlock by child(block)
|
||||
override var block: JKBlock by child(JKBodyStub)
|
||||
override var delegationCall: JKExpression by child(delegationCall)
|
||||
override var typeParameterList: JKTypeParameterList by child(JKTypeParameterListImpl())
|
||||
override var annotationList: JKAnnotationList by child(JKAnnotationListImpl())
|
||||
@@ -238,14 +238,6 @@ class JKKtInitDeclarationImpl(block: JKBlock) : JKKtInitDeclaration, JKBranchEle
|
||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitKtInitDeclaration(this, data)
|
||||
}
|
||||
|
||||
fun JKClass.getOrCreateInitDeclaration(): JKKtInitDeclaration {
|
||||
val existingDeclaration = declarationList.filterIsInstance<JKKtInitDeclaration>().firstOrNull()
|
||||
if (existingDeclaration != null) return existingDeclaration
|
||||
val newDeclaration = JKKtInitDeclarationImpl(JKBlockImpl())
|
||||
classBody.declarations += newDeclaration
|
||||
return newDeclaration
|
||||
}
|
||||
|
||||
class JKKtOperatorExpressionImpl(
|
||||
receiver: JKExpression,
|
||||
override var identifier: JKMethodSymbol,
|
||||
|
||||
Reference in New Issue
Block a user