diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/OptionalPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/OptionalPostfixTemplate.java index 0d0e26601840..79c6e3558866 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/OptionalPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/OptionalPostfixTemplate.java @@ -18,6 +18,7 @@ package com.intellij.codeInsight.template.postfix.templates; import com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiExpression; +import com.intellij.psi.PsiPrimitiveType; import com.intellij.psi.PsiType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -33,18 +34,24 @@ public class OptionalPostfixTemplate extends StringBasedPostfixTemplate { @Nullable @Override public String getTemplateString(@NotNull PsiElement element) { - String defaultTemplate = "Optional.ofNullable($expr$)"; - if (!(element instanceof PsiExpression)) return defaultTemplate; - - String method = ".of($expr$)"; - PsiType type = ((PsiExpression)element).getType(); - if (PsiType.INT.equals(type)) { - return "OptionalInt" + method; - } else if (PsiType.DOUBLE.equals(type)) { - return "OptionalDouble" + method; - } else if (PsiType.LONG.equals(type)) { - return "OptionalLong" + method; + String className = "Optional"; + String methodName = "ofNullable"; + + if (element instanceof PsiExpression) { + PsiType type = ((PsiExpression)element).getType(); + if (type instanceof PsiPrimitiveType) { + if (PsiType.INT.equals(type)) { + className = "OptionalInt"; + } + else if (PsiType.DOUBLE.equals(type)) { + className = "OptionalDouble"; + } + else if (PsiType.LONG.equals(type)) { + className = "OptionalLong"; + } + methodName = "of"; + } } - return defaultTemplate; + return "java.util." + className + "." + methodName + "($expr$)"; } } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplate.java index b31da8f53c10..c1603123d715 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplate.java @@ -23,7 +23,6 @@ import org.jetbrains.annotations.Nullable; import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.selectorTopmost; public class StreamPostfixTemplate extends StringBasedPostfixTemplate { - public StreamPostfixTemplate() { super("stream", "Arrays.stream(expr)", JavaPostfixTemplatesUtils.atLeastJava8Selector(selectorTopmost(JavaPostfixTemplatesUtils.IS_ARRAY))); } @@ -31,7 +30,7 @@ public class StreamPostfixTemplate extends StringBasedPostfixTemplate { @Nullable @Override public String getTemplateString(@NotNull PsiElement element) { - return "Arrays.stream($expr$)$END$"; + return "java.util.Arrays.stream($expr$)"; } @Override diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/doNotExpandOnJavaLess8.java b/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/doNotExpandOnJavaLess8.java new file mode 100644 index 000000000000..d43e90f46aeb --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/doNotExpandOnJavaLess8.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + array.lambda + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/doNotExpandOnJavaLess8_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/doNotExpandOnJavaLess8_after.java new file mode 100644 index 000000000000..16fd9f199feb --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/doNotExpandOnJavaLess8_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + array.lambda + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/simple.java b/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/simple.java new file mode 100644 index 000000000000..d43e90f46aeb --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/simple.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + array.lambda + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/simple_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/simple_after.java new file mode 100644 index 000000000000..e048bd31c7a5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/lambda/simple_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + () -> array + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/array.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/array.java new file mode 100644 index 000000000000..965d4d196d5f --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/array.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + array.opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/array_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/array_after.java new file mode 100644 index 000000000000..a783de31c4c3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/array_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + java.util.Optional.ofNullable(array) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/boxedType.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/boxedType.java new file mode 100644 index 000000000000..9d4b508e27b2 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/boxedType.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(Byte foo) { + foo.opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/boxedType_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/boxedType_after.java new file mode 100644 index 000000000000..ff25bb008185 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/boxedType_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(Byte foo) { + java.util.Optional.ofNullable(foo) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/doNotExpandOnJavaLess8.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/doNotExpandOnJavaLess8.java new file mode 100644 index 000000000000..965d4d196d5f --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/doNotExpandOnJavaLess8.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + array.opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/doNotExpandOnJavaLess8_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/doNotExpandOnJavaLess8_after.java new file mode 100644 index 000000000000..14b0408d88eb --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/doNotExpandOnJavaLess8_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + array.opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/double.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/double.java new file mode 100644 index 000000000000..c08eb84d3d3a --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/double.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(double foo) { + foo.opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/double_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/double_after.java new file mode 100644 index 000000000000..17cec2e59ece --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/double_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(double foo) { + java.util.OptionalDouble.of(foo) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/expression.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/expression.java new file mode 100644 index 000000000000..0928e33ed907 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/expression.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(Foo foo) { + foo.opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/expression_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/expression_after.java new file mode 100644 index 000000000000..f009ae61365a --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/expression_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(Foo foo) { + java.util.Optional.ofNullable(foo) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/int.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/int.java new file mode 100644 index 000000000000..cbc49f1badb6 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/int.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int foo) { + foo.opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/intLiteral.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/intLiteral.java new file mode 100644 index 000000000000..886acb44fdca --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/intLiteral.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m() { + 42.opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/intLiteral_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/intLiteral_after.java new file mode 100644 index 000000000000..36b67844c81e --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/intLiteral_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m() { + java.util.OptionalInt.of(42) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/int_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/int_after.java new file mode 100644 index 000000000000..b1b2c008a711 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/int_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int foo) { + java.util.OptionalInt.of(foo) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/long.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/long.java new file mode 100644 index 000000000000..92a2a6279f8f --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/long.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(long foo) { + foo.opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/long_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/long_after.java new file mode 100644 index 000000000000..bff6edcd44ed --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/long_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(long foo) { + java.util.OptionalLong.of(foo) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/primitiveType.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/primitiveType.java new file mode 100644 index 000000000000..51dc236fbd3c --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/primitiveType.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(byte foo) { + foo.opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/primitiveType_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/primitiveType_after.java new file mode 100644 index 000000000000..33ea33d8dd78 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/primitiveType_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(byte foo) { + java.util.Optional.of(foo) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/stream/doNotExpandOnJavaLess8.java b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/doNotExpandOnJavaLess8.java new file mode 100644 index 000000000000..b5e2724f21ac --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/doNotExpandOnJavaLess8.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + array.stream + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/stream/doNotExpandOnJavaLess8_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/doNotExpandOnJavaLess8_after.java new file mode 100644 index 000000000000..7181c4a64693 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/doNotExpandOnJavaLess8_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + array.stream + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/stream/simple.java b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/simple.java new file mode 100644 index 000000000000..b5e2724f21ac --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/simple.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(int[] array) { + array.stream + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/stream/simple_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/simple_after.java new file mode 100644 index 000000000000..d0334a8fecc3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/simple_after.java @@ -0,0 +1,9 @@ +package templates; + +import java.util.Arrays; + +public class Foo { + void m(int[] array) { + Arrays.stream(array) + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/LambdaPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/LambdaPostfixTemplateTest.java new file mode 100644 index 000000000000..bb0ec13de7b7 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/LambdaPostfixTemplateTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.postfix.templates; + +import com.intellij.openapi.roots.LanguageLevelProjectExtension; +import com.intellij.pom.java.LanguageLevel; +import org.jetbrains.annotations.NotNull; + +public class LambdaPostfixTemplateTest extends PostfixTemplateTestCase { + private LanguageLevel myDefaultLanguageLevel; + + @Override + protected void setUp() throws Exception { + super.setUp(); + LanguageLevelProjectExtension levelProjectExtension = LanguageLevelProjectExtension.getInstance(myFixture.getProject()); + myDefaultLanguageLevel = levelProjectExtension.getLanguageLevel(); + levelProjectExtension.setLanguageLevel(LanguageLevel.JDK_1_8); + } + + @Override + protected void tearDown() throws Exception { + try { + LanguageLevelProjectExtension.getInstance(myFixture.getProject()).setLanguageLevel(myDefaultLanguageLevel); + myDefaultLanguageLevel = null; + } + finally { + //noinspection ThrowFromFinallyBlock + super.tearDown(); + } + } + + @NotNull + @Override + protected String getSuffix() { + return "lambda"; + } + + public void testSimple() { + doTest(); + } + + public void testDoNotExpandOnJavaLess8() { + LanguageLevelProjectExtension.getInstance(myFixture.getProject()).setLanguageLevel(LanguageLevel.JDK_1_6); + doTest(); + } +} + diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/OptionalPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/OptionalPostfixTemplateTest.java new file mode 100644 index 000000000000..f90cfd1d83df --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/OptionalPostfixTemplateTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.postfix.templates; + +import com.intellij.openapi.roots.LanguageLevelProjectExtension; +import com.intellij.pom.java.LanguageLevel; +import org.jetbrains.annotations.NotNull; + +public class OptionalPostfixTemplateTest extends PostfixTemplateTestCase { + private LanguageLevel myDefaultLanguageLevel; + + @Override + protected void setUp() throws Exception { + super.setUp(); + LanguageLevelProjectExtension levelProjectExtension = LanguageLevelProjectExtension.getInstance(myFixture.getProject()); + myDefaultLanguageLevel = levelProjectExtension.getLanguageLevel(); + levelProjectExtension.setLanguageLevel(LanguageLevel.JDK_1_8); + } + + @Override + protected void tearDown() throws Exception { + try { + LanguageLevelProjectExtension.getInstance(myFixture.getProject()).setLanguageLevel(myDefaultLanguageLevel); + myDefaultLanguageLevel = null; + } + finally { + //noinspection ThrowFromFinallyBlock + super.tearDown(); + } + } + + @NotNull + @Override + protected String getSuffix() { + return "opt"; + } + + public void testExpression() { + doTest(); + } + + public void testBoxedType() { + doTest(); + } + + public void testPrimitiveType() { + doTest(); + } + + public void testIntLiteral() { + doTest(); + } + + public void testArray() { + doTest(); + } + + public void testInt() { + doTest(); + } + + public void testDouble() { + doTest(); + } + + public void testLong() { + doTest(); + } + + public void testDoNotExpandOnJavaLess8() { + LanguageLevelProjectExtension.getInstance(myFixture.getProject()).setLanguageLevel(LanguageLevel.JDK_1_6); + doTest(); + } +} + diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplateTest.java new file mode 100644 index 000000000000..c3828b3cf1e0 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplateTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.postfix.templates; + +import com.intellij.openapi.roots.LanguageLevelProjectExtension; +import com.intellij.pom.java.LanguageLevel; +import org.jetbrains.annotations.NotNull; + +public class StreamPostfixTemplateTest extends PostfixTemplateTestCase { + private LanguageLevel myDefaultLanguageLevel; + + @Override + protected void setUp() throws Exception { + super.setUp(); + LanguageLevelProjectExtension levelProjectExtension = LanguageLevelProjectExtension.getInstance(myFixture.getProject()); + myDefaultLanguageLevel = levelProjectExtension.getLanguageLevel(); + levelProjectExtension.setLanguageLevel(LanguageLevel.JDK_1_8); + } + + @Override + protected void tearDown() throws Exception { + try { + LanguageLevelProjectExtension.getInstance(myFixture.getProject()).setLanguageLevel(myDefaultLanguageLevel); + myDefaultLanguageLevel = null; + } + finally { + //noinspection ThrowFromFinallyBlock + super.tearDown(); + } + } + + @NotNull + @Override + protected String getSuffix() { + return "stream"; + } + + public void testSimple() { + doTest(); + } + + public void testDoNotExpandOnJavaLess8() { + LanguageLevelProjectExtension.getInstance(myFixture.getProject()).setLanguageLevel(LanguageLevel.JDK_1_6); + doTest(); + } +} +