From df4439ba6c4bf5d5a39562b644f052439a01ea63 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 14 Oct 2021 18:10:34 +0200 Subject: [PATCH] [java] add method body: add `default` modifier if method in interface must have body (IDEA-279878) GitOrigin-RevId: f856b41110e09d4e6d10b4841ba455cb7b8b69c7 --- .../daemon/impl/quickfix/AddMethodBodyFix.java | 10 +++++++++- .../addMethodBody/afterSuperAndInterface.java | 12 ++++++++++++ .../addMethodBody/beforeSuperAndInterface.java | 10 ++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addMethodBody/afterSuperAndInterface.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addMethodBody/beforeSuperAndInterface.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMethodBodyFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMethodBodyFix.java index 7d8f8876772d..c75dc52f27be 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMethodBodyFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMethodBodyFix.java @@ -1,4 +1,4 @@ -// 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. +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.daemon.impl.quickfix; import com.intellij.codeInsight.daemon.QuickFixBundle; @@ -16,6 +16,8 @@ import com.intellij.psi.util.PsiUtil; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; +import java.util.Objects; + public final class AddMethodBodyFix implements IntentionActionWithFixAllOption { private final PsiMethod myMethod; private final @Nls String myText; @@ -59,6 +61,12 @@ public final class AddMethodBodyFix implements IntentionActionWithFixAllOption { @Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) { PsiUtil.setModifierProperty(myMethod, PsiModifier.ABSTRACT, false); + if (Objects.requireNonNull(myMethod.getContainingClass()).isInterface() && + !myMethod.hasModifierProperty(PsiModifier.STATIC) && + !myMethod.hasModifierProperty(PsiModifier.DEFAULT) && + !myMethod.hasModifierProperty(PsiModifier.PRIVATE)) { + PsiUtil.setModifierProperty(myMethod, PsiModifier.DEFAULT, true); + } CreateFromUsageUtils.setupMethodBody(myMethod); CreateFromUsageUtils.setupEditor(myMethod, editor); } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addMethodBody/afterSuperAndInterface.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addMethodBody/afterSuperAndInterface.java new file mode 100644 index 000000000000..0b1a81d69302 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addMethodBody/afterSuperAndInterface.java @@ -0,0 +1,12 @@ +// "Make 'a.f' not abstract" "true" +interface a { + default String f() { + return null; + } +} +class b implements a { + void z() { + a.super.f(); + } +} + diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addMethodBody/beforeSuperAndInterface.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addMethodBody/beforeSuperAndInterface.java new file mode 100644 index 000000000000..1f4edb03bd94 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addMethodBody/beforeSuperAndInterface.java @@ -0,0 +1,10 @@ +// "Make 'a.f' not abstract" "true" +interface a { + String f(); +} +class b implements a { + void z() { + a.super.f(); + } +} +