Postfix completion: apply throw template on Throwable only

This commit is contained in:
Alexander Zolotov
2014-02-20 16:07:52 +04:00
parent 79e95dc617
commit e896024c69
6 changed files with 26 additions and 5 deletions
@@ -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
@@ -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;
@@ -0,0 +1,5 @@
public class Foo {
void m(Object o) {
o.throw<caret>
}
}
@@ -0,0 +1,5 @@
public class Foo {
void m(Object o) {
throw o;<caret>
}
}
@@ -1,5 +1,7 @@
import java.lang.RuntimeException;
public class Foo {
void m(Object o) {
o.throw<caret>
void m() {
new RuntimeException("error").throw<caret>
}
}
@@ -1,5 +1,7 @@
import java.lang.RuntimeException;
public class Foo {
void m(Object o) {
throw o;<caret>
void m() {
throw new RuntimeException("error");
}
}