• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.langmodel.Accessibility.accessibleTypeName;
21 import static dagger.internal.codegen.xprocessing.XProcessingEnvs.isPreJava8SourceVersion;
22 import static dagger.internal.codegen.xprocessing.XProcessingEnvs.wrapType;
23 
24 import androidx.room.compiler.processing.XProcessingEnv;
25 import androidx.room.compiler.processing.XType;
26 import com.google.common.util.concurrent.Futures;
27 import com.squareup.javapoet.ClassName;
28 import com.squareup.javapoet.CodeBlock;
29 import dagger.assisted.Assisted;
30 import dagger.assisted.AssistedFactory;
31 import dagger.assisted.AssistedInject;
32 import dagger.internal.codegen.javapoet.Expression;
33 import dagger.internal.codegen.javapoet.TypeNames;
34 
35 final class ImmediateFutureRequestRepresentation extends RequestRepresentation {
36   private final RequestRepresentation instanceRequestRepresentation;
37   private final XType type;
38   private final XProcessingEnv processingEnv;
39 
40   @AssistedInject
ImmediateFutureRequestRepresentation( @ssisted RequestRepresentation instanceRequestRepresentation, @Assisted XType type, XProcessingEnv processingEnv)41   ImmediateFutureRequestRepresentation(
42       @Assisted RequestRepresentation instanceRequestRepresentation,
43       @Assisted XType type,
44       XProcessingEnv processingEnv) {
45     this.instanceRequestRepresentation = checkNotNull(instanceRequestRepresentation);
46     this.type = checkNotNull(type);
47     this.processingEnv = processingEnv;
48   }
49 
50   @Override
getDependencyExpression(ClassName requestingClass)51   Expression getDependencyExpression(ClassName requestingClass) {
52     return Expression.create(
53         wrapType(TypeNames.LISTENABLE_FUTURE, type, processingEnv),
54         CodeBlock.of("$T.immediateFuture($L)", Futures.class, instanceExpression(requestingClass)));
55   }
56 
instanceExpression(ClassName requestingClass)57   private CodeBlock instanceExpression(ClassName requestingClass) {
58     Expression expression = instanceRequestRepresentation.getDependencyExpression(requestingClass);
59     if (isPreJava8SourceVersion(processingEnv)) {
60       // Java 7 type inference is not as strong as in Java 8, and therefore some generated code must
61       // cast.
62       //
63       // For example, javac7 cannot detect that Futures.immediateFuture(ImmutableSet.of("T"))
64       // can safely be assigned to ListenableFuture<Set<T>>.
65       if (!expression.type().isSameType(type)) {
66         return CodeBlock.of(
67             "($T) $L",
68             accessibleTypeName(type, requestingClass, processingEnv),
69             expression.codeBlock());
70       }
71     }
72     return expression.codeBlock();
73   }
74 
75   @AssistedFactory
76   static interface Factory {
create( RequestRepresentation instanceRequestRepresentation, XType type)77     ImmediateFutureRequestRepresentation create(
78         RequestRepresentation instanceRequestRepresentation, XType type);
79   }
80 }
81