• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.javapoet;
18 
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.squareup.javapoet.ClassName;
22 import com.squareup.javapoet.ParameterizedTypeName;
23 import com.squareup.javapoet.TypeName;
24 import dagger.Lazy;
25 import dagger.MembersInjector;
26 import dagger.internal.DoubleCheck;
27 import dagger.internal.Factory;
28 import dagger.internal.InjectedFieldSignature;
29 import dagger.internal.InstanceFactory;
30 import dagger.internal.MapFactory;
31 import dagger.internal.MapProviderFactory;
32 import dagger.internal.MembersInjectors;
33 import dagger.internal.ProviderOfLazy;
34 import dagger.internal.SetFactory;
35 import dagger.internal.SingleCheck;
36 import dagger.producers.Produced;
37 import dagger.producers.Producer;
38 import dagger.producers.ProducerModule;
39 import dagger.producers.internal.AbstractProducer;
40 import dagger.producers.internal.DependencyMethodProducer;
41 import dagger.producers.internal.MapOfProducedProducer;
42 import dagger.producers.internal.MapOfProducerProducer;
43 import dagger.producers.internal.MapProducer;
44 import dagger.producers.internal.Producers;
45 import dagger.producers.internal.SetOfProducedProducer;
46 import dagger.producers.internal.SetProducer;
47 import dagger.producers.monitoring.ProducerToken;
48 import dagger.producers.monitoring.ProductionComponentMonitor;
49 import java.util.List;
50 import java.util.Set;
51 import javax.inject.Provider;
52 
53 /** Common names and convenience methods for JavaPoet {@link TypeName} usage. */
54 public final class TypeNames {
55 
56   public static final ClassName ABSTRACT_PRODUCER = ClassName.get(AbstractProducer.class);
57   public static final ClassName DEPENDENCY_METHOD_PRODUCER =
58       ClassName.get(DependencyMethodProducer.class);
59   public static final ClassName DOUBLE_CHECK = ClassName.get(DoubleCheck.class);
60   public static final ClassName FACTORY = ClassName.get(Factory.class);
61   public static final ClassName FUTURES = ClassName.get(Futures.class);
62   public static final ClassName INJECTED_FIELD_SIGNATURE =
63       ClassName.get(InjectedFieldSignature.class);
64   public static final ClassName INSTANCE_FACTORY = ClassName.get(InstanceFactory.class);
65   public static final ClassName LAZY = ClassName.get(Lazy.class);
66   public static final ClassName LIST = ClassName.get(List.class);
67   public static final ClassName LISTENABLE_FUTURE = ClassName.get(ListenableFuture.class);
68   public static final ClassName MAP_FACTORY = ClassName.get(MapFactory.class);
69   public static final ClassName MAP_OF_PRODUCED_PRODUCER =
70       ClassName.get(MapOfProducedProducer.class);
71   public static final ClassName MAP_OF_PRODUCER_PRODUCER =
72       ClassName.get(MapOfProducerProducer.class);
73   public static final ClassName MAP_PRODUCER = ClassName.get(MapProducer.class);
74   public static final ClassName MAP_PROVIDER_FACTORY = ClassName.get(MapProviderFactory.class);
75   public static final ClassName MEMBERS_INJECTOR = ClassName.get(MembersInjector.class);
76   public static final ClassName MEMBERS_INJECTORS = ClassName.get(MembersInjectors.class);
77   public static final ClassName PRODUCER_TOKEN = ClassName.get(ProducerToken.class);
78   public static final ClassName PRODUCED = ClassName.get(Produced.class);
79   public static final ClassName PRODUCER = ClassName.get(Producer.class);
80   public static final ClassName PRODUCERS = ClassName.get(Producers.class);
81   public static final ClassName PRODUCER_MODULE = ClassName.get(ProducerModule.class);
82   public static final ClassName PRODUCTION_COMPONENT_MONITOR_FACTORY =
83       ClassName.get(ProductionComponentMonitor.Factory.class);
84   public static final ClassName PROVIDER = ClassName.get(Provider.class);
85   public static final ClassName PROVIDER_OF_LAZY = ClassName.get(ProviderOfLazy.class);
86   public static final ClassName SET = ClassName.get(Set.class);
87   public static final ClassName SET_FACTORY = ClassName.get(SetFactory.class);
88   public static final ClassName SET_OF_PRODUCED_PRODUCER =
89       ClassName.get(SetOfProducedProducer.class);
90   public static final ClassName SET_PRODUCER = ClassName.get(SetProducer.class);
91   public static final ClassName SINGLE_CHECK = ClassName.get(SingleCheck.class);
92 
93   /**
94    * {@link TypeName#VOID} is lowercase-v {@code void} whereas this represents the class, {@link
95    * Void}.
96    */
97   public static final ClassName VOID_CLASS = ClassName.get(Void.class);
98 
abstractProducerOf(TypeName typeName)99   public static ParameterizedTypeName abstractProducerOf(TypeName typeName) {
100     return ParameterizedTypeName.get(ABSTRACT_PRODUCER, typeName);
101   }
102 
factoryOf(TypeName factoryType)103   public static ParameterizedTypeName factoryOf(TypeName factoryType) {
104     return ParameterizedTypeName.get(FACTORY, factoryType);
105   }
106 
lazyOf(TypeName typeName)107   public static ParameterizedTypeName lazyOf(TypeName typeName) {
108     return ParameterizedTypeName.get(LAZY, typeName);
109   }
110 
listOf(TypeName typeName)111   public static ParameterizedTypeName listOf(TypeName typeName) {
112     return ParameterizedTypeName.get(LIST, typeName);
113   }
114 
listenableFutureOf(TypeName typeName)115   public static ParameterizedTypeName listenableFutureOf(TypeName typeName) {
116     return ParameterizedTypeName.get(LISTENABLE_FUTURE, typeName);
117   }
118 
membersInjectorOf(TypeName membersInjectorType)119   public static ParameterizedTypeName membersInjectorOf(TypeName membersInjectorType) {
120     return ParameterizedTypeName.get(MEMBERS_INJECTOR, membersInjectorType);
121   }
122 
producedOf(TypeName typeName)123   public static ParameterizedTypeName producedOf(TypeName typeName) {
124     return ParameterizedTypeName.get(PRODUCED, typeName);
125   }
126 
producerOf(TypeName typeName)127   public static ParameterizedTypeName producerOf(TypeName typeName) {
128     return ParameterizedTypeName.get(PRODUCER, typeName);
129   }
130 
dependencyMethodProducerOf(TypeName typeName)131   public static ParameterizedTypeName dependencyMethodProducerOf(TypeName typeName) {
132     return ParameterizedTypeName.get(DEPENDENCY_METHOD_PRODUCER, typeName);
133   }
134 
providerOf(TypeName typeName)135   public static ParameterizedTypeName providerOf(TypeName typeName) {
136     return ParameterizedTypeName.get(PROVIDER, typeName);
137   }
138 
setOf(TypeName elementType)139   public static ParameterizedTypeName setOf(TypeName elementType) {
140     return ParameterizedTypeName.get(SET, elementType);
141   }
142 
143   /**
144    * Returns the {@link TypeName} for the raw type of the given type name. If the argument isn't a
145    * parameterized type, it returns the argument unchanged.
146    */
rawTypeName(TypeName typeName)147   public static TypeName rawTypeName(TypeName typeName) {
148     return (typeName instanceof ParameterizedTypeName)
149         ? ((ParameterizedTypeName) typeName).rawType
150         : typeName;
151   }
152 
TypeNames()153   private TypeNames() {}
154 }
155