From 9e1704670c9f729f4464f481283f9d26b3ac5365 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 16 Sep 2016 14:34:51 +0200 Subject: [PATCH] support org.springframework.util.Assert.notNull (IDEA-159977) --- .../codeInspection/dataFlow/HardcodedContracts.java | 5 +++++ .../inspection/dataFlow/fixture/SpringAssert.java | 11 +++++++++++ .../codeInspection/HardcodedContractsTest.java | 2 ++ 3 files changed, 18 insertions(+) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/HardcodedContracts.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/HardcodedContracts.java index db6aa677095c..c9097d2f1e13 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/HardcodedContracts.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/HardcodedContracts.java @@ -74,6 +74,11 @@ public class HardcodedContracts { constraints[0] = FALSE_VALUE; return Collections.singletonList(new MethodContract(constraints, THROW_EXCEPTION)); } + if ("notNull".equals(methodName) && paramCount > 0) { + MethodContract.ValueConstraint[] constraints = createConstraintArray(paramCount); + constraints[0] = NULL_VALUE; + return Collections.singletonList(new MethodContract(constraints, THROW_EXCEPTION)); + } } else if ("junit.framework.Assert".equals(className) || "org.junit.Assert".equals(className) || diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/SpringAssert.java b/java/java-tests/testData/inspection/dataFlow/fixture/SpringAssert.java index 8fc586956569..a3699d5cf922 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/SpringAssert.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/SpringAssert.java @@ -1,4 +1,5 @@ import org.springframework.util.Assert; +import org.jetbrains.annotations.Nullable; class Contracts { @@ -12,4 +13,14 @@ class Contracts { String s = (String) o; } + void foo2(@Nullable Object o) { + Assert.notNull(o); + System.out.println(o.hashCode()); + } + + void foo3(@Nullable Object o) { + Assert.notNull(o, "not null"); + System.out.println(o.hashCode()); + } + } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/HardcodedContractsTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/HardcodedContractsTest.java index 47a8eae850fd..fcbaba1eeadf 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/HardcodedContractsTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/HardcodedContractsTest.java @@ -102,6 +102,8 @@ public class HardcodedContractsTest extends DataFlowInspectionTestCase { myFixture.addClass("package org.springframework.util; public class Assert {\n" + " public static void isTrue(boolean expression) {}\n" + " public static void state(boolean expression, String s) {}\n" + + " public static void notNull(Object o) {}\n" + + " public static void notNull(Object o, String s) {}\n" + "}"); checkHighlighting(); }