Light Classes: Support property accessors with non-conventional names

#KT-13032 Fixed
This commit is contained in:
Alexey Sedunov
2016-08-15 20:13:01 +03:00
parent 94c4b426f5
commit fbd6edce92
16 changed files with 126 additions and 5 deletions
@@ -198,7 +198,9 @@ object LightClassUtil {
val additionalAccessors = arrayListOf<PsiMethod>()
for (wrapper in getPsiMethodWrappers(ktDeclaration, true)) {
if (JvmAbi.isSetterName(wrapper.name)) {
if (wrapper !is KtLightMethod) continue
if (wrapper.isSetter) {
if (setterWrapper == null || setterWrapper === specialSetter) {
setterWrapper = wrapper
}
@@ -232,3 +232,16 @@ fun KtLightMethod.isTraitFakeOverride(): Boolean {
// Method was generated from declaration in some other trait
return (parentOfMethodOrigin != null && thisClassDeclaration !== parentOfMethodOrigin && KtPsiUtil.isTrait(parentOfMethodOrigin))
}
fun KtLightMethod.isAccessor(getter: Boolean): Boolean {
val origin = kotlinOrigin as? KtCallableDeclaration ?: return false
if (origin !is KtProperty && origin !is KtParameter) return false
val expectedParametersCount = (if (getter) 0 else 1) + (if (origin.receiverTypeReference != null) 1 else 0)
return parameterList.parametersCount == expectedParametersCount
}
val KtLightMethod.isGetter: Boolean
get() = isAccessor(true)
val KtLightMethod.isSetter: Boolean
get() = isAccessor(false)
@@ -28,7 +28,6 @@ import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
@@ -100,8 +99,8 @@ internal fun computeAnnotations(lightElement: PsiModifierList,
val descriptor = declaration?.let { LightClassGenerationSupport.getInstance(lightElement.project).resolveToDescriptor(it) }
val annotatedDescriptor = when {
descriptor !is PropertyDescriptor || lightOwner !is KtLightMethod -> descriptor
JvmAbi.isGetterName(lightOwner.name) -> descriptor.getter
JvmAbi.isSetterName(lightOwner.name) -> descriptor.setter
lightOwner.isGetter -> descriptor.getter
lightOwner.isSetter -> descriptor.setter
else -> descriptor
}
val ktAnnotations = annotatedDescriptor?.annotations?.getAllAnnotations() ?: emptyList()
@@ -171,7 +171,7 @@ fun propertyNameByAccessor(name: String, accessor: KtLightMethod): String? {
return when {
JvmAbi.isGetterName(name) -> propertyNameByGetMethodName(methodName)
JvmAbi.isSetterName(name) -> propertyNameBySetMethodName(methodName, propertyName.startsWith("is"))
else -> null
else -> methodName
}?.asString()
}