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