Skip to content

Commit

Permalink
Add positive case and make sure it reports an error.
Browse files Browse the repository at this point in the history
  • Loading branch information
lazaroclapp committed Aug 16, 2023
1 parent dfb9279 commit 364adc6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
6 changes: 3 additions & 3 deletions nullaway/src/main/java/com/uber/nullaway/LibraryModels.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public interface LibraryModels {
*
* <p>This allows users to define filter/map/other methods for APIs which behave similarly to Java
* 8 streams or ReactiveX streams, so that NullAway is able to understand nullability invariants
* across stream API calls. See {@see com.uber.nullaway.handlers.stream.StreamModelBuilder} for
* details on how to construct these {@see com.uber.nullaway.handlers.stream.StreamTypeRecord}
* specs. A full example is available at {@see
* across stream API calls. See {@link com.uber.nullaway.handlers.stream.StreamModelBuilder} for
* details on how to construct these {@link com.uber.nullaway.handlers.stream.StreamTypeRecord}
* specs. A full example is available at {@link
* com.uber.nullaway.testlibrarymodels.TestLibraryModels}.
*
* @return A list of StreamTypeRecord specs (usually generated using StreamModelBuilder).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ private CustomStream<Integer> filterThenMapLambdasCustomStream(CustomStream<Stri
return stream.filter(s -> s != null).map(s -> s.length());
}

private CustomStream<Integer> filterThenMapNullableContainerLambdasCustomStream(
CustomStream<NullableContainer<String>> stream) {
return stream
.filter(c -> c.get() != null)
.map(c -> c.get().length());
}

private CustomStream<Integer> filterThenMapMethodRefsCustomStream(
CustomStream<NullableContainer<String>> stream) {
return stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.uber.nullaway.testdata;

import com.google.common.base.Preconditions;
import com.uber.nullaway.testdata.unannotated.CustomStreamWithoutModel;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.DoubleStream;
Expand Down Expand Up @@ -153,6 +154,28 @@ private void forEachOrdered(Stream<NullableContainer<String>> stream) {
stream.forEachOrdered(s -> System.out.println(s.get().length()));
}

// CustomStreamWithoutModel is NOT modeled in TestLibraryModels
private CustomStreamWithoutModel<Integer> filterThenMapLambdasCustomStream(CustomStreamWithoutModel<String> stream) {
// Safe because generic is String, not @Nullable String
return stream.filter(s -> s != null).map(s -> s.length());
}

private CustomStreamWithoutModel<Integer> filterThenMapNullableContainerLambdasCustomStream(
CustomStreamWithoutModel<NullableContainer<String>> stream) {
return stream
.filter(c -> c.get() != null)
// BUG: Diagnostic contains: dereferenced expression
.map(c -> c.get().length());
}

private CustomStreamWithoutModel<Integer> filterThenMapMethodRefsCustomStream(
CustomStreamWithoutModel<NullableContainer<String>> stream) {
return stream
.filter(c -> c.get() != null && perhaps())
.map(NullableContainer::get) // CSWoM<NullableContainer<String>> -> CSWoM<@Nullable String>
.map(String::length); // Should be an error with proper generics support!
}

private static class CheckNonfinalBeforeStream<T> {
@Nullable private T ref;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2023 Uber Technologies, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.uber.nullaway.testdata.unannotated;

import java.util.function.Function;
import java.util.function.Predicate;

/**
* A copy of {@link CustomStream} but for which our test library models have no spec/model, to test that errors still
* get reported in the absence of a model.
*/
public class CustomStreamWithoutModel<T> {

private CustomStreamWithoutModel() {}

public CustomStreamWithoutModel<T> filter(Predicate<? super T> predicate) {
throw new UnsupportedOperationException();
}

public <R> CustomStreamWithoutModel<R> map(Function<? super T, ? extends R> mapper) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public ImmutableList<StreamTypeRecord> customStreamNullabilitySpecs() {
// Identical to the default model for java.util.stream.Stream, but with the original type
// renamed
return StreamModelBuilder.start()
.addStreamTypeFromName("com.uber.unannotated.CustomStreamA")
.addStreamTypeFromName("com.uber.nullaway.testdata.unannotated.CustomStream")
.withFilterMethodFromSignature("filter(java.util.function.Predicate<? super T>)")
.withMapMethodFromSignature(
"<R>map(java.util.function.Function<? super T,? extends R>)",
Expand Down

0 comments on commit 364adc6

Please sign in to comment.