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 79c6e3558866..072bee170b61 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 @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -16,6 +16,8 @@ package com.intellij.codeInsight.template.postfix.templates; import com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils; +import com.intellij.codeInspection.dataFlow.Nullness; +import com.intellij.codeInspection.dataFlow.NullnessUtil; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiExpression; import com.intellij.psi.PsiPrimitiveType; @@ -35,23 +37,21 @@ public class OptionalPostfixTemplate extends StringBasedPostfixTemplate { @Override public String getTemplateString(@NotNull PsiElement element) { 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"; + + 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"; } } + + String methodName = Nullness.NOT_NULL.equals(NullnessUtil.getExpressionNullness((PsiExpression)element)) ? "of" : "ofNullable"; return "java.util." + className + "." + methodName + "($expr$)"; } } diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/notNullMethodCall.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/notNullMethodCall.java new file mode 100644 index 000000000000..f874970148fd --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/notNullMethodCall.java @@ -0,0 +1,12 @@ +import org.jetbrains.annotations.NotNull; + +class Test { + @NotNull + String foo() { + return ""; + } + + void m() { + foo().opt + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/opt/notNullMethodCall_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/notNullMethodCall_after.java new file mode 100644 index 000000000000..e5331c237e5e --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/opt/notNullMethodCall_after.java @@ -0,0 +1,12 @@ +import org.jetbrains.annotations.NotNull; + +class Test { + @NotNull + String foo() { + return ""; + } + + void m() { + java.util.Optional.of(foo()) + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/OptionalPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/OptionalPostfixTemplateTest.java index de78213fb37e..e86e9e559c74 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/OptionalPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/OptionalPostfixTemplateTest.java @@ -80,6 +80,12 @@ public class OptionalPostfixTemplateTest extends PostfixTemplateTestCase { public void testLong() { doTest(); } + + public void testNotNullMethodCall() { + myFixture.addClass("package org.jetbrains.annotations;" + + "public @interface NotNull {}"); + doTest(); + } public void testDoNotExpandOnJavaLess8() { IdeaTestUtil.setModuleLanguageLevel(myModule, LanguageLevel.JDK_1_6, myFixture.getTestRootDisposable());