diff --git a/java/java-impl/src/META-INF/JavaPlugin.xml b/java/java-impl/src/META-INF/JavaPlugin.xml
index 489ab79adb1e..9374281c9094 100644
--- a/java/java-impl/src/META-INF/JavaPlugin.xml
+++ b/java/java-impl/src/META-INF/JavaPlugin.xml
@@ -1097,6 +1097,7 @@
+
diff --git a/java/java-impl/src/com/intellij/codeInsight/template/JavaCodeContextType.java b/java/java-impl/src/com/intellij/codeInsight/template/JavaCodeContextType.java
index 904a7e37f99e..76d78b93cf67 100644
--- a/java/java-impl/src/com/intellij/codeInsight/template/JavaCodeContextType.java
+++ b/java/java-impl/src/com/intellij/codeInsight/template/JavaCodeContextType.java
@@ -142,6 +142,28 @@ public abstract class JavaCodeContextType extends TemplateContextType {
return statement != null && statement.getTextRange().getStartOffset() == element.getTextRange().getStartOffset();
}
}
+
+ public static class ElsePlace extends JavaCodeContextType {
+ public ElsePlace() {
+ super("JAVA_ELSE_PLACE", JavaBundle.message("live.template.context.else"), Generic.class);
+ }
+
+ @Override
+ protected boolean isInContext(@NotNull PsiElement element) {
+ if (isAfterExpression(element) || JavaStringContextType.isStringLiteral(element)) return false;
+ PsiExpressionStatement parent =
+ PsiTreeUtil.getParentOfType(element, PsiExpressionStatement.class, true, PsiCodeBlock.class, PsiLambdaExpression.class);
+ if (parent == null) return false;
+ PsiIfStatement previous = ObjectUtils.tryCast(PsiTreeUtil.skipWhitespacesAndCommentsBackward(parent), PsiIfStatement.class);
+ if (previous == null) return false;
+ PsiStatement elseBranch = previous.getElseBranch();
+ while (elseBranch instanceof PsiIfStatement) {
+ elseBranch = ((PsiIfStatement)elseBranch).getElseBranch();
+ }
+ return elseBranch == null;
+ }
+ }
+
public static class Expression extends JavaCodeContextType {
public Expression() {
super("JAVA_EXPRESSION", JavaBundle.message("live.template.context.expression"), Generic.class);
diff --git a/java/java-tests/testData/codeInsight/template/ElseIf-out.java b/java/java-tests/testData/codeInsight/template/ElseIf-out.java
new file mode 100644
index 000000000000..255ae3e80f03
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/template/ElseIf-out.java
@@ -0,0 +1,8 @@
+import java.util.*;
+
+class Foo {
+ void test(int x){
+ if(x > 0) {} else if () {
+ }
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/template/ElseIf.java b/java/java-tests/testData/codeInsight/template/ElseIf.java
new file mode 100644
index 000000000000..0326052eba3b
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/template/ElseIf.java
@@ -0,0 +1,8 @@
+import java.util.*;
+
+class Foo {
+ void test(int x){
+ if(x > 0) {}
+
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/template/ElseIf2-out.java b/java/java-tests/testData/codeInsight/template/ElseIf2-out.java
new file mode 100644
index 000000000000..9113f164ffce
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/template/ElseIf2-out.java
@@ -0,0 +1,8 @@
+import java.util.*;
+
+class Foo {
+ void test(int x){
+ if(x > 0) {} else if () {
+ } else {}
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/template/ElseIf2.java b/java/java-tests/testData/codeInsight/template/ElseIf2.java
new file mode 100644
index 000000000000..fdedf204c14f
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/template/ElseIf2.java
@@ -0,0 +1,8 @@
+import java.util.*;
+
+class Foo {
+ void test(int x){
+ if(x > 0) {}
+ else {}
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/template/ElseIf3-out.java b/java/java-tests/testData/codeInsight/template/ElseIf3-out.java
new file mode 100644
index 000000000000..37b0dc0ffb38
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/template/ElseIf3-out.java
@@ -0,0 +1,9 @@
+import java.util.*;
+
+class Foo {
+ void test(int x){
+ if(x > 0) {}
+ else if(x == 0) {} else if () {
+ } else {}
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/template/ElseIf3.java b/java/java-tests/testData/codeInsight/template/ElseIf3.java
new file mode 100644
index 000000000000..3977c6960871
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/template/ElseIf3.java
@@ -0,0 +1,9 @@
+import java.util.*;
+
+class Foo {
+ void test(int x){
+ if(x > 0) {}
+ else if(x == 0) {}
+ else {}
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/JavaLiveTemplateTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/JavaLiveTemplateTest.groovy
index 1a73311b17ae..0dd31714b76d 100644
--- a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/JavaLiveTemplateTest.groovy
+++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/JavaLiveTemplateTest.groovy
@@ -158,6 +158,24 @@ class Outer {
checkResult()
}
+ void testElseIf() throws Throwable {
+ configure()
+ startTemplate("else-if", "Java")
+ checkResult()
+ }
+
+ void testElseIf2() throws Throwable {
+ configure()
+ startTemplate("else-if", "Java")
+ checkResult()
+ }
+
+ void testElseIf3() throws Throwable {
+ configure()
+ startTemplate("else-if", "Java")
+ checkResult()
+ }
+
void testIter() throws Throwable {
configure()
startTemplate("iter", "Java")
diff --git a/java/openapi/resources/messages/JavaBundle.properties b/java/openapi/resources/messages/JavaBundle.properties
index 9c56cbfc5fce..463c8f9c559a 100644
--- a/java/openapi/resources/messages/JavaBundle.properties
+++ b/java/openapi/resources/messages/JavaBundle.properties
@@ -1281,6 +1281,7 @@ highlight.suppressed.warnings.choose.inspections=Choose Inspections to Highlight
inspection.magic.constants.should.be.one.of.values=Should be one of: {0}{1,choice,0#|1# or their combination}
live.template.context.consumer.function=Consumer function
live.template.context.statement=Statement
+live.template.context.else='else' position
live.template.context.expression=Expression
live.template.context.declaration=Declaration
inspection.unused.display.name=Unused declaration
diff --git a/resources/src/liveTemplates/Java.xml b/resources/src/liveTemplates/Java.xml
index c551d8d658c3..376fa3681bc7 100644
--- a/resources/src/liveTemplates/Java.xml
+++ b/resources/src/liveTemplates/Java.xml
@@ -7,6 +7,11 @@
+
+
+
+
+