[FIR] Extract java synthetic names calculation to platform specific component

This commit is contained in:
Dmitriy Novozhilov
2020-08-28 13:54:05 +03:00
parent 375140ebcc
commit 28e6050551
5 changed files with 70 additions and 36 deletions
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir.resolve.calls
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.FirSessionComponent
import org.jetbrains.kotlin.name.Name
abstract class FirSyntheticNamesProvider : FirSessionComponent {
abstract fun possibleGetterNamesByPropertyName(name: Name): List<Name>
abstract fun setterNameByGetterName(name: Name): Name
}
val FirSession.syntheticNamesProvider: FirSyntheticNamesProvider by FirSession.sessionComponentAccessor()
@@ -19,10 +19,7 @@ import org.jetbrains.kotlin.fir.symbols.StandardClassIds
import org.jetbrains.kotlin.fir.symbols.SyntheticSymbol
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.load.java.propertyNameByGetMethodName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeFirstWord
class SyntheticPropertySymbol(
callableId: CallableId,
@@ -37,38 +34,10 @@ class FirSyntheticPropertiesScope(
val session: FirSession,
private val baseScope: FirTypeScope
) : FirScope() {
companion object {
private const val GETTER_PREFIX = "get"
private const val IS_PREFIX = "is"
fun possibleGetterNamesByPropertyName(name: Name): List<Name> {
if (name.isSpecial) return emptyList()
val identifier = name.identifier
val capitalizedAsciiName = identifier.capitalizeAsciiOnly()
val capitalizedFirstWordName = identifier.capitalizeFirstWord(asciiOnly = true)
return listOfNotNull(
Name.identifier(GETTER_PREFIX + capitalizedAsciiName),
if (capitalizedFirstWordName == capitalizedAsciiName) null else Name.identifier(GETTER_PREFIX + capitalizedFirstWordName),
name.takeIf { identifier.startsWith(IS_PREFIX) }
).filter {
propertyNameByGetMethodName(it) == name
}
}
fun setterNameByGetterName(name: Name): Name {
val identifier = name.identifier
val prefix = when {
identifier.startsWith("get") -> "get"
identifier.startsWith("is") -> "is"
else -> throw IllegalArgumentException()
}
return Name.identifier("set" + identifier.removePrefix(prefix))
}
}
private val syntheticNamesProvider = session.syntheticNamesProvider
override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) {
val getterNames = possibleGetterNamesByPropertyName(name)
val getterNames = syntheticNamesProvider.possibleGetterNamesByPropertyName(name)
for (getterName in getterNames) {
baseScope.processFunctionsByName(getterName) {
checkGetAndCreateSynthetic(name, getterName, it, processor)
@@ -94,7 +63,7 @@ class FirSyntheticPropertiesScope(
var matchingSetter: FirSimpleFunction? = null
if (getterReturnType != null) {
val setterName = setterNameByGetterName(getterName)
val setterName = syntheticNamesProvider.setterNameByGetterName(getterName)
baseScope.processFunctionsByName(setterName, fun(setterSymbol: FirFunctionSymbol<*>) {
if (matchingSetter != null) return
val setter = setterSymbol.fir as? FirSimpleFunction ?: return