1 /* 2 * Copyright (C) 2017. Uber Technologies 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.uber.nullaway.testlibrarymodels; 17 18 import static com.uber.nullaway.LibraryModels.FieldRef.fieldRef; 19 import static com.uber.nullaway.LibraryModels.MethodRef.methodRef; 20 21 import com.google.auto.service.AutoService; 22 import com.google.common.collect.ImmutableList; 23 import com.google.common.collect.ImmutableSet; 24 import com.google.common.collect.ImmutableSetMultimap; 25 import com.uber.nullaway.LibraryModels; 26 import com.uber.nullaway.handlers.stream.StreamModelBuilder; 27 import com.uber.nullaway.handlers.stream.StreamTypeRecord; 28 29 @AutoService(LibraryModels.class) 30 public class TestLibraryModels implements LibraryModels { 31 32 @Override failIfNullParameters()33 public ImmutableSetMultimap<MethodRef, Integer> failIfNullParameters() { 34 return ImmutableSetMultimap.of(); 35 } 36 37 @Override explicitlyNullableParameters()38 public ImmutableSetMultimap<MethodRef, Integer> explicitlyNullableParameters() { 39 return ImmutableSetMultimap.of(); 40 } 41 42 @Override nonNullParameters()43 public ImmutableSetMultimap<MethodRef, Integer> nonNullParameters() { 44 return new ImmutableSetMultimap.Builder<MethodRef, Integer>() 45 .put( 46 methodRef( 47 "com.uber.lib.unannotated.RestrictivelyAnnotatedFIWithModelOverride", 48 "apply(java.lang.Object)"), 49 0) 50 .build(); 51 } 52 53 @Override nullImpliesTrueParameters()54 public ImmutableSetMultimap<MethodRef, Integer> nullImpliesTrueParameters() { 55 return ImmutableSetMultimap.of(); 56 } 57 58 @Override nullImpliesFalseParameters()59 public ImmutableSetMultimap<MethodRef, Integer> nullImpliesFalseParameters() { 60 return ImmutableSetMultimap.of( 61 methodRef("com.uber.lib.unannotated.UnannotatedWithModels", "isNonNull(java.lang.Object)"), 62 0); 63 } 64 65 @Override nullImpliesNullParameters()66 public ImmutableSetMultimap<MethodRef, Integer> nullImpliesNullParameters() { 67 return ImmutableSetMultimap.of(); 68 } 69 70 @Override nullableReturns()71 public ImmutableSet<MethodRef> nullableReturns() { 72 return ImmutableSet.of( 73 methodRef("com.uber.AnnotatedWithModels", "returnsNullFromModel()"), 74 methodRef("com.uber.lib.unannotated.UnannotatedWithModels", "returnsNullUnannotated()"), 75 methodRef("com.uber.lib.unannotated.UnannotatedWithModels", "returnsNullUnannotated2()")); 76 } 77 78 @Override nonNullReturns()79 public ImmutableSet<MethodRef> nonNullReturns() { 80 return ImmutableSet.of(); 81 } 82 83 @Override castToNonNullMethods()84 public ImmutableSetMultimap<MethodRef, Integer> castToNonNullMethods() { 85 return ImmutableSetMultimap.<MethodRef, Integer>builder() 86 .put( 87 methodRef("com.uber.nullaway.testdata.Util", "<T>castToNonNull(T,java.lang.String)"), 0) 88 .put( 89 methodRef( 90 "com.uber.nullaway.testdata.Util", "<T>castToNonNull(java.lang.String,T,int)"), 91 1) 92 .build(); 93 } 94 95 @Override customStreamNullabilitySpecs()96 public ImmutableList<StreamTypeRecord> customStreamNullabilitySpecs() { 97 // Identical to the default model for java.util.stream.Stream, but with the original type 98 // renamed 99 return StreamModelBuilder.start() 100 .addStreamTypeFromName("com.uber.nullaway.testdata.unannotated.CustomStream") 101 .withFilterMethodFromSignature("filter(java.util.function.Predicate<? super T>)") 102 .withMapMethodFromSignature( 103 "<R>map(java.util.function.Function<? super T,? extends R>)", 104 "apply", 105 ImmutableSet.of(0)) 106 .withMapMethodFromSignature( 107 "mapToInt(java.util.function.ToIntFunction<? super T>)", 108 "applyAsInt", 109 ImmutableSet.of(0)) 110 .withMapMethodFromSignature( 111 "mapToLong(java.util.function.ToLongFunction<? super T>)", 112 "applyAsLong", 113 ImmutableSet.of(0)) 114 .withMapMethodFromSignature( 115 "mapToDouble(java.util.function.ToDoubleFunction<? super T>)", 116 "applyAsDouble", 117 ImmutableSet.of(0)) 118 .withMapMethodFromSignature( 119 "forEach(java.util.function.Consumer<? super T>)", "accept", ImmutableSet.of(0)) 120 .withMapMethodFromSignature( 121 "forEachOrdered(java.util.function.Consumer<? super T>)", "accept", ImmutableSet.of(0)) 122 .withMapMethodAllFromName("flatMap", "apply", ImmutableSet.of(0)) 123 .withPassthroughMethodFromSignature("distinct()") 124 .end(); 125 } 126 127 @Override nullableFields()128 public ImmutableSet<FieldRef> nullableFields() { 129 return ImmutableSet.<FieldRef>builder() 130 .add( 131 fieldRef("com.uber.lib.unannotated.UnannotatedWithModels", "nullableFieldUnannotated1"), 132 fieldRef("com.uber.lib.unannotated.UnannotatedWithModels", "nullableFieldUnannotated2")) 133 .build(); 134 } 135 } 136