New J2K: Add detect primary constructors conversion
This commit is contained in:
committed by
Ilya Kirillov
parent
44d2a6f432
commit
2aafeda33b
+16
-12
@@ -20,27 +20,31 @@ class PrimaryConstructorDetectConversion(private val context: ConversionContext)
|
|||||||
private fun processClass(element: JKClass) {
|
private fun processClass(element: JKClass) {
|
||||||
val constructors = element.declarationList.filterIsInstance<JKKtConstructor>()
|
val constructors = element.declarationList.filterIsInstance<JKKtConstructor>()
|
||||||
if (constructors.any { it is JKKtPrimaryConstructor }) return
|
if (constructors.any { it is JKKtPrimaryConstructor }) return
|
||||||
// TODO: Detecting primary for multiple constructors
|
val primaryConstructorCandidate = detectPrimaryConstructor(constructors) ?: return
|
||||||
// TODO: Detecting primary constructors with field initializers
|
val delegationCall = primaryConstructorCandidate.delegationCall as? JKDelegationConstructorCall
|
||||||
val single = constructors.singleOrNull { it.block.statements.isEmpty() } ?: return
|
|
||||||
val delegationCall = single.delegationCall as? JKDelegationConstructorCall
|
|
||||||
if (delegationCall?.expression is JKThisExpression) return
|
if (delegationCall?.expression is JKThisExpression) return
|
||||||
|
|
||||||
element.declarationList -= single
|
element.declarationList -= primaryConstructorCandidate
|
||||||
|
|
||||||
single.invalidate()
|
primaryConstructorCandidate.invalidate()
|
||||||
|
|
||||||
val primaryConstructor =
|
val primaryConstructor =
|
||||||
JKKtPrimaryConstructorImpl(
|
JKKtPrimaryConstructorImpl(
|
||||||
single.name,
|
primaryConstructorCandidate.name,
|
||||||
single.parameters,
|
primaryConstructorCandidate.parameters,
|
||||||
single.block,
|
primaryConstructorCandidate.block,
|
||||||
single.modifierList,
|
primaryConstructorCandidate.modifierList,
|
||||||
single.delegationCall
|
primaryConstructorCandidate.delegationCall
|
||||||
)
|
)
|
||||||
|
|
||||||
context.symbolProvider.transferSymbol(primaryConstructor, single)
|
context.symbolProvider.transferSymbol(primaryConstructor, primaryConstructorCandidate)
|
||||||
|
|
||||||
element.declarationList += primaryConstructor
|
element.declarationList += primaryConstructor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun detectPrimaryConstructor(constructors: List<JKKtConstructor>): JKKtConstructor? {
|
||||||
|
val constructorsWithoutOtherConstructorCall =
|
||||||
|
constructors.filterNot { (it.delegationCall as? JKDelegationConstructorCall)?.expression is JKThisExpression }
|
||||||
|
return constructorsWithoutOtherConstructorCall.singleOrNull()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user