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 20 import com.squareup.javapoet.ClassName; 21 import com.squareup.javapoet.ParameterizedTypeName; 22 import com.squareup.javapoet.TypeName; 23 import javax.lang.model.type.TypeMirror; 24 25 /** Common names and convenience methods for JavaPoet {@link TypeName} usage. */ 26 public final class TypeNames { 27 28 // Dagger Core classnames 29 public static final ClassName ASSISTED = ClassName.get("dagger.assisted", "Assisted"); 30 public static final ClassName ASSISTED_FACTORY = 31 ClassName.get("dagger.assisted", "AssistedFactory"); 32 public static final ClassName ASSISTED_INJECT = 33 ClassName.get("dagger.assisted", "AssistedInject"); 34 public static final ClassName BINDS = ClassName.get("dagger", "Binds"); 35 public static final ClassName BINDS_INSTANCE = ClassName.get("dagger", "BindsInstance"); 36 public static final ClassName BINDS_OPTIONAL_OF = ClassName.get("dagger", "BindsOptionalOf"); 37 public static final ClassName COMPONENT = ClassName.get("dagger", "Component"); 38 public static final ClassName COMPONENT_BUILDER = COMPONENT.nestedClass("Builder"); 39 public static final ClassName COMPONENT_FACTORY = COMPONENT.nestedClass("Factory"); 40 public static final ClassName DAGGER_PROCESSING_OPTIONS = 41 ClassName.get("dagger", "DaggerProcessingOptions"); 42 public static final ClassName ELEMENTS_INTO_SET = 43 ClassName.get("dagger.multibindings", "ElementsIntoSet"); 44 public static final ClassName INTO_MAP = ClassName.get("dagger.multibindings", "IntoMap"); 45 public static final ClassName INTO_SET = ClassName.get("dagger.multibindings", "IntoSet"); 46 public static final ClassName MAP_KEY = ClassName.get("dagger", "MapKey"); 47 public static final ClassName MODULE = ClassName.get("dagger", "Module"); 48 public static final ClassName MULTIBINDS = ClassName.get("dagger.multibindings", "Multibinds"); 49 public static final ClassName PROVIDES = ClassName.get("dagger", "Provides"); 50 public static final ClassName REUSABLE = ClassName.get("dagger", "Reusable"); 51 public static final ClassName SUBCOMPONENT = ClassName.get("dagger", "Subcomponent"); 52 public static final ClassName SUBCOMPONENT_BUILDER = SUBCOMPONENT.nestedClass("Builder"); 53 public static final ClassName SUBCOMPONENT_FACTORY = SUBCOMPONENT.nestedClass("Factory"); 54 55 // Dagger Internal classnames 56 public static final ClassName DELEGATE_FACTORY = 57 ClassName.get("dagger.internal", "DelegateFactory"); 58 public static final ClassName DOUBLE_CHECK = ClassName.get("dagger.internal", "DoubleCheck"); 59 public static final ClassName FACTORY = ClassName.get("dagger.internal", "Factory"); 60 public static final ClassName INJECTED_FIELD_SIGNATURE = 61 ClassName.get("dagger.internal", "InjectedFieldSignature"); 62 public static final ClassName INSTANCE_FACTORY = 63 ClassName.get("dagger.internal", "InstanceFactory"); 64 public static final ClassName MAP_FACTORY = ClassName.get("dagger.internal", "MapFactory"); 65 public static final ClassName MAP_PROVIDER_FACTORY = 66 ClassName.get("dagger.internal", "MapProviderFactory"); 67 public static final ClassName MEMBERS_INJECTOR = ClassName.get("dagger", "MembersInjector"); 68 public static final ClassName MEMBERS_INJECTORS = 69 ClassName.get("dagger.internal", "MembersInjectors"); 70 public static final ClassName PROVIDER = ClassName.get("javax.inject", "Provider"); 71 public static final ClassName PROVIDER_OF_LAZY = 72 ClassName.get("dagger.internal", "ProviderOfLazy"); 73 public static final ClassName SCOPE_METADATA = ClassName.get("dagger.internal", "ScopeMetadata"); 74 public static final ClassName QUALIFIER_METADATA = 75 ClassName.get("dagger.internal", "QualifierMetadata"); 76 public static final ClassName SET_FACTORY = ClassName.get("dagger.internal", "SetFactory"); 77 public static final ClassName SINGLE_CHECK = ClassName.get("dagger.internal", "SingleCheck"); 78 public static final ClassName LAZY = ClassName.get("dagger", "Lazy"); 79 80 // Dagger Producers classnames 81 public static final ClassName ABSTRACT_PRODUCER = 82 ClassName.get("dagger.producers.internal", "AbstractProducer"); 83 public static final ClassName CANCELLATION_LISTENER = 84 ClassName.get("dagger.producers.internal", "CancellationListener"); 85 public static final ClassName CANCELLATION_POLICY = 86 ClassName.get("dagger.producers", "CancellationPolicy"); 87 public static final ClassName DELEGATE_PRODUCER = 88 ClassName.get("dagger.producers.internal", "DelegateProducer"); 89 public static final ClassName DEPENDENCY_METHOD_PRODUCER = 90 ClassName.get("dagger.producers.internal", "DependencyMethodProducer"); 91 public static final ClassName MAP_OF_PRODUCED_PRODUCER = 92 ClassName.get("dagger.producers.internal", "MapOfProducedProducer"); 93 public static final ClassName MAP_OF_PRODUCER_PRODUCER = 94 ClassName.get("dagger.producers.internal", "MapOfProducerProducer"); 95 public static final ClassName MAP_PRODUCER = 96 ClassName.get("dagger.producers.internal", "MapProducer"); 97 public static final ClassName MONITORS = 98 ClassName.get("dagger.producers.monitoring.internal", "Monitors"); 99 public static final ClassName PRODUCED = ClassName.get("dagger.producers", "Produced"); 100 public static final ClassName PRODUCER = ClassName.get("dagger.producers", "Producer"); 101 public static final ClassName PRODUCERS = ClassName.get("dagger.producers.internal", "Producers"); 102 public static final ClassName PRODUCER_MODULE = 103 ClassName.get("dagger.producers", "ProducerModule"); 104 public static final ClassName PRODUCES = ClassName.get("dagger.producers", "Produces"); 105 public static final ClassName PRODUCTION = ClassName.get("dagger.producers", "Production"); 106 public static final ClassName PRODUCTION_COMPONENT = 107 ClassName.get("dagger.producers", "ProductionComponent"); 108 public static final ClassName PRODUCTION_COMPONENT_BUILDER = 109 PRODUCTION_COMPONENT.nestedClass("Builder"); 110 public static final ClassName PRODUCTION_COMPONENT_FACTORY = 111 PRODUCTION_COMPONENT.nestedClass("Factory"); 112 public static final ClassName PRODUCTION_EXECTUTOR_MODULE = 113 ClassName.get("dagger.producers.internal", "ProductionExecutorModule"); 114 public static final ClassName PRODUCTION_IMPLEMENTATION = 115 ClassName.get("dagger.producers.internal", "ProductionImplementation"); 116 public static final ClassName PRODUCTION_SUBCOMPONENT = 117 ClassName.get("dagger.producers", "ProductionSubcomponent"); 118 public static final ClassName PRODUCTION_SUBCOMPONENT_BUILDER = 119 PRODUCTION_SUBCOMPONENT.nestedClass("Builder"); 120 public static final ClassName PRODUCTION_SUBCOMPONENT_FACTORY = 121 PRODUCTION_SUBCOMPONENT.nestedClass("Factory"); 122 public static final ClassName PRODUCER_TOKEN = 123 ClassName.get("dagger.producers.monitoring", "ProducerToken"); 124 public static final ClassName PRODUCTION_COMPONENT_MONITOR = 125 ClassName.get("dagger.producers.monitoring", "ProductionComponentMonitor"); 126 public static final ClassName PRODUCTION_COMPONENT_MONITOR_FACTORY = 127 ClassName.get("dagger.producers.monitoring", "ProductionComponentMonitor", "Factory"); 128 public static final ClassName SET_OF_PRODUCED_PRODUCER = 129 ClassName.get("dagger.producers.internal", "SetOfProducedProducer"); 130 public static final ClassName SET_PRODUCER = 131 ClassName.get("dagger.producers.internal", "SetProducer"); 132 public static final ClassName PRODUCTION_SCOPE = 133 ClassName.get("dagger.producers", "ProductionScope"); 134 135 // Other classnames 136 public static final ClassName EXECUTOR = ClassName.get("java.util.concurrent", "Executor"); 137 public static final ClassName ERROR = ClassName.get("java.lang", "Error"); 138 public static final ClassName EXCEPTION = ClassName.get("java.lang", "Exception"); 139 public static final ClassName RUNTIME_EXCEPTION = ClassName.get("java.lang", "RuntimeException"); 140 public static final ClassName MAP = ClassName.get("java.util", "Map"); 141 public static final ClassName IMMUTABLE_MAP = 142 ClassName.get("com.google.common.collect", "ImmutableMap"); 143 public static final ClassName SINGLETON = ClassName.get("jakarta.inject", "Singleton"); 144 public static final ClassName SINGLETON_JAVAX = ClassName.get("javax.inject", "Singleton"); 145 public static final ClassName SCOPE = ClassName.get("jakarta.inject", "Scope"); 146 public static final ClassName SCOPE_JAVAX = ClassName.get("javax.inject", "Scope"); 147 public static final ClassName INJECT = ClassName.get("jakarta.inject", "Inject"); 148 public static final ClassName INJECT_JAVAX = ClassName.get("javax.inject", "Inject"); 149 public static final ClassName QUALIFIER = ClassName.get("jakarta.inject", "Qualifier"); 150 public static final ClassName QUALIFIER_JAVAX = ClassName.get("javax.inject", "Qualifier"); 151 public static final ClassName COLLECTION = ClassName.get("java.util", "Collection"); 152 public static final ClassName LIST = ClassName.get("java.util", "List"); 153 public static final ClassName SET = ClassName.get("java.util", "Set"); 154 public static final ClassName IMMUTABLE_SET = 155 ClassName.get("com.google.common.collect", "ImmutableSet"); 156 public static final ClassName FUTURES = 157 ClassName.get("com.google.common.util.concurrent", "Futures"); 158 public static final ClassName LISTENABLE_FUTURE = 159 ClassName.get("com.google.common.util.concurrent", "ListenableFuture"); 160 public static final ClassName GUAVA_OPTIONAL = 161 ClassName.get("com.google.common.base", "Optional"); 162 public static final ClassName JDK_OPTIONAL = ClassName.get("java.util", "Optional"); 163 public static final ClassName OVERRIDE = ClassName.get("java.lang", "Override"); 164 public static final ClassName JVM_STATIC = ClassName.get("kotlin.jvm", "JvmStatic"); 165 166 /** 167 * {@link TypeName#VOID} is lowercase-v {@code void} whereas this represents the class, {@link 168 * Void}. 169 */ 170 public static final ClassName VOID_CLASS = ClassName.get(Void.class); 171 abstractProducerOf(TypeName typeName)172 public static ParameterizedTypeName abstractProducerOf(TypeName typeName) { 173 return ParameterizedTypeName.get(ABSTRACT_PRODUCER, typeName); 174 } 175 factoryOf(TypeName factoryType)176 public static ParameterizedTypeName factoryOf(TypeName factoryType) { 177 return ParameterizedTypeName.get(FACTORY, factoryType); 178 } 179 lazyOf(TypeName typeName)180 public static ParameterizedTypeName lazyOf(TypeName typeName) { 181 return ParameterizedTypeName.get(LAZY, typeName); 182 } 183 listOf(TypeName typeName)184 public static ParameterizedTypeName listOf(TypeName typeName) { 185 return ParameterizedTypeName.get(LIST, typeName); 186 } 187 listenableFutureOf(TypeName typeName)188 public static ParameterizedTypeName listenableFutureOf(TypeName typeName) { 189 return ParameterizedTypeName.get(LISTENABLE_FUTURE, typeName); 190 } 191 membersInjectorOf(TypeName membersInjectorType)192 public static ParameterizedTypeName membersInjectorOf(TypeName membersInjectorType) { 193 return ParameterizedTypeName.get(MEMBERS_INJECTOR, membersInjectorType); 194 } 195 producedOf(TypeName typeName)196 public static ParameterizedTypeName producedOf(TypeName typeName) { 197 return ParameterizedTypeName.get(PRODUCED, typeName); 198 } 199 producerOf(TypeName typeName)200 public static ParameterizedTypeName producerOf(TypeName typeName) { 201 return ParameterizedTypeName.get(PRODUCER, typeName); 202 } 203 dependencyMethodProducerOf(TypeName typeName)204 public static ParameterizedTypeName dependencyMethodProducerOf(TypeName typeName) { 205 return ParameterizedTypeName.get(DEPENDENCY_METHOD_PRODUCER, typeName); 206 } 207 providerOf(TypeName typeName)208 public static ParameterizedTypeName providerOf(TypeName typeName) { 209 return ParameterizedTypeName.get(PROVIDER, typeName); 210 } 211 setOf(TypeName elementType)212 public static ParameterizedTypeName setOf(TypeName elementType) { 213 return ParameterizedTypeName.get(SET, elementType); 214 } 215 216 /** 217 * Returns the {@link TypeName} for the raw type of the given {@link TypeName}. If the argument 218 * isn't a parameterized type, it returns the argument unchanged. 219 */ rawTypeName(TypeName typeName)220 public static TypeName rawTypeName(TypeName typeName) { 221 return (typeName instanceof ParameterizedTypeName) 222 ? ((ParameterizedTypeName) typeName).rawType 223 : typeName; 224 } 225 226 /** 227 * Returns the {@link TypeName} for the raw type of the given {@link TypeMirror}. If the argument 228 * isn't a parameterized type, it returns the argument unchanged. 229 */ rawTypeName(TypeMirror type)230 public static TypeName rawTypeName(TypeMirror type) { 231 return rawTypeName(TypeName.get(type)); 232 } 233 TypeNames()234 private TypeNames() {} 235 } 236