IDEA-136406 Complete current statement doesn't insert a method body for default metho...
authorpeter <peter@jetbrains.com>
Fri, 13 Feb 2015 17:01:01 +0000 (18:01 +0100)
committerpeter <peter@jetbrains.com>
Fri, 13 Feb 2015 17:39:30 +0000 (18:39 +0100)
java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/AfterSemicolonEnterProcessor.java
java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingMethodBodyFixer.java
java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SemicolonFixer.java
java/java-tests/testData/codeInsight/completeStatement/DefaultMethodBody.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/completeStatement/DefaultMethodBody_after.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/codeInsight/CompleteStatementTest.java

index 9d43379ce92092b1f8dd36f5c1dd2a240113f3b6..35a3df7a3fccda09e5b0d4b4b0068f512d4b4dd8 100644 (file)
@@ -39,9 +39,7 @@ public class AfterSemicolonEnterProcessor implements EnterProcessor {
         psiElement instanceof PsiAssertStatement ||
         psiElement instanceof PsiField ||
         psiElement instanceof PsiImportStatementBase ||
-        psiElement instanceof PsiMethod && (((PsiMethod) psiElement).getContainingClass().isInterface() ||
-                                            ((PsiMethod) psiElement).hasModifierProperty(PsiModifier.ABSTRACT) ||
-                                            ((PsiMethod) psiElement).hasModifierProperty(PsiModifier.NATIVE))) {
+        psiElement instanceof PsiMethod && !MissingMethodBodyFixer.shouldHaveBody((PsiMethod)psiElement)) {
       int errorOffset = getErrorElementOffset(psiElement);
       int elementEndOffset = psiElement.getTextRange().getEndOffset();
       if (psiElement instanceof PsiEnumConstant) {
index f7f9459fcb6c1aa563424f9a97eed23387b10f3a..ae5c0423b7ffadce8223011880d3615a6eb7ad5d 100644 (file)
@@ -21,6 +21,8 @@ import com.intellij.psi.*;
 import com.intellij.psi.util.PsiTreeUtil;
 import com.intellij.util.IncorrectOperationException;
 
+import static com.intellij.psi.PsiModifier.*;
+
 /**
  * Created by IntelliJ IDEA.
  * User: max
@@ -34,8 +36,7 @@ public class MissingMethodBodyFixer implements Fixer {
     if (!(psiElement instanceof PsiMethod)) return;
     PsiMethod method = (PsiMethod) psiElement;
     final PsiClass containingClass = method.getContainingClass();
-    if (containingClass == null || containingClass.isInterface()
-        || method.hasModifierProperty(PsiModifier.ABSTRACT) || method.hasModifierProperty(PsiModifier.NATIVE)) return;
+    if (!shouldHaveBody(method)) return;
 
     final PsiCodeBlock body = method.getBody();
     final Document doc = editor.getDocument();
@@ -62,4 +63,12 @@ public class MissingMethodBodyFixer implements Fixer {
     }
     doc.insertString(endOffset, "{\n}");
   }
+
+  static boolean shouldHaveBody(PsiMethod method) {
+    PsiClass containingClass = method.getContainingClass();
+    if (containingClass == null) return false;
+    if (containingClass.isInterface() && !method.hasModifierProperty(DEFAULT)) return false;
+    if (method.hasModifierProperty(ABSTRACT)) return false;
+    return !method.hasModifierProperty(NATIVE);
+  }
 }
index fd78db19f78283d39190243ad3b0be861b9ba700..c43c02b2246dac9c36306fb198de9dbd30aa7bfa 100644 (file)
@@ -102,8 +102,7 @@ public class SemicolonFixer implements Fixer {
         !(psiElement instanceof PsiAssertStatement) &&
         !(psiElement instanceof PsiPackageStatement) &&
         (!(psiElement instanceof PsiField) || psiElement instanceof PsiEnumConstant) &&
-        (!(psiElement instanceof PsiMethod) || (!((PsiMethod)psiElement).getContainingClass().isInterface() &&
-                                                !((PsiMethod)psiElement).hasModifierProperty(PsiModifier.ABSTRACT)))) {
+        (!(psiElement instanceof PsiMethod) || ((PsiMethod)psiElement).getBody() != null || MissingMethodBodyFixer.shouldHaveBody((PsiMethod)psiElement))) {
       return false;
     }
     String text = psiElement.getText();
diff --git a/java/java-tests/testData/codeInsight/completeStatement/DefaultMethodBody.java b/java/java-tests/testData/codeInsight/completeStatement/DefaultMethodBody.java
new file mode 100644 (file)
index 0000000..dec337c
--- /dev/null
@@ -0,0 +1,3 @@
+public interface Foo {
+    default void foo(<caret>)
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/completeStatement/DefaultMethodBody_after.java b/java/java-tests/testData/codeInsight/completeStatement/DefaultMethodBody_after.java
new file mode 100644 (file)
index 0000000..b392981
--- /dev/null
@@ -0,0 +1,5 @@
+public interface Foo {
+    default void foo() {
+        <caret>
+    }
+}
\ No newline at end of file
index 7339d1f872107744d0f1cca2a0f27d63fa0a1927..75ec37f22f24d1179c33a5a4634903a04718b277 100644 (file)
@@ -269,7 +269,9 @@ public class CompleteStatementTest extends EditorActionTestCase {
   public void testCompleteMethodCallAtReturn() throws Exception { doTest(); }
   
   public void testGenericMethodBody() throws Exception { doTest(); }
-  
+
+  public void testDefaultMethodBody() { doTest(); }
+
   public void testArrayInitializerRBracket() throws Exception { doTest(); }
   
   public void testReturnInLambda() { doTest(); }