QuickFix for RETURN_TYPE_MISMATCH_ON_OVERRIDE and PROPERTY_TYPE_MISMATCH_ON_OVERRIDE.

This commit is contained in:
Wojciech Lopata
2013-02-09 11:03:41 +01:00
committed by Andrey Breslav
parent a880b2a699
commit 237be539b3
20 changed files with 407 additions and 2 deletions
@@ -52,6 +52,10 @@ rename.parameter.to.match.overridden.method=Rename parameter to match overridden
rename.family=Rename
add.semicolon.after.invocation=Add semicolon after invocation of ''{0}''
add.semicolon.family=Add Semicolon
change.return.type.to.match.overridden.method=Change return type to ''{0}''
remove.return.type.to.match.overridden.method=Remove explicitly specified return type to match overridden method
change.property.type.to.match.overridden.property=Change property type to ''{0}''
change.type.family=Change Type
add.kotlin.signature.action.family.name=Specify Custom Kotlin Signature
add.kotlin.signature.action.text=Specify custom Kotlin signature
@@ -0,0 +1,90 @@
/*
* Copyright 2010-2013 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.jet.plugin.quickfix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.psi.JetTypeReference;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.plugin.JetBundle;
import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManager;
import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction;
public class ChangePropertyTypeToMatchOverriddenPropertyFix extends JetIntentionAction<JetProperty> {
private JetType matchingType;
public ChangePropertyTypeToMatchOverriddenPropertyFix(@NotNull JetProperty property) {
super(property);
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!super.isAvailable(project, editor, file)) {
return false;
}
BindingContext context = KotlinCacheManager.getInstance(project).getDeclarationsFromProject().getBindingContext();
matchingType = QuickFixUtil.findLowerBoundOfOverriddenCallablesReturnTypes(context, element);
return matchingType != null;
}
@NotNull
@Override
public String getText() {
return JetBundle.message("change.property.type.to.match.overridden.property", matchingType);
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("change.type.family");
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
JetTypeReference typeReference = element.getTypeRef();
if (typeReference == null) {
SpecifyTypeExplicitlyAction.addTypeAnnotation(project, editor, element, matchingType);
}
else {
PsiElement newType = JetPsiFactory.createType(project, matchingType.toString());
typeReference.replace(newType);
}
}
@NotNull
public static JetIntentionActionFactory createFactory() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
JetProperty property = QuickFixUtil.getParentElementOfType(diagnostic, JetProperty.class);
return property == null ? null : new ChangePropertyTypeToMatchOverriddenPropertyFix(property);
}
};
}
}
@@ -0,0 +1,101 @@
/*
* Copyright 2010-2013 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.jet.plugin.quickfix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetFunction;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.psi.JetTypeReference;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.plugin.JetBundle;
import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManager;
import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction;
public class ChangeReturnTypeToMatchOverriddenMethodFix extends JetIntentionAction<JetFunction> {
private JetType matchingReturnType;
public ChangeReturnTypeToMatchOverriddenMethodFix(@NotNull JetFunction function) {
super(function);
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!super.isAvailable(project, editor, file)) {
return false;
}
BindingContext context = KotlinCacheManager.getInstance(project).getDeclarationsFromProject().getBindingContext();
matchingReturnType = QuickFixUtil.findLowerBoundOfOverriddenCallablesReturnTypes(context, element);
return matchingReturnType != null;
}
@NotNull
@Override
public String getText() {
if (KotlinBuiltIns.getInstance().isUnit(matchingReturnType)) {
return JetBundle.message("remove.return.type.to.match.overridden.method");
}
else {
return JetBundle.message("change.return.type.to.match.overridden.method", matchingReturnType);
}
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("change.type.family");
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if (KotlinBuiltIns.getInstance().isUnit(matchingReturnType)) {
SpecifyTypeExplicitlyAction.removeTypeAnnotation(element);
}
else {
JetTypeReference returnTypeReference = element.getReturnTypeRef();
if (returnTypeReference == null) {
SpecifyTypeExplicitlyAction.addTypeAnnotation(project, editor, element, matchingReturnType);
}
else {
PsiElement newReturnType = JetPsiFactory.createType(project, matchingReturnType.toString());
returnTypeReference.replace(newReturnType);
}
}
}
@NotNull
public static JetIntentionActionFactory createFactory() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
JetFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetFunction.class);
return function == null ? null : new ChangeReturnTypeToMatchOverriddenMethodFix(function);
}
};
}
}
@@ -25,11 +25,13 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamedDeclaration;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.DeferredType;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import static org.jetbrains.jet.plugin.project.AnalyzeSingleFileUtil.getContextForSingleFile;
@@ -63,4 +65,27 @@ public class QuickFixUtil {
}
return type;
}
@Nullable
public static JetType findLowerBoundOfOverriddenCallablesReturnTypes(BindingContext context, JetDeclaration callable) {
DeclarationDescriptor descriptor = context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, callable);
if (!(descriptor instanceof CallableDescriptor)) {
return null;
}
JetType matchingReturnType = null;
for (CallableDescriptor overriddenDescriptor : ((CallableDescriptor) descriptor).getOverriddenDescriptors()) {
JetType overriddenReturnType = overriddenDescriptor.getReturnType();
if (overriddenReturnType == null) {
return null;
}
if (matchingReturnType == null || JetTypeChecker.INSTANCE.isSubtypeOf(overriddenReturnType, matchingReturnType)) {
matchingReturnType = overriddenReturnType;
}
else if (!JetTypeChecker.INSTANCE.isSubtypeOf(matchingReturnType, overriddenReturnType)) {
return null;
}
}
return matchingReturnType;
}
}
@@ -202,5 +202,8 @@ public class QuickFixes {
factories.put(NOT_AN_ANNOTATION_CLASS, MakeClassAnAnnotationClassFix.createFactory());
factories.put(DANGLING_FUNCTION_LITERAL_ARGUMENT_SUSPECTED, AddSemicolonAfterFunctionCallFix.createFactory());
factories.put(RETURN_TYPE_MISMATCH_ON_OVERRIDE, ChangeReturnTypeToMatchOverriddenMethodFix.createFactory());
factories.put(PROPERTY_TYPE_MISMATCH_ON_OVERRIDE, ChangePropertyTypeToMatchOverriddenPropertyFix.createFactory());
}
}
@@ -0,0 +1,8 @@
// "Change property type to 'Int'" "true"
abstract class A {
abstract var x : Int
}
abstract class B : A() {
override abstract var x : Int
}
@@ -0,0 +1,8 @@
// "Change property type to 'Int'" "true"
abstract class A {
abstract var x : Int
}
abstract class B : A() {
override abstract var x: Int
}
@@ -0,0 +1,15 @@
// "Change return type to 'T'" "true"
open class S {}
open class T : S() {}
abstract class A {
abstract fun foo() : S;
}
trait X {
fun foo() : T;
}
abstract class B : A(), X {
override abstract fun foo() : T;
}
@@ -0,0 +1,8 @@
// "Change return type to 'Int'" "true"
abstract class A {
abstract fun foo() : Int;
}
abstract class B : A() {
abstract override fun foo() : Int;
}
@@ -0,0 +1,8 @@
// "Change return type to 'Int'" "true"
abstract class A {
abstract fun foo() : Int;
}
abstract class B : A() {
abstract override fun foo(): Int;
}
@@ -0,0 +1,4 @@
// "Remove explicitly specified return type to match overridden method" "true"
abstract class A : java.util.Iterator<Int> {
public abstract override fun remove();
}
@@ -0,0 +1,8 @@
// "Change property type to 'Int'" "true"
abstract class A {
abstract var x : Int
}
abstract class B : A() {
override abstract var x : Long<caret>
}
@@ -0,0 +1,8 @@
// "Change property type to 'Int'" "true"
abstract class A {
abstract var x : Int
}
abstract class B : A() {
override abstract var x<caret>
}
@@ -0,0 +1,15 @@
// "Change return type to 'T'" "true"
open class S {}
open class T : S() {}
abstract class A {
abstract fun foo() : S;
}
trait X {
fun foo() : T;
}
abstract class B : A(), X {
override abstract fun foo() : Int<caret>;
}
@@ -0,0 +1,15 @@
// "Change return type to 'Int'" "false"
// "Change return type to 'Long'" "false"
// "Remove explicitly specified return type to match overridden method" "false"
// ERROR: <html>Return type is 'jet.String', which is not a subtype of overridden<br/><b>internal</b> <b>abstract</b> <b>fun</b> foo() : jet.Int <i>defined in</i> A</html>
abstract class A {
abstract fun foo() : Int;
}
trait X {
fun foo() : Long;
}
abstract class B : A(), X {
abstract override fun foo() : String<caret>;
}
@@ -0,0 +1,8 @@
// "Change return type to 'Int'" "true"
abstract class A {
abstract fun foo() : Int;
}
abstract class B : A() {
abstract override fun foo() : Long<caret>;
}
@@ -0,0 +1,8 @@
// "Change return type to 'Int'" "true"
abstract class A {
abstract fun foo() : Int;
}
abstract class B : A() {
abstract override fun foo<caret>();
}
@@ -0,0 +1,4 @@
// "Remove explicitly specified return type to match overridden method" "true"
abstract class A : java.util.Iterator<Int> {
public abstract override fun remove() : Int<caret>;
}
@@ -31,7 +31,7 @@ import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixMultiFileTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("idea/testData/quickfix")
@InnerTestClasses({QuickFixMultiFileTestGenerated.AddStarProjections.class, QuickFixMultiFileTestGenerated.AutoImports.class, QuickFixMultiFileTestGenerated.Modifiers.class, QuickFixMultiFileTestGenerated.Nullables.class, QuickFixMultiFileTestGenerated.TypeImports.class, QuickFixMultiFileTestGenerated.Variables.class})
@InnerTestClasses({QuickFixMultiFileTestGenerated.AddStarProjections.class, QuickFixMultiFileTestGenerated.AutoImports.class, QuickFixMultiFileTestGenerated.Modifiers.class, QuickFixMultiFileTestGenerated.Nullables.class, QuickFixMultiFileTestGenerated.Override.class, QuickFixMultiFileTestGenerated.TypeImports.class, QuickFixMultiFileTestGenerated.Variables.class})
public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTest {
public void testAllFilesPresentInQuickfix() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix"), Pattern.compile("^(\\w+)\\.before\\.Main\\.kt$"), true);
@@ -192,6 +192,20 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
}
}
@TestMetadata("idea/testData/quickfix/override")
@InnerTestClasses({})
public static class Override extends AbstractQuickFixMultiFileTest {
public void testAllFilesPresentInOverride() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/override"), Pattern.compile("^(\\w+)\\.before\\.Main\\.kt$"), true);
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("Override");
suite.addTestSuite(Override.class);
return suite;
}
}
@TestMetadata("idea/testData/quickfix/typeImports")
public static class TypeImports extends AbstractQuickFixMultiFileTest {
public void testAllFilesPresentInTypeImports() throws Exception {
@@ -226,6 +240,7 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
suite.addTestSuite(AutoImports.class);
suite.addTest(Modifiers.innerSuite());
suite.addTest(Nullables.innerSuite());
suite.addTest(Override.innerSuite());
suite.addTestSuite(TypeImports.class);
suite.addTest(Variables.innerSuite());
return suite;
@@ -695,6 +695,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
@TestMetadata("idea/testData/quickfix/override")
@InnerTestClasses({Override.TypeMismatchOnOverride.class})
public static class Override extends AbstractQuickFixTest {
public void testAllFilesPresentInOverride() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/override"), Pattern.compile("^before(\\w+)\\.kt$"), true);
@@ -730,6 +731,55 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/override/beforeVirtualMethodHidden.kt");
}
@TestMetadata("idea/testData/quickfix/override/typeMismatchOnOverride")
public static class TypeMismatchOnOverride extends AbstractQuickFixTest {
public void testAllFilesPresentInTypeMismatchOnOverride() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/override/typeMismatchOnOverride"), Pattern.compile("^before(\\w+)\\.kt$"), true);
}
@TestMetadata("beforePropertyTypeMismatchOnOverrideIntLong.kt")
public void testPropertyTypeMismatchOnOverrideIntLong() throws Exception {
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyTypeMismatchOnOverrideIntLong.kt");
}
@TestMetadata("beforePropertyTypeMismatchOnOverrideIntUnit.kt")
public void testPropertyTypeMismatchOnOverrideIntUnit() throws Exception {
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyTypeMismatchOnOverrideIntUnit.kt");
}
@TestMetadata("beforeReturnTypeMismatchOnMultipleOverride.kt")
public void testReturnTypeMismatchOnMultipleOverride() throws Exception {
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnMultipleOverride.kt");
}
@TestMetadata("beforeReturnTypeMismatchOnMultipleOverrideAmbiguity.kt")
public void testReturnTypeMismatchOnMultipleOverrideAmbiguity() throws Exception {
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnMultipleOverrideAmbiguity.kt");
}
@TestMetadata("beforeReturnTypeMismatchOnOverrideIntLong.kt")
public void testReturnTypeMismatchOnOverrideIntLong() throws Exception {
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideIntLong.kt");
}
@TestMetadata("beforeReturnTypeMismatchOnOverrideIntUnit.kt")
public void testReturnTypeMismatchOnOverrideIntUnit() throws Exception {
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideIntUnit.kt");
}
@TestMetadata("beforeReturnTypeMismatchOnOverrideUnitInt.kt")
public void testReturnTypeMismatchOnOverrideUnitInt() throws Exception {
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideUnitInt.kt");
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("Override");
suite.addTestSuite(Override.class);
suite.addTestSuite(TypeMismatchOnOverride.class);
return suite;
}
}
@TestMetadata("idea/testData/quickfix/supertypeInitialization")
@@ -1056,7 +1106,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
suite.addTestSuite(Migration.class);
suite.addTest(Modifiers.innerSuite());
suite.addTest(Nullables.innerSuite());
suite.addTestSuite(Override.class);
suite.addTest(Override.innerSuite());
suite.addTestSuite(SupertypeInitialization.class);
suite.addTestSuite(TypeAddition.class);
suite.addTestSuite(TypeImports.class);