Optimize FirJavaSyntheticNamesProvider.possibleGetterNamesByPropertyName

This commit is contained in:
Mikhail Glukhikh
2020-12-10 11:25:59 +03:00
parent 6d545fc281
commit dd66da7c65
@@ -7,10 +7,9 @@ package org.jetbrains.kotlin.fir.resolve
import org.jetbrains.kotlin.fir.NoMutableState
import org.jetbrains.kotlin.fir.resolve.calls.FirSyntheticNamesProvider
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
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toUpperCaseAsciiOnly
@NoMutableState
object FirJavaSyntheticNamesProvider : FirSyntheticNamesProvider() {
@@ -18,18 +17,49 @@ object FirJavaSyntheticNamesProvider : FirSyntheticNamesProvider() {
private const val SETTER_PREFIX = "set"
private const val IS_PREFIX = "is"
@Suppress("NOTHING_TO_INLINE")
private inline fun Char.isAsciiUpperCase() = this in 'A'..'Z'
@Suppress("NOTHING_TO_INLINE")
private inline fun Char.isAsciiLowerCase() = this in 'a'..'z'
override 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
if (identifier.isEmpty()) return emptyList()
val result = ArrayList<Name>(3)
val standardName = Name.identifier(GETTER_PREFIX + identifier.capitalizeAsciiOnly())
val length = identifier.length
if (length == 1) {
if (identifier[0].isAsciiLowerCase()) {
// 'x' --> 'getX' but not 'X' --> 'getX'
result += standardName
}
} else if (identifier[1].isAsciiLowerCase()) {
if (identifier[0].isAsciiLowerCase()) {
// 'something' --> 'getSomething' classic case
result += standardName
}
var secondWordStart = 2
while (secondWordStart < length && identifier[secondWordStart].isAsciiLowerCase()) {
secondWordStart++
}
val capitalizedFirstWordName = Name.identifier(
GETTER_PREFIX + identifier.substring(0, secondWordStart).toUpperCaseAsciiOnly() + identifier.substring(secondWordStart)
)
if (secondWordStart >= length || identifier[secondWordStart].isAsciiUpperCase()) {
// 'xyz' --> 'getXYZ' or 'xyzOfSomething' --> 'getXYZOfSomething'
result += capitalizedFirstWordName
}
} else if (length < 3 || !identifier[2].isAsciiUpperCase()) {
// 'xOfSomething' --> 'getXOfSomething' but not 'xYZ' --> 'getXYZ'
result += standardName
}
if (length > IS_PREFIX.length && identifier.startsWith(IS_PREFIX) && !identifier[IS_PREFIX.length].isAsciiLowerCase()) {
// 'isSomething' (but not 'is' or 'issomething')
result += name
}
return result
}
override fun setterNameByGetterName(name: Name): Name? {