Optimize FirJavaSyntheticNamesProvider.possibleGetterNamesByPropertyName
This commit is contained in:
+40
-10
@@ -7,10 +7,9 @@ package org.jetbrains.kotlin.fir.resolve
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.fir.NoMutableState
|
import org.jetbrains.kotlin.fir.NoMutableState
|
||||||
import org.jetbrains.kotlin.fir.resolve.calls.FirSyntheticNamesProvider
|
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.name.Name
|
||||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
|
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
|
||||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeFirstWord
|
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toUpperCaseAsciiOnly
|
||||||
|
|
||||||
@NoMutableState
|
@NoMutableState
|
||||||
object FirJavaSyntheticNamesProvider : FirSyntheticNamesProvider() {
|
object FirJavaSyntheticNamesProvider : FirSyntheticNamesProvider() {
|
||||||
@@ -18,18 +17,49 @@ object FirJavaSyntheticNamesProvider : FirSyntheticNamesProvider() {
|
|||||||
private const val SETTER_PREFIX = "set"
|
private const val SETTER_PREFIX = "set"
|
||||||
private const val IS_PREFIX = "is"
|
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> {
|
override fun possibleGetterNamesByPropertyName(name: Name): List<Name> {
|
||||||
if (name.isSpecial) return emptyList()
|
if (name.isSpecial) return emptyList()
|
||||||
val identifier = name.identifier
|
val identifier = name.identifier
|
||||||
val capitalizedAsciiName = identifier.capitalizeAsciiOnly()
|
if (identifier.isEmpty()) return emptyList()
|
||||||
val capitalizedFirstWordName = identifier.capitalizeFirstWord(asciiOnly = true)
|
val result = ArrayList<Name>(3)
|
||||||
return listOfNotNull(
|
val standardName = Name.identifier(GETTER_PREFIX + identifier.capitalizeAsciiOnly())
|
||||||
Name.identifier(GETTER_PREFIX + capitalizedAsciiName),
|
val length = identifier.length
|
||||||
if (capitalizedFirstWordName == capitalizedAsciiName) null else Name.identifier(GETTER_PREFIX + capitalizedFirstWordName),
|
if (length == 1) {
|
||||||
name.takeIf { identifier.startsWith(IS_PREFIX) }
|
if (identifier[0].isAsciiLowerCase()) {
|
||||||
).filter {
|
// 'x' --> 'getX' but not 'X' --> 'getX'
|
||||||
propertyNameByGetMethodName(it) == name
|
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? {
|
override fun setterNameByGetterName(name: Name): Name? {
|
||||||
|
|||||||
Reference in New Issue
Block a user