[java-highlighting] IDEA-216274 Provide fix for the "Not allowed in interface" error

GitOrigin-RevId: 1290ab27997b03c6a75b1c8172a3477722144941
This commit is contained in:
Andrey.Cherkasov
2021-03-09 23:59:53 +03:00
committed by intellij-monorepo-bot
parent ff6535cf3e
commit c19534589b
19 changed files with 156 additions and 2 deletions

View File

@@ -512,4 +512,6 @@ public abstract class QuickFixFactory {
public abstract @NotNull IntentionAction createReceiverParameterTypeFix(@NotNull PsiReceiverParameter receiverParameter,
@NotNull PsiType enclosingClassType);
public abstract @NotNull IntentionAction createConvertInterfaceContainingNotAllowedToClassFix(@NotNull PsiClass aClass);
}

View File

@@ -752,7 +752,10 @@ public final class HighlightClassUtil {
static HighlightInfo checkThingNotAllowedInInterface(@NotNull PsiElement element, @Nullable PsiClass aClass) {
if (aClass == null || !aClass.isInterface()) return null;
String description = JavaErrorBundle.message("not.allowed.in.interface");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createDeleteFix(element));
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createConvertInterfaceContainingNotAllowedToClassFix(aClass));
return info;
}
static HighlightInfo checkQualifiedNew(@NotNull PsiNewExpression expression, @Nullable PsiType type, @Nullable PsiClass aClass) {

View File

@@ -46,6 +46,7 @@ import com.intellij.util.DocumentUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.fixes.CreateDefaultBranchFix;
import com.siyeh.ig.fixes.CreateMissingSwitchBranchesFix;
import com.siyeh.ipp.interfacetoclass.ConvertInterfaceContainingNotAllowedToClassFix;
import com.siyeh.ipp.modifiers.ChangeModifierIntention;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -999,4 +1000,8 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
}
};
}
public @NotNull IntentionAction createConvertInterfaceContainingNotAllowedToClassFix(@NotNull PsiClass aClass) {
return new ConvertInterfaceContainingNotAllowedToClassFix(aClass);
}
}

View File

@@ -0,0 +1,5 @@
// "Convert to 'class'" "true"
abstract class A {
public A();
}

View File

@@ -0,0 +1,5 @@
// "Convert to 'class'" "true"
abstract class A {
{};
}

View File

@@ -0,0 +1,5 @@
// "Convert to 'class'" "true"
abstract class A {
static {};
}

View File

@@ -0,0 +1,5 @@
// "Convert to 'class'" "true"
interface A {
A()<caret>;
}

View File

@@ -0,0 +1,5 @@
// "Convert to 'class'" "true"
interface A {
{}<caret>;
}

View File

@@ -0,0 +1,5 @@
// "Convert to 'class'" "true"
interface A {
static {}<caret>;
}

View File

@@ -0,0 +1,4 @@
// "Remove constructor" "true"
interface A {
}

View File

@@ -0,0 +1,4 @@
// "Remove initializer" "true"
interface A {
}

View File

@@ -0,0 +1,4 @@
// "Remove initializer" "true"
interface A {
}

View File

@@ -0,0 +1,5 @@
// "Remove constructor" "true"
interface A {
A()<caret>;
}

View File

@@ -0,0 +1,5 @@
// "Remove initializer" "true"
interface A {
{}<caret>
}

View File

@@ -0,0 +1,5 @@
// "Remove initializer" "true"
interface A {
static {}<caret>
}

View File

@@ -0,0 +1,12 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
public class ConvertInterfaceContainingNotAllowedToClassFixTest extends LightQuickFixParameterizedTestCase {
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/convertInterfaceContainingNotAllowedToClass";
}
}

View File

@@ -0,0 +1,12 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
public class RemoveNotAllowedInInterfaceFixTest extends LightQuickFixParameterizedTestCase {
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/removeNotAllowedInInterface";
}
}

View File

@@ -0,0 +1,63 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ipp.interfacetoclass;
import com.intellij.codeInsight.intention.PriorityAction;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.codeInspection.util.IntentionFamilyName;
import com.intellij.codeInspection.util.IntentionName;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ConvertInterfaceContainingNotAllowedToClassFix extends LocalQuickFixAndIntentionActionOnPsiElement implements PriorityAction {
public ConvertInterfaceContainingNotAllowedToClassFix(@Nullable PsiClass aClass) {
super(aClass);
}
@Override
public boolean isAvailable(@NotNull Project project,
@NotNull PsiFile file,
@Nullable Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
final PsiElement nameIdentifier = getNameIdentifier(startElement);
if (nameIdentifier == null) return false;
return new ConvertInterfaceToClassIntention().getElementPredicate().satisfiedBy(nameIdentifier);
}
@Override
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@Nullable Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
final PsiElement nameIdentifier = getNameIdentifier(startElement);
if (nameIdentifier == null) return;
new ConvertInterfaceToClassIntention().processIntention(nameIdentifier);
}
@Override
public @NotNull Priority getPriority() {
return Priority.LOW;
}
private static PsiIdentifier getNameIdentifier(@NotNull PsiElement element) {
final PsiClass aClass = ObjectUtils.tryCast(element, PsiClass.class);
if (aClass == null) return null;
return aClass.getNameIdentifier();
}
@Override
public @IntentionName @NotNull String getText() {
return new ConvertInterfaceToClassIntention().getText();
}
@Override
public @IntentionFamilyName @NotNull String getFamilyName() {
return new ConvertInterfaceToClassIntention().getFamilyName();
}
}

View File

@@ -79,7 +79,7 @@ public class ConvertInterfaceToClassIntention extends Intention {
if (method.hasModifierProperty(PsiModifier.DEFAULT)) {
PsiUtil.setModifierProperty(method, PsiModifier.DEFAULT, false);
}
else if (!method.hasModifierProperty(PsiModifier.STATIC)) {
else if (!method.hasModifierProperty(PsiModifier.STATIC) && !method.isConstructor()) {
PsiUtil.setModifierProperty(method, PsiModifier.ABSTRACT, true);
}
}