From e896024c692575b788c50a89c4312ba4b1eac1cd Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Thu, 20 Feb 2014 16:04:49 +0400 Subject: [PATCH] Postfix completion: apply throw template on Throwable only --- .../postfix/templates/ThrowExceptionPostfixTemplate.java | 4 +++- .../template/postfix/util/PostfixTemplatesUtils.java | 5 +++++ .../template/postfix/templates/throw/notThrowable.java | 5 +++++ .../postfix/templates/throw/notThrowable_after.java | 5 +++++ .../template/postfix/templates/throw/simple.java | 6 ++++-- .../template/postfix/templates/throw/simple_after.java | 6 ++++-- 6 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/throw/notThrowable.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/throw/notThrowable_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java index 7c2017ffe290..b1bb654d6dff 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java @@ -19,6 +19,7 @@ import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiExpression; import org.jetbrains.annotations.NotNull; public class ThrowExceptionPostfixTemplate extends PostfixTemplate { @@ -28,7 +29,8 @@ public class ThrowExceptionPostfixTemplate extends PostfixTemplate { @Override public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { - return getTopmostExpression(context) != null; + PsiExpression expression = getTopmostExpression(context); + return expression != null && PostfixTemplatesUtils.isThrowable(expression.getType()); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java index 92cb7fcc064e..09cf4fb19112 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java @@ -56,6 +56,11 @@ public abstract class PostfixTemplatesUtils { return type != null && InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ITERABLE); } + @Contract("null -> false") + public static boolean isThrowable(@Nullable PsiType type) { + return type != null && InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_THROWABLE); + } + @Contract("null -> false") public static boolean isArray(@Nullable PsiType type) { return type != null && type instanceof PsiArrayType; diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/throw/notThrowable.java b/java/java-tests/testData/codeInsight/template/postfix/templates/throw/notThrowable.java new file mode 100644 index 000000000000..edfbaabf3674 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/throw/notThrowable.java @@ -0,0 +1,5 @@ +public class Foo { + void m(Object o) { + o.throw + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/throw/notThrowable_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/throw/notThrowable_after.java new file mode 100644 index 000000000000..900e720607eb --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/throw/notThrowable_after.java @@ -0,0 +1,5 @@ +public class Foo { + void m(Object o) { + throw o; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/throw/simple.java b/java/java-tests/testData/codeInsight/template/postfix/templates/throw/simple.java index edfbaabf3674..2d183ba79714 100644 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/throw/simple.java +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/throw/simple.java @@ -1,5 +1,7 @@ +import java.lang.RuntimeException; + public class Foo { - void m(Object o) { - o.throw + void m() { + new RuntimeException("error").throw } } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/throw/simple_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/throw/simple_after.java index 900e720607eb..a0219e42ffc7 100644 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/throw/simple_after.java +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/throw/simple_after.java @@ -1,5 +1,7 @@ +import java.lang.RuntimeException; + public class Foo { - void m(Object o) { - throw o; + void m() { + throw new RuntimeException("error"); } } \ No newline at end of file