method reference: check NPE unboxing (IDEA-133714)

This commit is contained in:
Anna Kozlova
2014-12-02 13:55:43 +01:00
parent a18e9031bf
commit e17a949a6e
5 changed files with 39 additions and 1 deletions

View File

@@ -44,6 +44,7 @@ import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.extractMethod.ExtractMethodUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ArrayUtilRt;
@@ -107,6 +108,21 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
analyzeCodeBlock(initializer.getBody(), holder, isOnTheFly);
}
@Override
public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
super.visitMethodReferenceExpression(expression);
final PsiElement resolve = expression.resolve();
if (resolve instanceof PsiMethod) {
final PsiType methodReturnType = ((PsiMethod)resolve).getReturnType();
if (TypeConversionUtil.isPrimitiveWrapper(methodReturnType) && NullableNotNullManager.isNullable((PsiMethod)resolve)) {
final PsiType returnType = LambdaUtil.getFunctionalInterfaceReturnType(expression);
if (TypeConversionUtil.isPrimitiveAndNotNull(returnType)) {
holder.registerProblem(expression, InspectionsBundle.message("dataflow.message.unboxing.method.reference"));
}
}
}
}
@Override
public void visitIfStatement(PsiIfStatement statement) {
PsiExpression condition = statement.getCondition();

View File

@@ -38,7 +38,7 @@ public class LambdaUtil {
private static final Logger LOG = Logger.getInstance("#" + LambdaUtil.class.getName());
@Nullable
public static PsiType getFunctionalInterfaceReturnType(PsiLambdaExpression expr) {
public static PsiType getFunctionalInterfaceReturnType(PsiFunctionalExpression expr) {
return getFunctionalInterfaceReturnType(expr.getFunctionalInterfaceType());
}

View File

@@ -0,0 +1,17 @@
class X {
interface I {
int get();
}
interface J {
Integer get();
}
I is = <warning descr="Use of 'X::m' would need unboxing which may produce 'java.lang.NullPointerException'">X::m</warning>;
J j = X::m;
@org.jetbrains.annotations.Nullable
static Integer m() {
return null;
}
}

View File

@@ -57,6 +57,10 @@ public class DataFlowInspection8Test extends LightCodeInsightFixtureTestCase {
doTest();
}
public void testUnboxingInMethodReferences() throws Exception {
doTest();
}
private void setupCustomAnnotations() {
myFixture.addClass("package foo;\n\nimport java.lang.annotation.*;\n\n@Target({ElementType.TYPE_USE}) public @interface Nullable { }");
myFixture.addClass("package foo;\n\nimport java.lang.annotation.*;\n\n@Target({ElementType.TYPE_USE}) public @interface NotNull { }");