1 /* 2 * Copyright (C) 2018 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.internal.codegen.writing; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 import static dagger.internal.codegen.binding.BindingRequest.bindingRequest; 21 22 import com.google.common.util.concurrent.Futures; 23 import com.google.common.util.concurrent.ListenableFuture; 24 import com.squareup.javapoet.ClassName; 25 import com.squareup.javapoet.CodeBlock; 26 import dagger.internal.codegen.javapoet.Expression; 27 import dagger.internal.codegen.langmodel.DaggerTypes; 28 import dagger.model.Key; 29 import dagger.model.RequestKind; 30 import javax.lang.model.SourceVersion; 31 32 final class ImmediateFutureBindingExpression extends BindingExpression { 33 private final Key key; 34 private final ComponentBindingExpressions componentBindingExpressions; 35 private final DaggerTypes types; 36 private final SourceVersion sourceVersion; 37 ImmediateFutureBindingExpression( Key key, ComponentBindingExpressions componentBindingExpressions, DaggerTypes types, SourceVersion sourceVersion)38 ImmediateFutureBindingExpression( 39 Key key, 40 ComponentBindingExpressions componentBindingExpressions, 41 DaggerTypes types, 42 SourceVersion sourceVersion) { 43 this.key = key; 44 this.componentBindingExpressions = checkNotNull(componentBindingExpressions); 45 this.types = checkNotNull(types); 46 this.sourceVersion = checkNotNull(sourceVersion); 47 } 48 49 @Override getDependencyExpression(ClassName requestingClass)50 Expression getDependencyExpression(ClassName requestingClass) { 51 return Expression.create( 52 types.wrapType(key.type(), ListenableFuture.class), 53 CodeBlock.of("$T.immediateFuture($L)", Futures.class, instanceExpression(requestingClass))); 54 } 55 instanceExpression(ClassName requestingClass)56 private CodeBlock instanceExpression(ClassName requestingClass) { 57 Expression expression = 58 componentBindingExpressions.getDependencyExpression( 59 bindingRequest(key, RequestKind.INSTANCE), requestingClass); 60 if (sourceVersion.compareTo(SourceVersion.RELEASE_7) <= 0) { 61 // Java 7 type inference is not as strong as in Java 8, and therefore some generated code must 62 // cast. 63 // 64 // For example, javac7 cannot detect that Futures.immediateFuture(ImmutableSet.of("T")) 65 // can safely be assigned to ListenableFuture<Set<T>>. 66 if (!types.isSameType(expression.type(), key.type())) { 67 return CodeBlock.of( 68 "($T) $L", types.accessibleType(key.type(), requestingClass), expression.codeBlock()); 69 } 70 } 71 return expression.codeBlock(); 72 } 73 } 74