Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.uber.nullaway.Nullness.NULLABLE;
import static com.uber.nullaway.librarymodel.NestedAnnotationInfo.TypePathEntry.Kind.ARRAY_ELEMENT;
import static com.uber.nullaway.librarymodel.NestedAnnotationInfo.TypePathEntry.Kind.TYPE_ARGUMENT;
import static com.uber.nullaway.librarymodel.NestedAnnotationInfo.TypePathEntry.Kind.WILDCARD_BOUND;

import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
Expand Down Expand Up @@ -750,6 +751,9 @@ private static class DefaultLibraryModels implements LibraryModels {
"android.view.GestureDetector.OnGestureListener",
"onScroll(android.view.MotionEvent,android.view.MotionEvent,float,float)"),
0)
.put(methodRef("java.util.Optional", "<T>ofNullable(T)"), 0)
.put(methodRef("java.util.Optional", "orElse(T)"), 0)
.put(methodRef("java.util.Optional", "equals(java.lang.Object)"), 0)
.build();

private static final ImmutableSetMultimap<MethodRef, Integer> NON_NULL_PARAMETERS =
Expand Down Expand Up @@ -980,6 +984,11 @@ private static class DefaultLibraryModels implements LibraryModels {
.add(methodRef("android.view.View", "getHandler()"))
.add(methodRef("android.webkit.WebView", "getUrl()"))
.add(methodRef("android.widget.TextView", "getLayout()"))
.add(methodRef("java.util.Optional", "orElse(T)"))
// https://github.com/uber/NullAway/issues/1616
// .add(
// methodRef(
// "java.util.Optional", "orElseGet(java.util.function.Supplier<? extends T>)"))
.add(methodRef("java.lang.System", "console()"))
.build();

Expand Down Expand Up @@ -1055,21 +1064,48 @@ private static class DefaultLibraryModels implements LibraryModels {
private static final ImmutableMap<
MethodRef, ImmutableSetMultimap<Integer, NestedAnnotationInfo>>
NESTED_ANNOTATIONS_FOR_METHODS =
ImmutableMap.of(
methodRef(
"java.util.concurrent.atomic.AtomicReferenceFieldUpdater",
"<U,W>newUpdater(java.lang.Class<U>,java.lang.Class<W>,java.lang.String)"),
// turns Class<W> into Class<@NonNull W>
new ImmutableMap.Builder<
MethodRef, ImmutableSetMultimap<Integer, NestedAnnotationInfo>>()
.put(
methodRef(
"java.util.concurrent.atomic.AtomicReferenceFieldUpdater",
"<U,W>newUpdater(java.lang.Class<U>,java.lang.Class<W>,java.lang.String)"),
// turns Class<W> into Class<@NonNull W>
ImmutableSetMultimap.of(
1,
new NestedAnnotationInfo(
Annotation.NONNULL,
ImmutableList.of(
new NestedAnnotationInfo.TypePathEntry(TYPE_ARGUMENT, 0)))))
.put(
methodRef(
"java.util.Optional",
"<U>map(java.util.function.Function<? super T,? extends U>)"),
ImmutableSetMultimap.of(
0,
new NestedAnnotationInfo(
Annotation.NULLABLE,
ImmutableList.of(
new NestedAnnotationInfo.TypePathEntry(TYPE_ARGUMENT, 1),
new NestedAnnotationInfo.TypePathEntry(WILDCARD_BOUND, 0)))))
// https://github.com/uber/NullAway/issues/1616
/*.put(
methodRef(
"java.util.Optional",
"orElseGet(java.util.function.Supplier<? extends T>)"),
ImmutableSetMultimap.of(
1,
0,
new NestedAnnotationInfo(
Annotation.NONNULL,
Annotation.NULLABLE,
ImmutableList.of(
new NestedAnnotationInfo.TypePathEntry(TYPE_ARGUMENT, 0)))));
new NestedAnnotationInfo.TypePathEntry(TYPE_ARGUMENT, 0),
new NestedAnnotationInfo.TypePathEntry(WILDCARD_BOUND, 0)))))*/
.build();

private static final ImmutableSet<String> NULLMARKED_CLASSES =
new ImmutableSet.Builder<String>()
.add("java.util.function.Function")
.add("java.util.Optional")
.add("java.util.concurrent.atomic.AtomicReference")
.add("java.util.concurrent.atomic.AtomicReferenceFieldUpdater")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.uber.nullaway.NullAwayTestsBase;
import com.uber.nullaway.generics.JSpecifyJavacConfig;
import java.util.Arrays;
import org.junit.Ignore;
import org.junit.Test;

public class JSpecifyLibraryModelsTests extends NullAwayTestsBase {
Expand Down Expand Up @@ -32,6 +33,124 @@ void testPositive() {
.doTest();
}

@Test
public void optionalMethods() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import java.util.Optional;
import java.util.function.Function;
import org.jspecify.annotations.*;

@NullMarked
class Test {
Optional<String> ofNullableAcceptsNullable(@Nullable String value) {
return Optional.ofNullable(value);
}

Optional<String> mapCanReturnNullable(Optional<String> value) {
Function<String, @Nullable String> mapper = unused -> null;
return value.map(mapper);
}

void equalsAcceptsNullable(Optional<String> value) {
value.equals(null);
}

void ofRejectsNull() {
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
Optional.of(null);
}

void orElseReturnsNullable(Optional<String> value) {
// BUG: Diagnostic contains: dereferenced expression 'value.orElse(null)' is @Nullable
value.orElse(null).hashCode();
}
}
""")
.doTest();
}

@Test
public void optionalMapWithLambdaArg() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import java.util.Optional;
import org.jspecify.annotations.*;

@NullMarked
class Test {
Optional<String> mapCanReturnNullable(Optional<String> value) {
return value.map(unused -> null);
}
}
""")
.doTest();
}

@Ignore("https://github.com/uber/NullAway/issues/1616")
@Test
public void optionalOrElseGet() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import java.util.Optional;
import java.util.function.Function;
import org.jspecify.annotations.*;

@NullMarked
class Test {
void orElseGetReturnsNullable(Optional<String> value) {
// BUG: Diagnostic contains: dereferenced expression 'value.orElseGet(() -> null)' is @Nullable
value.orElseGet(() -> null).hashCode();
}
}
""")
.doTest();
}

@Test
public void issue1611() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import java.util.Optional;
import org.jspecify.annotations.*;

@NullMarked
class Test {
static Optional<String> getKey(@Nullable String key) {
return Optional.ofNullable(key).or(() -> Optional.ofNullable(System.getenv("DEFAULT_KEY")));
}
}
""")
.doTest();
}

@Test
public void optionalOfNullableFilterIssue1597() {
makeHelper()
.addSourceLines(
"Repro.java",
"""
import java.util.Optional;
import org.jspecify.annotations.*;

@NullMarked
public record Repro(@Nullable String name) {
public Optional<String> getNameOpt() {
return Optional.ofNullable(name).filter(it -> true);
}
}
""")
.doTest();
}

private CompilationTestHelper makeHelper() {
return makeTestHelperWithArgs(
JSpecifyJavacConfig.withJSpecifyModeArgs(
Expand Down
Loading