mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
IDEA-176650 Provide a quick fix of "can produce NPE" that wraps qualifier with Obejcts.requireNonNull
This commit is contained in:
+5
-4
@@ -24,10 +24,7 @@ import com.intellij.codeInsight.daemon.impl.quickfix.SimplifyBooleanExpressionFi
|
||||
import com.intellij.codeInsight.intention.impl.AddNotNullAnnotationFix;
|
||||
import com.intellij.codeInsight.intention.impl.AddNullableAnnotationFix;
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.codeInspection.dataFlow.fix.RedundantInstanceofFix;
|
||||
import com.intellij.codeInspection.dataFlow.fix.ReplaceWithConstantValueFix;
|
||||
import com.intellij.codeInspection.dataFlow.fix.ReplaceWithObjectsEqualsFix;
|
||||
import com.intellij.codeInspection.dataFlow.fix.SimplifyToAssignmentFix;
|
||||
import com.intellij.codeInspection.dataFlow.fix.*;
|
||||
import com.intellij.codeInspection.dataFlow.instructions.*;
|
||||
import com.intellij.codeInspection.dataFlow.value.DfaConstValue;
|
||||
import com.intellij.codeInspection.dataFlow.value.DfaUnknownValue;
|
||||
@@ -286,6 +283,10 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
|
||||
}
|
||||
}
|
||||
|
||||
if (PsiUtil.isLanguageLevel7OrHigher(qualifier)) {
|
||||
fixes.add(new SurroundWithRequireNonNullFix(qualifier));
|
||||
}
|
||||
|
||||
ContainerUtil.addIfNotNull(fixes, DfaOptionalSupport.registerReplaceOptionalOfWithOfNullableFix(qualifier));
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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.codeInspection.dataFlow.fix;
|
||||
|
||||
import com.intellij.codeInspection.InspectionsBundle;
|
||||
import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.SmartPointerManager;
|
||||
import com.intellij.psi.SmartPsiElementPointer;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SurroundWithRequireNonNullFix implements LocalQuickFix {
|
||||
private final String myText;
|
||||
private final SmartPsiElementPointer<PsiExpression> myQualifierPointer;
|
||||
|
||||
public SurroundWithRequireNonNullFix(@NotNull PsiExpression expressionToSurround) {
|
||||
myText = expressionToSurround.getText();
|
||||
myQualifierPointer =
|
||||
SmartPointerManager.getInstance(expressionToSurround.getProject()).createSmartPsiElementPointer(expressionToSurround);
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return InspectionsBundle.message("inspection.surround.requirenonnull.quickfix", myText);
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return InspectionsBundle.message("inspection.surround.requirenonnull.quickfix", "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
PsiExpression qualifier = myQualifierPointer.getElement();
|
||||
if (qualifier == null) return;
|
||||
PsiExpression replacement = JavaPsiFacade.getElementFactory(project)
|
||||
.createExpressionFromText("java.util.Objects.requireNonNull(" + qualifier.getText() + ")", qualifier);
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(qualifier.replace(replacement));
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace with 'Objects.requireNonNull(arr)'" "true"
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
class MyClass {
|
||||
void test() {
|
||||
int[] arr = Math.random() > 0.5 ? null : new int[10];
|
||||
System.out.println(Objects.requireNonNull(arr)[1]);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Replace with 'Objects.requireNonNull(getObject())'" "true"
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
class MyClass {
|
||||
int a;
|
||||
|
||||
static MyClass getObject() {
|
||||
return Math.random() > 0.5 ? new MyClass() : null;
|
||||
}
|
||||
void test() {
|
||||
Objects.requireNonNull(getObject()).a = 5;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Replace with 'Objects.requireNonNull(arr)'" "true"
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
class MyClass {
|
||||
void foo(String[] arr) {}
|
||||
|
||||
void test() {
|
||||
String[] arr = Math.random() > 0.5 ? null : new String[10];
|
||||
foo(Objects.requireNonNull(arr));
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Replace with 'Objects.requireNonNull(Math.random() > 0.5 ? null : "bar")'" "true"
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
class MyClass {
|
||||
void foo(String str) {}
|
||||
|
||||
void test() {
|
||||
foo(Objects.requireNonNull(Math.random() > 0.5 ? null : "bar"));
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace with 'Objects.requireNonNull(s1)'" "true"
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
class MyClass {
|
||||
void test(List<String> list) {
|
||||
list.stream().map(s -> s.isEmpty() ? null : s)
|
||||
.map(s1 -> Objects.requireNonNull(s1).trim())
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace with 'Objects.requireNonNull(arr)'" "true"
|
||||
|
||||
class MyClass {
|
||||
void test() {
|
||||
int[] arr = Math.random() > 0.5 ? null : new int[10];
|
||||
System.out.println(arr<caret>[1]);
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Replace with 'Objects.requireNonNull(getObject())'" "true"
|
||||
|
||||
class MyClass {
|
||||
int a;
|
||||
|
||||
static MyClass getObject() {
|
||||
return Math.random() > 0.5 ? new MyClass() : null;
|
||||
}
|
||||
void test() {
|
||||
getObject<caret>().a = 5;
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace with 'Objects.requireNonNull(arr)'" "true"
|
||||
|
||||
class MyClass {
|
||||
void foo(String[] arr) {}
|
||||
|
||||
void test() {
|
||||
String[] arr = Math.random() > 0.5 ? null : new String[10];
|
||||
foo(ar<caret>r);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace with 'Objects.requireNonNull(Math.random() > 0.5 ? null : "bar")'" "true"
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class MyClass {
|
||||
void foo(String str) {}
|
||||
|
||||
void test() {
|
||||
foo(Math.random() > 0.5 ? null :<caret> "bar");
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace with 'Objects.requireNonNull(s1)'" "true"
|
||||
import java.util.List;
|
||||
|
||||
class MyClass {
|
||||
void test(List<String> list) {
|
||||
list.stream().map(s -> s.isEmpty() ? null : s)
|
||||
.map(s1 -> s1.t<caret>rim())
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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.java.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.dataFlow.DataFlowInspection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SurroundWithRequireNonNullFixTest extends LightQuickFixParameterizedTestCase {
|
||||
@NotNull
|
||||
@Override
|
||||
protected LocalInspectionTool[] configureLocalInspectionTools() {
|
||||
DataFlowInspection inspection = new DataFlowInspection();
|
||||
inspection.SUGGEST_NULLABLE_ANNOTATIONS = true;
|
||||
return new LocalInspectionTool[]{inspection};
|
||||
}
|
||||
|
||||
public void test() throws Exception {
|
||||
doAllTests();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithRequireNonNull";
|
||||
}
|
||||
}
|
||||
@@ -326,6 +326,7 @@ inspection.surround.if.quickfix=Surround with ''if ({0} != null)''
|
||||
inspection.replace.ternary.quickfix=Replace with ''{0} != null ?:''
|
||||
inspection.surround.if.family=Surround with if
|
||||
inspection.dependency.configure.button.text=Configure dependency rules
|
||||
inspection.surround.requirenonnull.quickfix=Replace with ''Objects.requireNonNull({0})''
|
||||
|
||||
inspection.javadoc.label.text=Additional Javadoc Tags:
|
||||
inspection.javadoc.dialog.title=Edit Additional Javadoc Tags
|
||||
|
||||
Reference in New Issue
Block a user