Enum.valueOf and annotations methods are not-null (IDEA-151305, IDEA-151174)

This commit is contained in:
peter
2016-02-10 12:39:19 +01:00
parent 97c4d325e2
commit c1fdc02b1a
4 changed files with 27 additions and 1 deletions
@@ -70,9 +70,18 @@ public class DfaPsiUtil {
return Nullness.UNKNOWN;
}
if (owner instanceof PsiEnumConstant) {
if (owner instanceof PsiEnumConstant || PsiUtil.isAnnotationMethod(owner)) {
return Nullness.NOT_NULL;
}
if (owner instanceof PsiMethod) {
PsiMethod method = (PsiMethod)owner;
if ("valueOf".equals(method.getName()) && method.hasModifierProperty(PsiModifier.STATIC)) {
PsiClass containingClass = method.getContainingClass();
if (containingClass != null && containingClass.isEnum()) {
return Nullness.NOT_NULL;
}
}
}
if (resultType != null) {
NullableNotNullManager nnn = NullableNotNullManager.getInstance(owner.getProject());
@@ -0,0 +1,11 @@
class Test {
@interface FooBar {
String x();
}
static void baz(FooBar annotation) {
if (<warning descr="Condition 'annotation.x() == null' is always 'false'">annotation.x() == null</warning>) {
System.out.println("null");
}
}
}
@@ -12,6 +12,10 @@ class FooWithComments {
}
}
boolean checkValueOf(String name) {
return <warning descr="Condition 'AnEnum.valueOf(name) != null' is always 'true'">AnEnum.valueOf(name) != null</warning>;
}
private native void anotherMethod(String name);
@Nullable
@@ -91,6 +91,8 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testGreaterIsNotEquals() throws Throwable { doTest(); }
public void testNotGreaterIsNotEquals() throws Throwable { doTest(); }
public void testAnnotationMethodNotNull() { doTest(); }
public void testChainedFinalFieldsDfa() throws Throwable { doTest(); }
public void testFinalFieldsDifferentInstances() throws Throwable { doTest(); }
public void testThisFieldGetters() throws Throwable { doTest(); }