Skip to content

Quickfix "Add 'open' modifier to supertype" for FINAL_SYPERTYPE error. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions idea/src/org/jetbrains/jet/plugin/quickfix/FinalSupertypeFix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lexer.JetKeywordToken;
import org.jetbrains.jet.lexer.JetToken;
import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManager;

import static org.jetbrains.jet.lexer.JetTokens.FINAL_KEYWORD;
import static org.jetbrains.jet.lexer.JetTokens.OPEN_KEYWORD;

public class FinalSupertypeFix extends JetIntentionAction<JetClass> {
private final JetClass childClass;

public FinalSupertypeFix(@NotNull JetClass childClass) {
super(childClass);
this.childClass = childClass;
}

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return super.isAvailable(project, editor, file) && findSuperClass(childClass, getBindingContext(project)) != null;
}

@NotNull
@Override
public String getText() {
return "Add 'open' modifier to superclass";
}

@NotNull
@Override
public String getFamilyName() {
return "Add modifier";
}

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
JetToken[] modifiersThanCanBeReplaced = new JetKeywordToken[] { FINAL_KEYWORD };
JetClass superClass = findSuperClass(childClass, getBindingContext(project));
if (superClass != null) {
superClass.replace(AddModifierFix.addModifier(superClass, OPEN_KEYWORD, modifiersThanCanBeReplaced, project, false));
}
}

@NotNull
private static BindingContext getBindingContext(Project project) {
return KotlinCacheManager.getInstance(project).getDeclarationsFromProject(project).getBindingContext();
}

@Nullable
private static JetClass findSuperClass(JetClass childClass, BindingContext context) {
ClassDescriptor childClassDescriptor = context.get(BindingContext.CLASS, childClass);
if (childClassDescriptor == null) {
return null;
}
for (JetType supertype: childClassDescriptor.getTypeConstructor().getSupertypes()) {
ClassDescriptor superClassDescriptor = (ClassDescriptor) supertype.getConstructor().getDeclarationDescriptor();
if (superClassDescriptor == null) {
continue;
}
JetClass superClass = (JetClass) BindingContextUtils.descriptorToDeclaration(context, superClassDescriptor);
if (superClass != null) {
return superClass;
}
}
return null;
}

@Nullable
public static JetIntentionActionFactory createFactory() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
JetClass childClass = QuickFixUtil.getParentElementOfType(diagnostic, JetClass.class);
return childClass == null ? null : new FinalSupertypeFix(childClass);
}
};
}
}
2 changes: 2 additions & 0 deletions idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,7 @@ private QuickFixes() {}

factories.put(NO_TYPE_ARGUMENTS_ON_RHS_OF_IS_EXPRESSION, AddStarProjectionsFix.createFactoryForIsExpression());
factories.put(WRONG_NUMBER_OF_TYPE_ARGUMENTS, AddStarProjectionsFix.createFactoryForJavaClass());

factories.put(FINAL_SUPERTYPE, FinalSupertypeFix.createFactory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// "Add 'open' modifier to superclass" "true"
open class A {}
class B : A<caret>() {}
3 changes: 3 additions & 0 deletions idea/testData/quickfix/modifiers/afterFinalSupertype.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// "Add 'open' modifier to superclass" "true"
open class A {}
class B : A<caret>() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// "Add 'open' modifier to superclass" "true"
final class A {}
class B : A<caret>() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// "Add 'open' modifier to superclass" "false"
// ERROR: This type is final, so it cannot be inherited from
class A : String<caret>() {}
3 changes: 3 additions & 0 deletions idea/testData/quickfix/modifiers/beforeFinalSupertype.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// "Add 'open' modifier to superclass" "true"
class A {}
class B : A<caret>() {}