From c4c7c4bbb0f874127a74c0d28111eaf9137e5783 Mon Sep 17 00:00:00 2001 From: Christopher Lambert Date: Fri, 20 Jan 2023 19:24:25 +0100 Subject: [PATCH] Add Throwable.getCause and getLocalizedMessage() library models (#717) They both can return `null`: - In its default implementation `getLocalizedMessage()` calls `getMessage()`, so should be treated in the same way. - Not every throwable was created with a cause, so `getCause()` will often return `null`, as indicated by its javadoc. --- .../java/com/uber/nullaway/handlers/LibraryModelsHandler.java | 2 ++ .../com/uber/nullaway/testdata/NullAwayNativeModels.java | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java b/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java index 3f12a03475..91879bf60f 100644 --- a/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java +++ b/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java @@ -620,6 +620,8 @@ private static class DefaultLibraryModels implements LibraryModels { new ImmutableSet.Builder() .add(methodRef("com.sun.source.tree.CompilationUnitTree", "getPackageName()")) .add(methodRef("java.lang.Throwable", "getMessage()")) + .add(methodRef("java.lang.Throwable", "getLocalizedMessage()")) + .add(methodRef("java.lang.Throwable", "getCause()")) .add(methodRef("java.lang.ref.Reference", "get()")) .add(methodRef("java.lang.ref.PhantomReference", "get()")) .add(methodRef("java.lang.ref.SoftReference", "get()")) diff --git a/nullaway/src/test/resources/com/uber/nullaway/testdata/NullAwayNativeModels.java b/nullaway/src/test/resources/com/uber/nullaway/testdata/NullAwayNativeModels.java index be70a6b30d..8c337e9d6c 100644 --- a/nullaway/src/test/resources/com/uber/nullaway/testdata/NullAwayNativeModels.java +++ b/nullaway/src/test/resources/com/uber/nullaway/testdata/NullAwayNativeModels.java @@ -58,6 +58,10 @@ public static void referenceStuff() { Exception e = new RuntimeException(); // BUG: Diagnostic contains: dereferenced expression e.getMessage().hashCode(); + // BUG: Diagnostic contains: dereferenced expression + e.getLocalizedMessage().hashCode(); + // BUG: Diagnostic contains: dereferenced expression + e.getCause().toString(); } // we will add bug annotations when we have full support for maps