mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
IDEA-160797 Support Objects.requireNonNull as field initialization in constructor
GitOrigin-RevId: 738f0e842ded8e9d88ef4432cd6c093173400a89
This commit is contained in:
committed by
intellij-monorepo-bot
parent
6ffe6658b4
commit
20630cfd4f
@@ -16,6 +16,7 @@
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.NullableNotNullManager;
|
||||
import com.intellij.codeInspection.dataFlow.JavaMethodContractUtil;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
@@ -30,6 +31,7 @@ import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -123,6 +125,14 @@ public final class FieldFromParameterUtils {
|
||||
}
|
||||
else {
|
||||
PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
|
||||
if (parent instanceof PsiExpressionList) {
|
||||
// skip validating calls like Objects.requireNonNull
|
||||
PsiMethodCallExpression call = ObjectUtils.tryCast(parent.getParent(), PsiMethodCallExpression.class);
|
||||
PsiExpression returnedValue = PsiUtil.skipParenthesizedExprDown(JavaMethodContractUtil.findReturnedValue(call));
|
||||
if (returnedValue == expression) {
|
||||
parent = PsiUtil.skipParenthesizedExprUp(call.getParent());
|
||||
}
|
||||
}
|
||||
assignmentExpression = parent instanceof PsiAssignmentExpression ? (PsiAssignmentExpression)parent : null;
|
||||
}
|
||||
if (assignmentExpression == null) continue;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// "Create field for parameter 'val'" "true"
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
class Test{
|
||||
String s;
|
||||
private String myVal;
|
||||
|
||||
public void Test(String val, String message) {
|
||||
myVal = val;
|
||||
s = Objects.requireNonNull(message, val);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Create field for parameter 'val'" "false"
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
class Test{
|
||||
String s;
|
||||
|
||||
public void Test(String <caret>val, String message) {
|
||||
s = Objects.requireNonNull(val, message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Create field for parameter 'val'" "true"
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
class Test{
|
||||
String s;
|
||||
|
||||
public void Test(String <caret>val, String message) {
|
||||
s = Objects.requireNonNull(message, val);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ package com.intellij.java.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import static com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase.JAVA_8;
|
||||
|
||||
/**
|
||||
* @author ven
|
||||
@@ -28,6 +32,12 @@ public class CreateFieldFromParameterTest extends LightIntentionActionTestCase {
|
||||
JavaCodeStyleSettings.getInstance(getProject()).FIELD_NAME_PREFIX = "my";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected LightProjectDescriptor getProjectDescriptor() {
|
||||
return JAVA_8;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/createFieldFromParameter";
|
||||
|
||||
Reference in New Issue
Block a user