Refactor light methods: drop KotlinLightMethodForTraitFake and KotlinNoOriginLightMethod

Move two remaining meaningful implementation into one place
Move code that choose which implementation to use out of KotlinWrappingLightClass
This commit is contained in:
Pavel V. Talanov
2015-10-29 15:25:45 +03:00
parent bca516bac3
commit f8efdacefd
8 changed files with 175 additions and 286 deletions
@@ -1,51 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.asJava
import com.intellij.psi.PsiAnnotationMethod
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiManager
import org.jetbrains.kotlin.psi.KtDeclaration
class KotlinAnnotationLightMethod(
manager: PsiManager,
delegate: PsiAnnotationMethod,
origin: KtDeclaration,
containingClass: PsiClass
) : KotlinLightMethodForDeclaration(manager, delegate, origin, containingClass), PsiAnnotationMethod {
override fun getDefaultValue() = getDelegate().defaultValue
override fun getDelegate() = super.getDelegate() as PsiAnnotationMethod
override fun copy() = KotlinAnnotationLightMethod(manager, getDelegate(), getOrigin(), containingClass!!)
}
@@ -16,7 +16,165 @@
package org.jetbrains.kotlin.asJava
import com.intellij.psi.PsiMethod
import org.jetbrains.kotlin.psi.KtDeclaration
import com.intellij.core.JavaCoreBundle
import com.intellij.psi.*
import com.intellij.psi.impl.light.LightMethod
import com.intellij.psi.scope.PsiScopeProcessor
import com.intellij.psi.util.*
import com.intellij.util.IncorrectOperationException
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.psi.*
public interface KotlinLightMethod: PsiMethod, KotlinLightElement<KtDeclaration, PsiMethod>
sealed class KotlinLightMethodImpl(
private val delegate: PsiMethod,
private val origin: KtDeclaration?,
containingClass: KotlinLightClass
): LightMethod(delegate.manager, delegate, containingClass), KotlinLightMethod {
override fun getContainingClass(): KotlinLightClass = super.getContainingClass() as KotlinLightClass
private val paramsList: CachedValue<PsiParameterList> by lazy {
val cacheManager = CachedValuesManager.getManager(delegate.project)
cacheManager.createCachedValue<PsiParameterList>({
val parameterBuilder = LightParameterListBuilder(manager, KotlinLanguage.INSTANCE, this)
for ((index, parameter) in delegate.parameterList.parameters.withIndex()) {
parameterBuilder.addParameter(KotlinLightParameter(parameter, index, this))
}
CachedValueProvider.Result.create(parameterBuilder, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT)
}, false)
}
private val typeParamsList: CachedValue<PsiTypeParameterList> by lazy {
val cacheManager = CachedValuesManager.getManager(delegate.project)
cacheManager.createCachedValue<PsiTypeParameterList>({
val list = if (origin is KtClassOrObject) {
KotlinLightTypeParameterListBuilder(manager)
}
else if (origin == null) {
delegate.typeParameterList
}
else {
LightClassUtil.buildLightTypeParameterList(this@KotlinLightMethodImpl, origin)
}
CachedValueProvider.Result.create(list, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT)
}, false)
}
override fun getNavigationElement(): PsiElement = origin ?: super.getNavigationElement()
override fun getOriginalElement(): PsiElement = origin ?: super.getOriginalElement()
override fun getDelegate() = delegate
override fun getOrigin() = origin
override fun getParent(): PsiElement? = containingClass
override fun accept(visitor: PsiElementVisitor) {
if (visitor is JavaElementVisitor) {
visitor.visitMethod(this)
}
else {
visitor.visitElement(this)
}
}
override fun setName(name: String): PsiElement? {
val toRename = origin as? PsiNamedElement ?: throwCanNotModify()
toRename.setName(name)
return this
}
public override fun delete() {
origin?.let {
if (it.isValid) {
it.delete()
}
} ?: throwCanNotModify()
}
private fun throwCanNotModify(): Nothing {
throw IncorrectOperationException(JavaCoreBundle.message("psi.error.attempt.to.edit.class.file"))
}
override fun getParameterList() = paramsList.value
override fun getTypeParameterList() = typeParamsList.value
override fun getTypeParameters(): Array<PsiTypeParameter> =
typeParameterList?.let { it.typeParameters } ?: PsiTypeParameter.EMPTY_ARRAY
override fun getSignature(substitutor: PsiSubstitutor): MethodSignature {
if (substitutor == PsiSubstitutor.EMPTY) {
return delegate.getSignature(substitutor)
}
return MethodSignatureBackedByPsiMethod.create(this, substitutor)
}
override fun copy(): PsiElement {
return Factory.create(delegate, origin?.copy() as? KtDeclaration, containingClass)
}
override fun getUseScope() = origin?.useScope ?: super.getUseScope()
override fun getLanguage() = KotlinLanguage.INSTANCE
override fun processDeclarations(processor: PsiScopeProcessor, state: ResolveState, lastParent: PsiElement?, place: PsiElement): Boolean {
return typeParameters.all { processor.execute(it, state) }
}
override fun isEquivalentTo(another: PsiElement?): Boolean {
if (another is KotlinLightMethod && origin == another.getOrigin() && delegate == another.getDelegate()) {
return true
}
return super.isEquivalentTo(another)
}
override fun equals(other: Any?): Boolean =
other is KotlinLightMethod &&
name == other.name &&
origin == other.getOrigin() &&
containingClass == other.containingClass &&
delegate == other.getDelegate()
override fun hashCode(): Int = ((name.hashCode() * 31 + (origin?.hashCode() ?: 0)) * 31 + containingClass.hashCode()) * 31 + delegate.hashCode()
override fun toString(): String = "${this.javaClass.simpleName}:$name"
private class KotlinLightMethodForDeclaration(
delegate: PsiMethod, origin: KtDeclaration?, containingClass: KotlinLightClass
) : KotlinLightMethodImpl(delegate, origin, containingClass)
private class KotlinAnnotationLightMethod(
delegate: PsiAnnotationMethod,
origin: KtDeclaration?,
containingClass: KotlinLightClass
) : KotlinLightMethodImpl(delegate, origin, containingClass), PsiAnnotationMethod {
override fun getDefaultValue() = getDelegate().defaultValue
override fun getDelegate() = super.getDelegate() as PsiAnnotationMethod
}
companion object Factory {
fun create(
delegate: PsiMethod, origin: KtDeclaration?, containingClass: KotlinLightClass
): KotlinLightMethodImpl {
return when (delegate) {
is PsiAnnotationMethod -> KotlinAnnotationLightMethod(delegate, origin, containingClass)
else -> KotlinLightMethodForDeclaration(delegate, origin, containingClass)
}
}
}
}
fun KotlinLightMethod.isTraitFakeOverride(): Boolean {
val methodOrigin = this.getOrigin()
if (!(methodOrigin is KtNamedFunction || methodOrigin is KtPropertyAccessor || methodOrigin is KtProperty)) {
return false
}
val parentOfMethodOrigin = PsiTreeUtil.getParentOfType(methodOrigin, KtClassOrObject::class.java)
val thisClassDeclaration = (this.containingClass as KotlinLightClass).getOrigin()
// Method was generated from declaration in some other trait
return (parentOfMethodOrigin != null && thisClassDeclaration !== parentOfMethodOrigin && KtPsiUtil.isTrait(parentOfMethodOrigin))
}
@@ -1,140 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.asJava
import com.intellij.psi.impl.light.LightMethod
import com.intellij.psi.*
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.idea.KotlinLanguage
import kotlin.properties.Delegates
import com.intellij.psi.util.CachedValuesManager
import com.intellij.psi.util.PsiModificationTracker
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValue
import org.jetbrains.kotlin.psi.KtClassOrObject
import com.intellij.psi.search.SearchScope
import com.intellij.lang.Language
import com.intellij.psi.scope.PsiScopeProcessor
import com.intellij.psi.util.MethodSignature
import com.intellij.psi.util.MethodSignatureBackedByPsiMethod
open public class KotlinLightMethodForDeclaration(
manager: PsiManager,
private val delegate: PsiMethod,
private val origin: KtDeclaration,
containingClass: PsiClass
): LightMethod(manager, delegate, containingClass), KotlinLightMethod {
private val paramsList: CachedValue<PsiParameterList> by lazy {
val cacheManager = CachedValuesManager.getManager(delegate.getProject())
cacheManager.createCachedValue<PsiParameterList>({
val parameterBuilder = LightParameterListBuilder(getManager(), KotlinLanguage.INSTANCE, this)
for ((index, parameter) in delegate.getParameterList().getParameters().withIndex()) {
parameterBuilder.addParameter(KotlinLightParameter(parameter, index, this))
}
CachedValueProvider.Result.create(parameterBuilder, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT)
}, false)
}
private val typeParamsList: CachedValue<PsiTypeParameterList> by lazy {
val cacheManager = CachedValuesManager.getManager(delegate.getProject())
cacheManager.createCachedValue<PsiTypeParameterList>({
val list = if (origin is KtClassOrObject) {
KotlinLightTypeParameterListBuilder(getManager())
}
else {
LightClassUtil.buildLightTypeParameterList(this@KotlinLightMethodForDeclaration, origin)
}
CachedValueProvider.Result.create(list, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT)
}, false)
}
override fun getNavigationElement(): PsiElement = getOrigin()
override fun getOriginalElement(): PsiElement = origin
override fun getDelegate(): PsiMethod = delegate
override fun getOrigin(): KtDeclaration = origin
override fun getParent(): PsiElement? = getContainingClass()
override fun accept(visitor: PsiElementVisitor) {
if (visitor is JavaElementVisitor) {
visitor.visitMethod(this)
}
else {
visitor.visitElement(this)
}
}
override fun setName(name: String): PsiElement? {
(origin as PsiNamedElement).setName(name)
return this
}
public override fun delete() {
if (origin.isValid()) {
origin.delete()
}
}
override fun getParameterList(): PsiParameterList = paramsList.getValue()!!
override fun getTypeParameterList(): PsiTypeParameterList? = typeParamsList.getValue()
override fun getTypeParameters(): Array<PsiTypeParameter> =
getTypeParameterList()?.let { it.getTypeParameters() } ?: PsiTypeParameter.EMPTY_ARRAY
override fun getSignature(substitutor: PsiSubstitutor): MethodSignature {
if (substitutor == PsiSubstitutor.EMPTY) {
return delegate.getSignature(substitutor)
}
return MethodSignatureBackedByPsiMethod.create(this, substitutor)
}
override fun copy(): PsiElement {
return KotlinLightMethodForDeclaration(getManager()!!, delegate, origin.copy() as KtDeclaration, getContainingClass()!!)
}
override fun getUseScope(): SearchScope = origin.getUseScope()
override fun getLanguage(): Language = KotlinLanguage.INSTANCE
override fun processDeclarations(processor: PsiScopeProcessor, state: ResolveState, lastParent: PsiElement?, place: PsiElement): Boolean {
return getTypeParameters().all { processor.execute(it, state) }
}
override fun isEquivalentTo(another: PsiElement?): Boolean {
if (another is KotlinLightMethod && origin == another.getOrigin() && delegate == another.getDelegate()) {
return true
}
return super<LightMethod>.isEquivalentTo(another)
}
override fun equals(other: Any?): Boolean =
other is KotlinLightMethodForDeclaration &&
getName() == other.getName() &&
origin == other.origin &&
getContainingClass() == other.getContainingClass() &&
delegate == other.getDelegate()
override fun hashCode(): Int = ((getName().hashCode() * 31 + origin.hashCode()) * 31 + getContainingClass()!!.hashCode()) * 31 + delegate.hashCode()
override fun toString(): String = "KotlinLightMethodForDeclaration:" + getName()
}
@@ -1,38 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.asJava
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiMethod
import org.jetbrains.kotlin.psi.KtDeclaration
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
public class KotlinLightMethodForTraitFakeOverride(
manager: PsiManager,
private val delegate: PsiMethod,
private val origin: KtDeclaration,
containingClass: PsiClass
) : KotlinLightMethodForDeclaration(manager, delegate, origin, containingClass) {
override fun getDelegate(): PsiMethod = delegate
override fun getOrigin(): KtDeclaration = origin
override fun copy(): PsiElement {
return KotlinLightMethodForTraitFakeOverride(getManager(), delegate, origin.copy() as KtDeclaration, getContainingClass()!!)
}
}
@@ -16,25 +16,10 @@
package org.jetbrains.kotlin.asJava
import com.intellij.psi.*
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiField
import com.intellij.psi.PsiManager
import com.intellij.psi.impl.light.LightField
import com.intellij.psi.impl.light.LightMethod
public class KotlinNoOriginLightMethod(manager: PsiManager, method: PsiMethod, containingClass: PsiClass) :
LightMethod(manager, method, containingClass) {
override fun toString() = "KotlinNoOriginLightMethod:${getName()}" + if (isConstructor()) " ctor" else ""
override fun accept(visitor: PsiElementVisitor) {
if (visitor is JavaElementVisitor) {
visitor.visitMethod(this)
}
else {
visitor.visitElement(this)
}
}
}
public class KotlinNoOriginLightField(manager: PsiManager, field: PsiField, containingClass: PsiClass) :
LightField(manager, field, containingClass) {
@@ -153,20 +153,7 @@ public abstract class KotlinWrappingLightClass extends AbstractLightClass implem
declaration = PsiTreeUtil.getParentOfType(declaration, KtProperty.class);
}
if (declaration != null) {
if (isTraitFakeOverride(declaration)) {
return new KotlinLightMethodForTraitFakeOverride(myManager, method, declaration, KotlinWrappingLightClass.this);
}
else if (method instanceof PsiAnnotationMethod) {
return new KotlinAnnotationLightMethod(myManager, (PsiAnnotationMethod) method, declaration,
KotlinWrappingLightClass.this);
}
else {
return new KotlinLightMethodForDeclaration(myManager, method, declaration, KotlinWrappingLightClass.this);
}
}
return new KotlinNoOriginLightMethod(myManager, method, KotlinWrappingLightClass.this);
return KotlinLightMethodImpl.Factory.create(method, declaration, KotlinWrappingLightClass.this);
}
});
}
@@ -194,20 +181,6 @@ public abstract class KotlinWrappingLightClass extends AbstractLightClass implem
return KotlinLanguage.INSTANCE;
}
private boolean isTraitFakeOverride(@NotNull KtDeclaration originMethodDeclaration) {
if (!(originMethodDeclaration instanceof KtNamedFunction ||
originMethodDeclaration instanceof KtPropertyAccessor ||
originMethodDeclaration instanceof KtProperty)) {
return false;
}
KtClassOrObject parentOfMethodOrigin = PsiTreeUtil.getParentOfType(originMethodDeclaration, KtClassOrObject.class);
KtClassOrObject thisClassDeclaration = getOrigin();
// Method was generated from declaration in some other trait
return (parentOfMethodOrigin != null && thisClassDeclaration != parentOfMethodOrigin && KtPsiUtil.isTrait(parentOfMethodOrigin));
}
@Override
public ItemPresentation getPresentation() {
return ItemPresentationProviders.getItemPresentation(this);
@@ -22,10 +22,7 @@ import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiReference
import com.intellij.psi.search.SearchScope
import com.intellij.psi.util.MethodSignatureUtil
import org.jetbrains.kotlin.asJava.KotlinLightElement
import org.jetbrains.kotlin.asJava.KotlinNoOriginLightMethod
import org.jetbrains.kotlin.asJava.toLightMethods
import org.jetbrains.kotlin.asJava.unwrapped
import org.jetbrains.kotlin.asJava.*
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getJavaMethodDescriptor
@@ -131,7 +128,7 @@ private fun PsiElement.processDelegationCallKotlinConstructorUsages(scope: Searc
private fun PsiElement.processDelegationCallJavaConstructorUsages(scope: SearchScope, process: (KtCallElement) -> Boolean): Boolean {
if (this is KotlinLightElement<*, *>) return true
// TODO: Temporary hack to avoid NPE while KotlinNoOriginLightMethod is around
if (this is KotlinNoOriginLightMethod) return true
if (this is KotlinLightMethod && this.getOrigin() == null) return true
if (!(this is PsiMethod && isConstructor())) return true
val klass = getContainingClass() ?: return true
val descriptor = getJavaMethodDescriptor() as? ConstructorDescriptor ?: return true
@@ -38,11 +38,16 @@ import com.intellij.psi.util.PsiUtil
import com.intellij.util.CommonProcessors
import com.intellij.util.Processor
import gnu.trove.THashSet
import org.jetbrains.kotlin.asJava.KotlinLightMethodForTraitFakeOverride
import org.jetbrains.kotlin.asJava.KotlinLightMethod
import org.jetbrains.kotlin.asJava.isTraitFakeOverride
import java.awt.event.MouseEvent
import java.util.*
import javax.swing.JComponent
private fun PsiMethod.isMethodWithDeclarationInOtherClass(): Boolean {
return this is KotlinLightMethod && this.isTraitFakeOverride()
}
internal fun <T> getOverriddenDeclarations(mappingToJava: MutableMap<PsiMethod, T>, classes: Set<PsiClass>): Set<T> {
val overridden = HashSet<T>()
for (aClass in classes) {
@@ -50,7 +55,7 @@ internal fun <T> getOverriddenDeclarations(mappingToJava: MutableMap<PsiMethod,
override fun process(pair: Pair<PsiMethod, PsiMethod>?): Boolean {
ProgressManager.checkCanceled()
if (pair!!.getSecond() !is KotlinLightMethodForTraitFakeOverride) {
if (!pair!!.getSecond().isMethodWithDeclarationInOtherClass()) {
val superMethod = pair.getFirst()
val declaration = mappingToJava.get(superMethod)
@@ -80,7 +85,7 @@ public fun getOverriddenMethodTooltip(method: PsiMethod): String? {
val comparator = MethodCellRenderer(false).getComparator()
val overridingJavaMethods = processor.getCollection().filter { it !is KotlinLightMethodForTraitFakeOverride } sortedWith (comparator)
val overridingJavaMethods = processor.getCollection().filter { !it.isMethodWithDeclarationInOtherClass() } sortedWith (comparator)
if (overridingJavaMethods.isEmpty()) return null
val start = if (isAbstract) DaemonBundle.message("method.is.implemented.header") else DaemonBundle.message("method.is.overriden.header")
@@ -103,7 +108,7 @@ public fun navigateToOverriddenMethod(e: MouseEvent?, method: PsiMethod) {
return
}
var overridingJavaMethods = processor.getCollection().filter { it !is KotlinLightMethodForTraitFakeOverride }
var overridingJavaMethods = processor.getCollection().filter { !it.isMethodWithDeclarationInOtherClass() }
if (overridingJavaMethods.isEmpty()) return
val showMethodNames = !PsiUtil.allMethodsHaveSameSignature(overridingJavaMethods.toTypedArray())