diff --git a/infer/models/java/src/com/google/common/base/Optional.java b/infer/models/java/src/com/google/common/base/Optional.java new file mode 100644 index 000000000..c1fd2e068 --- /dev/null +++ b/infer/models/java/src/com/google/common/base/Optional.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2013 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.google.common.base; + +import javax.annotation.Nullable; + +public abstract class Optional { + + @Nullable + public abstract T orNull(); + +} diff --git a/infer/tests/build_systems/expected_outputs/ant_report.json b/infer/tests/build_systems/expected_outputs/ant_report.json index e8860417f..f68869805 100644 --- a/infer/tests/build_systems/expected_outputs/ant_report.json +++ b/infer/tests/build_systems/expected_outputs/ant_report.json @@ -649,6 +649,11 @@ "file": "codetoanalyze/java/infer/ResourceLeaks.java", "procedure": "void ResourceLeaks.openHttpsURLConnectionNotDisconnected()" }, + { + "bug_type": "NULL_DEREFERENCE", + "file": "codetoanalyze/java/infer/NullPointerExceptions.java", + "procedure": "void NullPointerExceptions.optionalNPE(Optional)" + }, { "bug_type": "NULL_DEREFERENCE", "file": "codetoanalyze/java/infer/NullPointerExceptions.java", diff --git a/infer/tests/build_systems/expected_outputs/buck_report.json b/infer/tests/build_systems/expected_outputs/buck_report.json index 4ff82a3c2..a0374eb9a 100644 --- a/infer/tests/build_systems/expected_outputs/buck_report.json +++ b/infer/tests/build_systems/expected_outputs/buck_report.json @@ -649,6 +649,11 @@ "file": "infer/tests/codetoanalyze/java/infer/ResourceLeaks.java", "procedure": "void ResourceLeaks.openHttpsURLConnectionNotDisconnected()" }, + { + "bug_type": "NULL_DEREFERENCE", + "file": "infer/tests/codetoanalyze/java/infer/NullPointerExceptions.java", + "procedure": "void NullPointerExceptions.optionalNPE(Optional)" + }, { "bug_type": "NULL_DEREFERENCE", "file": "infer/tests/codetoanalyze/java/infer/NullPointerExceptions.java", diff --git a/infer/tests/codetoanalyze/java/infer/NullPointerExceptions.java b/infer/tests/codetoanalyze/java/infer/NullPointerExceptions.java index b8c97e7ad..21a2bb147 100644 --- a/infer/tests/codetoanalyze/java/infer/NullPointerExceptions.java +++ b/infer/tests/codetoanalyze/java/infer/NullPointerExceptions.java @@ -15,6 +15,7 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.text.TextUtils; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; @@ -558,4 +559,9 @@ public class NullPointerExceptions { s.toString(); // Expect NPE here } + void optionalNPE(Optional o) { + o.orNull().toString(); + } + + } diff --git a/infer/tests/endtoend/java/infer/NullPointerExceptionTest.java b/infer/tests/endtoend/java/infer/NullPointerExceptionTest.java index 6b62ba9f3..aea1cabf2 100644 --- a/infer/tests/endtoend/java/infer/NullPointerExceptionTest.java +++ b/infer/tests/endtoend/java/infer/NullPointerExceptionTest.java @@ -74,6 +74,7 @@ public class NullPointerExceptionTest { "dereferenceAfterLoopOnList", "dereferenceAfterUnlock1", "dereferenceAfterUnlock2", + "optionalNPE", }; assertThat( "Results should contain " + NULL_DEREFERENCE,