Search in all superclasses in quickfix for NOTHING_TO_OVERRIDE.

This commit is contained in:
Michał Sapalski
2013-05-02 16:26:07 +02:00
committed by Andrey Breslav
parent 0e96e83154
commit 68cd832831
8 changed files with 78 additions and 21 deletions
@@ -26,8 +26,6 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.renderer.DescriptorRenderer;
@@ -46,25 +44,13 @@ public class JavaToKotlinMethodMap {
private JavaToKotlinMethodMap() {
}
@NotNull
private static Set<ClassDescriptor> getAllSuperClasses(@NotNull ClassDescriptor klass) {
Set<JetType> allSupertypes = TypeUtils.getAllSupertypes(klass.getDefaultType());
Set<ClassDescriptor> allSuperclasses = Sets.newHashSet();
for (JetType supertype : allSupertypes) {
ClassDescriptor superclass = TypeUtils.getClassDescriptor(supertype);
assert superclass != null;
allSuperclasses.add(superclass);
}
return allSuperclasses;
}
@NotNull
public List<FunctionDescriptor> getFunctions(@NotNull PsiMethod psiMethod, @NotNull ClassDescriptor containingClass) {
ImmutableCollection<ClassData> classDatas = mapContainer.map.get(psiMethod.getContainingClass().getQualifiedName());
List<FunctionDescriptor> result = Lists.newArrayList();
Set<ClassDescriptor> allSuperClasses = getAllSuperClasses(containingClass);
Set<ClassDescriptor> allSuperClasses = DescriptorUtils.getAllSuperClasses(containingClass);
String serializedPsiMethod = serializePsiMethod(psiMethod);
for (ClassData classData : classDatas) {
@@ -18,6 +18,7 @@ package org.jetbrains.jet.lang.resolve;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
@@ -545,4 +546,16 @@ public class DescriptorUtils {
return "values".equals(functionDescriptor.getName().getName())
&& methodTypeParameters.isEmpty();
}
@NotNull
public static Set<ClassDescriptor> getAllSuperClasses(@NotNull ClassDescriptor klass) {
Set<JetType> allSupertypes = TypeUtils.getAllSupertypes(klass.getDefaultType());
Set<ClassDescriptor> allSuperclasses = Sets.newHashSet();
for (JetType supertype : allSupertypes) {
ClassDescriptor superclass = TypeUtils.getClassDescriptor(supertype);
assert superclass != null;
allSuperclasses.add(superclass);
}
return allSuperclasses;
}
}
@@ -31,6 +31,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.impl.FunctionDescriptorUtil;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
@@ -75,7 +76,7 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
}
@NotNull
private String getFunctionSignatureString(@NotNull FunctionDescriptor functionSignature, boolean shortTypeNames) {
private static String getFunctionSignatureString(@NotNull FunctionDescriptor functionSignature, boolean shortTypeNames) {
return CodeInsightUtils.createFunctionSignatureStringFromDescriptor(
functionSignature, shortTypeNames);
}
@@ -87,7 +88,7 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
}
@Override
public void invoke(@NotNull final Project project, @NotNull final Editor editor, PsiFile file)
protected void invoke(@NotNull final Project project, @NotNull final Editor editor, JetFile file)
throws IncorrectOperationException {
CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
@Override
@@ -106,7 +107,7 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
* Computes all the signatures a 'functionElement' could be changed to in order to remove NOTHING_TO_OVERRIDE error.
*/
@NotNull
private List<FunctionDescriptor> computePossibleSignatures(JetNamedFunction functionElement) {
private static List<FunctionDescriptor> computePossibleSignatures(JetNamedFunction functionElement) {
BindingContext context = KotlinCacheManagerUtil.getDeclarationsFromProject(functionElement).getBindingContext();
FunctionDescriptor functionDescriptor = context.get(BindingContext.FUNCTION, functionElement);
if (functionDescriptor == null) return Lists.newArrayList();
@@ -234,7 +235,7 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
Name name = functionDescriptor.getName();
for (ClassDescriptor superclass : DescriptorUtils.getSuperclassDescriptors(classDescriptor)) {
for (ClassDescriptor superclass : DescriptorUtils.getAllSuperClasses(classDescriptor)) {
JetType type = superclass.getDefaultType();
JetScope scope = type.getMemberScope();
for (FunctionDescriptor function : scope.getFunctions(name)) {
@@ -0,0 +1,11 @@
// "Change function signature to 'override fun f(a: Int)'" "true"
trait A {
fun f(a: Int)
}
trait B : A {
}
class C : B {
override fun f(a: Int) {}
}
@@ -0,0 +1,11 @@
// "Change function signature to 'override fun f(a: Int)'" "true"
open class A {
open fun f(a: Int) {}
}
open class B : A() {
}
class C : B() {
<caret>override fun f(a: Int) {}
}
@@ -0,0 +1,11 @@
// "Change function signature to 'override fun f(a: Int)'" "true"
trait A {
fun f(a: Int)
}
trait B : A {
}
class C : B {
<caret>override fun f() {}
}
@@ -0,0 +1,11 @@
// "Change function signature to 'override fun f(a: Int)'" "true"
open class A {
open fun f(a: Int) {}
}
open class B : A() {
}
class C : B() {
<caret>override fun f() {}
}
@@ -16,14 +16,17 @@
package org.jetbrains.jet.plugin.quickfix;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@@ -848,6 +851,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/override/nothingToOverride/beforeAddParameterPreserveVisibility.kt");
}
@TestMetadata("beforeAddParameterTwoSupertraits.kt")
public void testAddParameterTwoSupertraits() throws Exception {
doTest("idea/testData/quickfix/override/nothingToOverride/beforeAddParameterTwoSupertraits.kt");
}
@TestMetadata("beforeAddParameterTwoSupertypes.kt")
public void testAddParameterTwoSupertypes() throws Exception {
doTest("idea/testData/quickfix/override/nothingToOverride/beforeAddParameterTwoSupertypes.kt");
}
public void testAllFilesPresentInNothingToOverride() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/override/nothingToOverride"), Pattern.compile("^before(\\w+)\\.kt$"), true);
}