QuickFix for OVERRIDING_FINAL_MEMBER

This commit is contained in:
Wojciech Lopata
2013-02-26 17:15:26 +01:00
committed by Evgeny Gerashchenko
parent 08ce27e97e
commit cee45f6a0a
12 changed files with 239 additions and 0 deletions
@@ -12,6 +12,7 @@ remove.function.body=Remove function body
make.element.modifier=Make {0} {1}
add.modifier=Add ''{0}'' modifier
add.modifier.family=Add Modifier
make.element.in.classifiers.open=Make ''{0}'' in {1} open
make.class.annotation.class=Make ''{0}'' an annotation class
make.class.annotation.class.family=Make Class an Annotation Class
add.star.projections=Add ''{0}''
@@ -0,0 +1,122 @@
/*
* 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.descriptors.CallableMemberDescriptor;
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.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
import org.jetbrains.jet.plugin.JetBundle;
import org.jetbrains.jet.plugin.project.WholeProjectAnalyzerFacade;
import java.util.*;
import static org.jetbrains.jet.lexer.JetTokens.OPEN_KEYWORD;
public class MakeOverriddenMemberOpenFix extends JetIntentionAction<JetDeclaration> {
private final List<PsiElement> overriddenMembers = new ArrayList<PsiElement>();
private final List<String> containingDeclarationsNames = new ArrayList<String>();
public MakeOverriddenMemberOpenFix(@NotNull JetDeclaration declaration) {
super(declaration);
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!super.isAvailable(project, editor, file) || !(file instanceof JetFile)) {
return false;
}
// When running single test 'isAvailable()' is invoked multiple times, so we need to clear lists.
overriddenMembers.clear();
containingDeclarationsNames.clear();
ResolveSession resolveSession = WholeProjectAnalyzerFacade.getLazyResolveSessionForFile((JetFile) file);
DeclarationDescriptor descriptor = resolveSession.resolveToDescriptor(element);
if (!(descriptor instanceof CallableMemberDescriptor)) return false;
CallableMemberDescriptor callableMemberDescriptor = (CallableMemberDescriptor) descriptor;
for (CallableMemberDescriptor overriddenDescriptor : callableMemberDescriptor.getOverriddenDescriptors()) {
if (!overriddenDescriptor.getModality().isOverridable()) {
PsiElement overriddenMember =
BindingContextUtils.descriptorToDeclaration(resolveSession.getBindingContext(), overriddenDescriptor);
if (overriddenMember == null || !overriddenMember.isWritable()) {
return false;
}
String containingDeclarationName = overriddenDescriptor.getContainingDeclaration().getName().getName();
overriddenMembers.add(overriddenMember);
containingDeclarationsNames.add(containingDeclarationName);
}
}
return overriddenMembers.size() > 0;
}
@NotNull
@Override
public String getText() {
if (overriddenMembers.size() == 1) {
return JetBundle.message("make.element.modifier", containingDeclarationsNames.get(0) + "." + element.getName(), OPEN_KEYWORD);
}
StringBuilder declarations = new StringBuilder();
Collections.sort(containingDeclarationsNames);
for (int i = 0; i < containingDeclarationsNames.size() - 2; i++) {
declarations.append(containingDeclarationsNames.get(i));
declarations.append(", ");
}
declarations.append(containingDeclarationsNames.get(containingDeclarationsNames.size() - 2));
declarations.append(" and ");
declarations.append(containingDeclarationsNames.get(containingDeclarationsNames.size() - 1));
return JetBundle.message("make.element.in.classifiers.open", element.getName(), declarations.toString());
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("add.modifier.family");
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
for (PsiElement overriddenMember : overriddenMembers) {
overriddenMember.replace(AddModifierFix.addModifierWithDefaultReplacement(overriddenMember, OPEN_KEYWORD, project, false));
}
}
@NotNull
public static JetIntentionActionFactory createFactory() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
JetDeclaration declaration = QuickFixUtil.getParentElementOfType(diagnostic, JetDeclaration.class);
assert declaration != null;
return new MakeOverriddenMemberOpenFix(declaration);
}
};
}
}
@@ -194,6 +194,8 @@ public class QuickFixes {
factories.put(FINAL_SUPERTYPE, addOpenModifierToClassDeclarationFix);
factories.put(FINAL_UPPER_BOUND, addOpenModifierToClassDeclarationFix);
factories.put(OVERRIDING_FINAL_MEMBER, MakeOverriddenMemberOpenFix.createFactory());
factories.put(PARAMETER_NAME_CHANGED_ON_OVERRIDE, RenameParameterToMatchOverriddenMethodFix.createFactory());
factories.put(OPEN_MODIFIER_IN_ENUM, RemoveModifierFix.createRemoveModifierFromListOwnerFactory(OPEN_KEYWORD));
@@ -0,0 +1,20 @@
// "Make 'foo' in A, X and Y open" "true"
open class A {
open fun foo() {}
}
trait X {
open fun foo() {}
}
trait Y {
open fun foo() {}
}
trait Z {
fun foo() {}
}
class B : A(), X, Y, Z {
override<caret> fun foo() {}
}
@@ -0,0 +1,8 @@
// "Make A.foo open" "true"
open class A {
open fun foo() {}
}
class B : A() {
override<caret> fun foo() {}
}
@@ -0,0 +1,8 @@
// "Make A.x open" "true"
open class A {
open var x = 42;
}
class B : A() {
override<caret> var x = 24;
}
@@ -0,0 +1,20 @@
// "Make 'foo' in A, X and Y open" "true"
open class A {
fun foo() {}
}
trait X {
final fun foo() {}
}
trait Y {
final fun foo() {}
}
trait Z {
fun foo() {}
}
class B : A(), X, Y, Z {
override<caret> fun foo() {}
}
@@ -0,0 +1,5 @@
// "Make Object.notify open" "false"
// ERROR: 'notify' in 'Object' is final and cannot be overridden
class A : Object() {
override<caret> fun notify() {}
}
@@ -0,0 +1,8 @@
// "Make A.foo open" "true"
open class A {
fun foo() {}
}
class B : A() {
override<caret> fun foo() {}
}
@@ -0,0 +1,8 @@
// "Make A.x open" "true"
open class A {
final var x = 42;
}
class B : A() {
override<caret> var x = 24;
}
@@ -0,0 +1,12 @@
// "Make 'ordinal' in E and X open" "false"
// ERROR: This type is final, so it cannot be inherited from
// ERROR: Cannot access '<init>': it is 'private' in 'E'
// ERROR: 'ordinal' in 'E' is final and cannot be overridden
enum class E {}
trait X {
final fun ordinal() : Int = 42
}
class A : E(), X {
override<caret> fun ordinal() : Int = 24;
}
@@ -711,6 +711,31 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/override/beforeNothingToOverride.kt");
}
@TestMetadata("beforeOverriddingMultipleFinalMethods.kt")
public void testOverriddingMultipleFinalMethods() throws Exception {
doTest("idea/testData/quickfix/override/beforeOverriddingMultipleFinalMethods.kt");
}
@TestMetadata("beforeOverridingFinalJavaMethod.kt")
public void testOverridingFinalJavaMethod() throws Exception {
doTest("idea/testData/quickfix/override/beforeOverridingFinalJavaMethod.kt");
}
@TestMetadata("beforeOverridingFinalMethod.kt")
public void testOverridingFinalMethod() throws Exception {
doTest("idea/testData/quickfix/override/beforeOverridingFinalMethod.kt");
}
@TestMetadata("beforeOverridingFinalProperty.kt")
public void testOverridingFinalProperty() throws Exception {
doTest("idea/testData/quickfix/override/beforeOverridingFinalProperty.kt");
}
@TestMetadata("beforeOverridingWritableAndNotWritableFinalMembers.kt")
public void testOverridingWritableAndNotWritableFinalMembers() throws Exception {
doTest("idea/testData/quickfix/override/beforeOverridingWritableAndNotWritableFinalMembers.kt");
}
@TestMetadata("beforeParameterNameChangedAmbiguousRename.kt")
public void testParameterNameChangedAmbiguousRename() throws Exception {
doTest("idea/testData/quickfix/override/beforeParameterNameChangedAmbiguousRename.kt");