Skip to content

Commit

Permalink
fixes with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msridhar committed Feb 24, 2025
1 parent b70e03f commit bbfd36c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,21 @@ public NullnessStore getNullnessInfoBeforeNestedMethodNode(
for (int i = 0; i < elements.size(); i++) {
AccessPathElement ape = elements.get(i);
Element e = ape.getJavaElement();
if (i != elements.size() - 1) { // "inner" elements of the access path, must be fields
if (!e.getModifiers().contains(Modifier.FINAL)) {
if (i != elements.size() - 1) { // "inner" elements of the access path
if (!e.getKind().equals(ElementKind.FIELD)
|| !e.getModifiers().contains(Modifier.FINAL)) {
allAPNonRootElementsAreFinalFields = false;
break;
}
} else { // last element; can be a final field or @MonotonicNonNull field
if (!e.getModifiers().contains(Modifier.FINAL)
&& !e.getAnnotationMirrors().stream()
.anyMatch(
am ->
Nullness.isMonotonicNonNullAnnotation(
am.getAnnotationType().toString()))) {
} else { // last element
// must be a field that is final or annotated with @MonotonicNonNull
if (!e.getKind().equals(ElementKind.FIELD)
|| (!e.getModifiers().contains(Modifier.FINAL)
&& !e.getAnnotationMirrors().stream()
.anyMatch(
am ->
Nullness.isMonotonicNonNullAnnotation(
am.getAnnotationType().toString())))) {
allAPNonRootElementsAreFinalFields = false;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,48 @@ public void nestedObjects() {
"}")
.doTest();
}

@Test
public void accessPathsWithMethodCalls() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"package com.uber;",
"import com.uber.nullaway.annotations.MonotonicNonNull;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" class Foo {",
" @MonotonicNonNull Object x;",
" }",
" Foo f1 = new Foo();",
" final Foo getF1() {",
" return f1;",
" }",
" final @Nullable Foo getOther() {",
" return null;",
" }",
" void testPositive1() {",
" getF1().x = new Object();",
" Runnable r = () -> {",
" // BUG: Diagnostic contains: dereferenced expression",
" getF1().x.toString();",
" };",
" }",
" void testPositive2() {",
" if (getOther() != null) {",
" getOther().x = new Object();",
" Runnable r1 = () -> {",
" // getOther() should be treated as @Nullable in the lambda",
" // BUG: Diagnostic contains: dereferenced expression",
" getOther().toString();",
" };",
" Runnable r2 = () -> {",
" // BUG: Diagnostic contains: dereferenced expression",
" getOther().x.toString();",
" };",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit bbfd36c

Please sign in to comment.