Changed synthetic properties naming for getters starting with "is"

This commit is contained in:
Valentin Kipyatkov
2015-07-17 14:28:21 +03:00
parent cf2aa73ee6
commit b6027a0efe
16 changed files with 127 additions and 20 deletions
@@ -45,7 +45,9 @@ interface SyntheticJavaPropertyDescriptor : PropertyDescriptor {
companion object {
fun findByGetterOrSetter(getterOrSetter: FunctionDescriptor, resolutionScope: JetScope): SyntheticJavaPropertyDescriptor? {
val name = getterOrSetter.getName()
if (propertyNameByGetMethodName(name) == null && propertyNameBySetMethodName(name) == null) return null // optimization
if (name.isSpecial()) return null
val identifier = name.getIdentifier()
if (!identifier.startsWith("get") && !identifier.startsWith("is") && !identifier.startsWith("set")) return null // optimization
val owner = getterOrSetter.getContainingDeclaration()
if (owner !is JavaClassDescriptor) return null
@@ -56,15 +58,22 @@ interface SyntheticJavaPropertyDescriptor : PropertyDescriptor {
}
fun propertyNameByGetMethodName(methodName: Name): Name?
= propertyNameFromAccessorMethodName(methodName, "get") ?: propertyNameFromAccessorMethodName(methodName, "is")
= propertyNameFromAccessorMethodName(methodName, "get") ?: propertyNameFromAccessorMethodName(methodName, "is", removePrefix = false)
fun propertyNameBySetMethodName(methodName: Name): Name?
= propertyNameFromAccessorMethodName(methodName, "set")
fun propertyNameBySetMethodName(methodName: Name, withIsPrefix: Boolean): Name?
= propertyNameFromAccessorMethodName(methodName, "set", addPrefix = if (withIsPrefix) "is" else null)
private fun propertyNameFromAccessorMethodName(methodName: Name, prefix: String): Name? {
private fun propertyNameFromAccessorMethodName(methodName: Name, prefix: String, removePrefix: Boolean = true, addPrefix: String? = null): Name? {
if (methodName.isSpecial()) return null
val identifier = methodName.getIdentifier()
if (!identifier.startsWith(prefix)) return null
if (addPrefix != null) {
assert(removePrefix)
return Name.identifier(addPrefix + identifier.removePrefix(prefix))
}
if (!removePrefix) return methodName
val name = Introspector.decapitalize(identifier.removePrefix(prefix))
if (!Name.isValidIdentifier(name)) return null
return Name.identifier(name)
@@ -103,7 +112,7 @@ class JavaSyntheticExtensionsScope(storageManager: StorageManager) : JetScope by
.singleOrNull { isGoodGetMethod(it) } ?: return null
val propertyType = getMethod.getReturnType() ?: return null
val setMethod = memberScope.getFunctions(possibleSetMethodName(name)).singleOrNull { isGoodSetMethod(it, propertyType) }
val setMethod = memberScope.getFunctions(setMethodName(getMethod.getName())).singleOrNull { isGoodSetMethod(it, propertyType) }
return MyPropertyDescriptor(javaClass, getMethod, setMethod, name, propertyType, type)
}
@@ -177,12 +186,24 @@ class JavaSyntheticExtensionsScope(storageManager: StorageManager) : JetScope by
//TODO: reuse code with generation?
private fun possibleGetMethodNames(propertyName: Name): Collection<Name> {
val capitalized = propertyName.getIdentifier().capitalize()
return listOf(Name.identifier("get" + capitalized), Name.identifier("is" + capitalized))
val identifier = propertyName.getIdentifier()
val getPrefixName = Name.identifier("get" + identifier.capitalize())
if (identifier.startsWith("is")) {
return listOf(propertyName, getPrefixName)
}
else {
return listOf(getPrefixName)
}
}
private fun possibleSetMethodName(propertyName: Name): Name {
return Name.identifier("set" + propertyName.getIdentifier().capitalize())
private fun setMethodName(getMethodName: Name): Name {
val identifier = getMethodName.getIdentifier()
val prefix = when {
identifier.startsWith("get") -> "get"
identifier.startsWith("is") -> "is"
else -> throw IllegalArgumentException()
}
return Name.identifier("set" + identifier.removePrefix(prefix).capitalize())
}
private class MyPropertyDescriptor(
@@ -1,13 +1,30 @@
// FILE: KotlinFile.kt
fun foo(javaClass: JavaClass) {
javaClass.something = !javaClass.something
javaClass.isSomething = !javaClass.isSomething
javaClass.isSomething2 = !javaClass.isSomething2
javaClass.<!UNRESOLVED_REFERENCE!>something<!>
javaClass.<!FUNCTION_CALL_EXPECTED!>isSomethingWrong<!>
javaClass.<!UNRESOLVED_REFERENCE!>somethingWrong<!>
}
// FILE: JavaClass.java
public class JavaClass {
public boolean isSomething() { return true; }
public void setSomething(boolean value) { }
public int isSomethingWrong() { return 1; }
public boolean isSomething() {
return true;
}
public void setSomething(boolean value) {
}
public boolean getIsSomething2() {
return true;
}
public void setIsSomething2(boolean value) {
}
public int isSomethingWrong() {
return 1;
}
}
@@ -5,9 +5,11 @@ internal fun foo(/*0*/ javaClass: JavaClass): kotlin.Unit
public open class JavaClass {
public constructor JavaClass()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open fun getIsSomething2(): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open fun isSomething(): kotlin.Boolean
public open fun isSomethingWrong(): kotlin.Int
public open fun setIsSomething2(/*0*/ value: kotlin.Boolean): kotlin.Unit
public open fun setSomething(/*0*/ value: kotlin.Boolean): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}