mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
use Optional.of() instead ofNullable() in "opt" postfix template when expression is not null
This commit is contained in:
+16
-16
@@ -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$)";
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
class Test {
|
||||
@NotNull
|
||||
String foo() {
|
||||
return "";
|
||||
}
|
||||
|
||||
void m() {
|
||||
foo().opt<caret>
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
class Test {
|
||||
@NotNull
|
||||
String foo() {
|
||||
return "";
|
||||
}
|
||||
|
||||
void m() {
|
||||
java.util.Optional.of(foo())
|
||||
}
|
||||
}
|
||||
+6
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user