From 4af912ddd49f6c674b6853fb395e0d4b8974e3d7 Mon Sep 17 00:00:00 2001 From: Abhijit Kulkarni Date: Mon, 13 Nov 2023 10:30:30 -0800 Subject: [PATCH] JSpecify: handling the return of a diamond operator anonymous object method caller (#858) In reference to the [exception](https://github.com/uber/NullAway/issues/791#issuecomment-1803020173) mentioned in the discussion for #791 . Adding the test case to reproduce the same issue: ```java class Test { static class B{ String build(){return "x";} } static String testNegative() { //We were getting the aforementioned mentioned exception when we tried to do this return new B<>().build(); } } ``` All unit tests have passed for the changes that were made for this. --------- Co-authored-by: Manu Sridharan --- .../java/com/uber/nullaway/GenericsChecks.java | 11 +++++++---- .../NullAwayJSpecifyGenericsTests.java | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java b/nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java index ef56490473..4870366771 100644 --- a/nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java @@ -788,10 +788,13 @@ public static Nullness getGenericReturnNullnessAtInvocation( return Nullness.NONNULL; } Type methodReceiverType = - castToNonNull( - getTreeType(((MemberSelectTree) tree.getMethodSelect()).getExpression(), state)); - return getGenericMethodReturnTypeNullness( - invokedMethodSymbol, methodReceiverType, state, config); + getTreeType(((MemberSelectTree) tree.getMethodSelect()).getExpression(), state); + if (methodReceiverType == null) { + return Nullness.NONNULL; + } else { + return getGenericMethodReturnTypeNullness( + invokedMethodSymbol, methodReceiverType, state, config); + } } /** diff --git a/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java b/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java index 58f09d1f8a..eb7c02fe65 100644 --- a/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java @@ -1522,6 +1522,24 @@ public void testForStaticMethodCallAsAParam() { .doTest(); } + @Test + public void testForDiamondOperatorReturnedAsAMethodCaller() { + makeHelper() + .addSourceLines( + "Test.java", + "package com.uber;", + "import org.jspecify.annotations.Nullable;", + "class Test {", + " static class B{", + " String build(){return \"x\";}", + " }", + " static String testNegative() {", + " return new B<>().build();", + " }", + "}") + .doTest(); + } + private CompilationTestHelper makeHelper() { return makeTestHelperWithArgs( Arrays.asList(