1 /* 2 * Copyright (C) 2015 The Dagger Authors. 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 17 package dagger.functional.producers; 18 19 import com.google.common.collect.ImmutableSet; 20 import com.google.common.util.concurrent.Futures; 21 import com.google.common.util.concurrent.ListenableFuture; 22 import com.google.common.util.concurrent.SettableFuture; 23 import dagger.Lazy; 24 import dagger.multibindings.ElementsIntoSet; 25 import dagger.multibindings.IntoSet; 26 import dagger.producers.Produced; 27 import dagger.producers.Producer; 28 import dagger.producers.ProducerModule; 29 import dagger.producers.Produces; 30 import java.io.IOException; 31 import java.util.Set; 32 import javax.inject.Provider; 33 import javax.inject.Qualifier; 34 35 /** 36 * A module that contains various signatures of produces methods. This is not used in any 37 * components. 38 */ 39 @ProducerModule 40 final class SimpleProducerModule { 41 @Qualifier @interface Qual { value()42 int value(); 43 } 44 45 // Unique bindings. 46 47 @Produces 48 @Qual(-2) throwingProducer()49 static ListenableFuture<String> throwingProducer() { 50 throw new RuntimeException("monkey"); 51 } 52 53 @Produces 54 @Qual(-1) settableFutureStr(SettableFuture<String> future)55 static ListenableFuture<String> settableFutureStr(SettableFuture<String> future) { 56 return future; 57 } 58 59 @Produces 60 @Qual(0) str()61 static String str() { 62 return "str"; 63 } 64 65 @Produces 66 @Qual(1) futureStr()67 static ListenableFuture<String> futureStr() { 68 return Futures.immediateFuture("future str"); 69 } 70 71 @Produces 72 @Qual(2) strWithArg(@uppressWarnings"unused") int i)73 static String strWithArg(@SuppressWarnings("unused") int i) { 74 return "str with arg"; 75 } 76 77 @Produces 78 @Qual(3) futureStrWithArg(@uppressWarnings"unused") int i)79 static ListenableFuture<String> futureStrWithArg(@SuppressWarnings("unused") int i) { 80 return Futures.immediateFuture("future str with arg"); 81 } 82 83 @Produces 84 @Qual(4) 85 @SuppressWarnings("unused") // unthrown exception for test strThrowingException()86 static String strThrowingException() throws IOException { 87 return "str throwing exception"; 88 } 89 90 @Produces 91 @Qual(5) 92 @SuppressWarnings("unused") // unthrown exception for test futureStrThrowingException()93 static ListenableFuture<String> futureStrThrowingException() throws IOException { 94 return Futures.immediateFuture("future str throwing exception"); 95 } 96 97 @Produces 98 @Qual(6) 99 @SuppressWarnings("unused") // unthrown exception for test, unused parameter for test strWithArgThrowingException(int i)100 static String strWithArgThrowingException(int i) throws IOException { 101 return "str with arg throwing exception"; 102 } 103 104 @Produces 105 @Qual(7) 106 @SuppressWarnings("unused") // unthrown exception for test, unused parameter for test futureStrWithArgThrowingException(int i)107 static ListenableFuture<String> futureStrWithArgThrowingException(int i) throws IOException { 108 return Futures.immediateFuture("future str with arg throwing exception"); 109 } 110 111 @Produces 112 @Qual(8) strWithArgs( @uppressWarnings"unused") int i, @SuppressWarnings("unused") Produced<Double> b, @SuppressWarnings("unused") Producer<Object> c, @SuppressWarnings("unused") Provider<Boolean> d)113 static String strWithArgs( 114 @SuppressWarnings("unused") int i, 115 @SuppressWarnings("unused") Produced<Double> b, 116 @SuppressWarnings("unused") Producer<Object> c, 117 @SuppressWarnings("unused") Provider<Boolean> d) { 118 return "str with args"; 119 } 120 121 @Produces 122 @Qual(9) 123 @SuppressWarnings("unused") // unthrown exception for test, unused parameters for test strWithArgsThrowingException( int i, Produced<Double> b, Producer<Object> c, Provider<Boolean> d)124 static String strWithArgsThrowingException( 125 int i, Produced<Double> b, Producer<Object> c, Provider<Boolean> d) throws IOException { 126 return "str with args throwing exception"; 127 } 128 129 @Produces 130 @Qual(10) futureStrWithArgs( @uppressWarnings"unused") int i, @SuppressWarnings("unused") Produced<Double> b, @SuppressWarnings("unused") Producer<Object> c, @SuppressWarnings("unused") Provider<Boolean> d)131 static ListenableFuture<String> futureStrWithArgs( 132 @SuppressWarnings("unused") int i, 133 @SuppressWarnings("unused") Produced<Double> b, 134 @SuppressWarnings("unused") Producer<Object> c, 135 @SuppressWarnings("unused") Provider<Boolean> d) { 136 return Futures.immediateFuture("future str with args"); 137 } 138 139 @Produces 140 @Qual(11) 141 @SuppressWarnings("unused") // unthrown exception for test, unused parameter for test futureStrWithArgsThrowingException( int i, Produced<Double> b, Producer<Object> c, Provider<Boolean> d)142 static ListenableFuture<String> futureStrWithArgsThrowingException( 143 int i, Produced<Double> b, Producer<Object> c, Provider<Boolean> d) throws IOException { 144 return Futures.immediateFuture("str with args throwing exception"); 145 } 146 147 @Produces 148 @Qual(12) strWithFrameworkTypeArgs( @uppressWarnings"unused") @ual1) int i, @SuppressWarnings("unused") @Qual(1) Provider<Integer> iProvider, @SuppressWarnings("unused") @Qual(1) Lazy<Integer> iLazy, @SuppressWarnings("unused") @Qual(2) int j, @SuppressWarnings("unused") @Qual(2) Produced<Integer> jProduced, @SuppressWarnings("unused") @Qual(2) Producer<Integer> jProducer, @SuppressWarnings("unused") @Qual(3) Produced<Integer> kProduced, @SuppressWarnings("unused") @Qual(3) Producer<Integer> kProducer)149 static String strWithFrameworkTypeArgs( 150 @SuppressWarnings("unused") @Qual(1) int i, 151 @SuppressWarnings("unused") @Qual(1) Provider<Integer> iProvider, 152 @SuppressWarnings("unused") @Qual(1) Lazy<Integer> iLazy, 153 @SuppressWarnings("unused") @Qual(2) int j, 154 @SuppressWarnings("unused") @Qual(2) Produced<Integer> jProduced, 155 @SuppressWarnings("unused") @Qual(2) Producer<Integer> jProducer, 156 @SuppressWarnings("unused") @Qual(3) Produced<Integer> kProduced, 157 @SuppressWarnings("unused") @Qual(3) Producer<Integer> kProducer) { 158 return "str with framework type args"; 159 } 160 161 // Set bindings. 162 163 @Produces 164 @IntoSet setOfStrElement()165 static String setOfStrElement() { 166 return "set of str element"; 167 } 168 169 @Produces 170 @IntoSet 171 @SuppressWarnings("unused") // unthrown exception for test setOfStrElementThrowingException()172 static String setOfStrElementThrowingException() throws IOException { 173 return "set of str element throwing exception"; 174 } 175 176 @Produces 177 @IntoSet setOfStrFutureElement()178 static ListenableFuture<String> setOfStrFutureElement() { 179 return Futures.immediateFuture("set of str element"); 180 } 181 182 @Produces 183 @IntoSet 184 @SuppressWarnings("unused") // unthrown exception for test setOfStrFutureElementThrowingException()185 static ListenableFuture<String> setOfStrFutureElementThrowingException() throws IOException { 186 return Futures.immediateFuture("set of str element throwing exception"); 187 } 188 189 @Produces 190 @IntoSet setOfStrElementWithArg(@uppressWarnings"unused") int i)191 static String setOfStrElementWithArg(@SuppressWarnings("unused") int i) { 192 return "set of str element with arg"; 193 } 194 195 @Produces 196 @IntoSet 197 @SuppressWarnings("unused") // unthrown exception for test, unused parameter for test setOfStrElementWithArgThrowingException(int i)198 static String setOfStrElementWithArgThrowingException(int i) throws IOException { 199 return "set of str element with arg throwing exception"; 200 } 201 202 @Produces 203 @IntoSet setOfStrFutureElementWithArg(@uppressWarnings"unused") int i)204 static ListenableFuture<String> setOfStrFutureElementWithArg(@SuppressWarnings("unused") int i) { 205 return Futures.immediateFuture("set of str element with arg"); 206 } 207 208 @Produces 209 @IntoSet 210 @SuppressWarnings("unused") // unthrown exception for test, unused parameter for test setOfStrFutureElementWithArgThrowingException(int i)211 static ListenableFuture<String> setOfStrFutureElementWithArgThrowingException(int i) 212 throws IOException { 213 return Futures.immediateFuture("set of str element with arg throwing exception"); 214 } 215 216 @Produces 217 @ElementsIntoSet setOfStrValues()218 static Set<String> setOfStrValues() { 219 return ImmutableSet.of("set of str 1", "set of str 2"); 220 } 221 222 @Produces 223 @ElementsIntoSet 224 @SuppressWarnings("unused") // unthrown exception for test setOfStrValuesThrowingException()225 static Set<String> setOfStrValuesThrowingException() throws IOException { 226 return ImmutableSet.of("set of str 1", "set of str 2 throwing exception"); 227 } 228 229 @Produces 230 @ElementsIntoSet setOfStrFutureValues()231 static ListenableFuture<Set<String>> setOfStrFutureValues() { 232 return Futures.<Set<String>>immediateFuture(ImmutableSet.of("set of str 1", "set of str 2")); 233 } 234 235 @Produces 236 @ElementsIntoSet 237 @SuppressWarnings("unused") // unthrown exception for test setOfStrFutureValuesThrowingException()238 static ListenableFuture<Set<String>> setOfStrFutureValuesThrowingException() throws IOException { 239 return Futures.<Set<String>>immediateFuture( 240 ImmutableSet.of("set of str 1", "set of str 2 throwing exception")); 241 } 242 243 @Produces 244 @ElementsIntoSet setOfStrValuesWithArg(@uppressWarnings"unused") int i)245 static Set<String> setOfStrValuesWithArg(@SuppressWarnings("unused") int i) { 246 return ImmutableSet.of("set of str with arg 1", "set of str with arg 2"); 247 } 248 249 @Produces 250 @ElementsIntoSet 251 @SuppressWarnings("unused") // unthrown exception for test, unused parameter for test setOfStrValuesWithArgThrowingException(int i)252 static Set<String> setOfStrValuesWithArgThrowingException(int i) throws IOException { 253 return ImmutableSet.of("set of str with arg 1", "set of str with arg 2 throwing exception"); 254 } 255 256 @Produces 257 @ElementsIntoSet setOfStrFutureValuesWithArg( @uppressWarnings"unused") int i)258 static ListenableFuture<Set<String>> setOfStrFutureValuesWithArg( 259 @SuppressWarnings("unused") int i) { 260 return Futures.<Set<String>>immediateFuture( 261 ImmutableSet.of("set of str with arg 1", "set of str with arg 2")); 262 } 263 264 @Produces 265 @ElementsIntoSet 266 @SuppressWarnings("unused") // unthrown exception for test, unused parameter for test setOfStrFutureValuesWithArgThrowingException(int i)267 static ListenableFuture<Set<String>> setOfStrFutureValuesWithArgThrowingException(int i) 268 throws IOException { 269 return Futures.<Set<String>>immediateFuture( 270 ImmutableSet.of("set of str with arg 1", "set of str with arg 2 throwing exception")); 271 } 272 273 /** 274 * A binding method that might result in a generated factory with conflicting field and parameter 275 * names. 276 */ 277 @Produces object(int foo, Provider<String> fooProvider)278 static Object object(int foo, Provider<String> fooProvider) { 279 return foo + fooProvider.get(); 280 } 281 } 282