diff --git a/infer/models/java/src/java/lang/Integer.java b/infer/models/java/src/java/lang/Integer.java new file mode 100644 index 000000000..85ecca25f --- /dev/null +++ b/infer/models/java/src/java/lang/Integer.java @@ -0,0 +1,28 @@ +package java.lang; + +public final class Integer { + + public static int MAX_VALUE = 2147483647; // 2**31-1 + public static int MIN_VALUE = -2147483648; // -2**31 + + protected final int value; + + public Integer(int i) { + this.value = i; + } + + public static Integer valueOf(int i) { + return new Integer(i); + } + + public boolean equals(Object anObject) { + return anObject != null + && anObject instanceof Integer + && this.value == ((Integer) anObject).value; + } + + public int intValue() { + return this.value; + } + +} diff --git a/infer/tests/codetoanalyze/java/infer/IntegerExample.java b/infer/tests/codetoanalyze/java/infer/IntegerExample.java new file mode 100644 index 000000000..6f3887281 --- /dev/null +++ b/infer/tests/codetoanalyze/java/infer/IntegerExample.java @@ -0,0 +1,19 @@ +import java.util.HashMap; + +public class IntegerExample { + + private static void testIntegerEquals() { + Integer a = new Integer(42); + Integer b = new Integer(42); + Integer c = null; + + if (!a.equals(b)) { + c.intValue(); + } + + if (a != 42) { + c.intValue(); + } + } + +} diff --git a/infer/tests/endtoend/java/infer/IntegerClassTest.java b/infer/tests/endtoend/java/infer/IntegerClassTest.java new file mode 100644 index 000000000..98907a9f6 --- /dev/null +++ b/infer/tests/endtoend/java/infer/IntegerClassTest.java @@ -0,0 +1,44 @@ +// Copyright (c) 2015-Present Facebook. All rights reserved. + +package endtoend.java.infer; + +import static org.hamcrest.MatcherAssert.assertThat; +import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain; + +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; + +import utils.InferException; +import utils.InferResults; + +public class IntegerClassTest { + + public static final String SOURCE_FILE = + "infer/tests/codetoanalyze/java/infer/IntegerExample.java"; + + public static final String NULL_DEREFERENCE = "NULL_DEREFERENCE"; + + private static InferResults inferResults; + + @BeforeClass + public static void loadResults() throws IOException { + inferResults = InferResults.loadInferResults(IntegerClassTest.class, SOURCE_FILE); + } + + @Test + public void test() + throws InterruptedException, IOException, InferException { + assertThat( + "Results should not contain NPE errors", + inferResults, + doesNotContain( + NULL_DEREFERENCE, + SOURCE_FILE, + "testIntegerEquals" + ) + ); + } + +}