diff --git a/java/dagger/internal/codegen/binding/AnnotationExpression.java b/java/dagger/internal/codegen/binding/AnnotationExpression.java index 535a7ef5755..db0bf556430 100644 --- a/java/dagger/internal/codegen/binding/AnnotationExpression.java +++ b/java/dagger/internal/codegen/binding/AnnotationExpression.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.binding; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static androidx.room.compiler.processing.XTypeKt.isArray; import static dagger.internal.codegen.binding.SourceFiles.classFileName; import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList; @@ -26,11 +27,11 @@ import static dagger.internal.codegen.xprocessing.XTypes.asArray; import static dagger.internal.codegen.xprocessing.XTypes.isTypeOf; +import androidx.room.compiler.codegen.XClassName; import androidx.room.compiler.processing.XAnnotation; import androidx.room.compiler.processing.XAnnotationValue; import androidx.room.compiler.processing.XType; import androidx.room.compiler.processing.XTypeElement; -import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; import dagger.internal.codegen.javapoet.TypeNames; @@ -47,7 +48,7 @@ */ public final class AnnotationExpression { private final XAnnotation annotation; - private final ClassName creatorClass; + private final XClassName creatorClass; AnnotationExpression(XAnnotation annotation) { this.annotation = annotation; @@ -65,7 +66,7 @@ CodeBlock getAnnotationInstanceExpression() { private CodeBlock getAnnotationInstanceExpression(XAnnotation annotation) { return CodeBlock.of( "$T.$L($L)", - creatorClass, + toJavaPoet(creatorClass), createMethodName(annotation.getType().getTypeElement()), makeParametersCodeBlock( annotation.getAnnotationValues().stream() @@ -77,11 +78,11 @@ private CodeBlock getAnnotationInstanceExpression(XAnnotation annotation) { * Returns the name of the generated class that contains the static {@code create} methods for an * annotation type. */ - public static ClassName getAnnotationCreatorClassName(XTypeElement annotationType) { - ClassName annotationTypeName = annotationType.getClassName(); - return annotationTypeName - .topLevelClassName() - .peerClass(classFileName(annotationTypeName) + "Creator"); + public static XClassName getAnnotationCreatorClassName(XTypeElement annotationType) { + XClassName annotationTypeName = annotationType.asClassName(); + return XClassName.Companion.get( + annotationTypeName.getPackageName(), + classFileName(annotationTypeName) + "Creator"); } public static String createMethodName(XTypeElement annotationType) { diff --git a/java/dagger/internal/codegen/binding/ComponentDeclarations.java b/java/dagger/internal/codegen/binding/ComponentDeclarations.java index 401dcf3031f..3bf35cdf018 100644 --- a/java/dagger/internal/codegen/binding/ComponentDeclarations.java +++ b/java/dagger/internal/codegen/binding/ComponentDeclarations.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.binding; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static com.google.common.collect.Iterables.getOnlyElement; import static dagger.internal.codegen.binding.SourceFiles.generatedMonitoringModuleName; import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; @@ -245,7 +246,8 @@ private ImmutableSet implicitProductionModules( ? ImmutableSet.of( moduleDescriptorFactory.create( DaggerSuperficialValidation.requireTypeElement( - processingEnv, generatedMonitoringModuleName(descriptor.typeElement()))), + processingEnv, + toJavaPoet(generatedMonitoringModuleName(descriptor.typeElement())))), moduleDescriptorFactory.create( processingEnv.requireTypeElement(TypeNames.PRODUCTION_EXECTUTOR_MODULE))) : ImmutableSet.of(); diff --git a/java/dagger/internal/codegen/binding/ComponentRequirement.java b/java/dagger/internal/codegen/binding/ComponentRequirement.java index e54fee19f77..5813506a88c 100644 --- a/java/dagger/internal/codegen/binding/ComponentRequirement.java +++ b/java/dagger/internal/codegen/binding/ComponentRequirement.java @@ -228,7 +228,7 @@ private static ComponentRequirement create(Kind kind, XType type) { /* overrideNullPolicy= */ Optional.empty(), /* key= */ Optional.empty(), Nullability.NOT_NULLABLE, - simpleVariableName(type.getTypeElement().getClassName())); + simpleVariableName(type.getTypeElement().asClassName())); } private static ComponentRequirement create( diff --git a/java/dagger/internal/codegen/binding/InjectionAnnotations.java b/java/dagger/internal/codegen/binding/InjectionAnnotations.java index a6d771425d3..13abe8b24e5 100644 --- a/java/dagger/internal/codegen/binding/InjectionAnnotations.java +++ b/java/dagger/internal/codegen/binding/InjectionAnnotations.java @@ -36,6 +36,7 @@ import static dagger.internal.codegen.xprocessing.XElements.asTypeElement; import static dagger.internal.codegen.xprocessing.XElements.closestEnclosingTypeElement; +import androidx.room.compiler.codegen.XClassName; import androidx.room.compiler.processing.XAnnotation; import androidx.room.compiler.processing.XConstructorElement; import androidx.room.compiler.processing.XElement; @@ -45,7 +46,6 @@ import androidx.room.compiler.processing.XTypeElement; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import com.squareup.javapoet.ClassName; import dagger.internal.codegen.base.DaggerSuperficialValidation; import dagger.internal.codegen.base.ElementFormatter; import dagger.internal.codegen.compileroption.CompilerOptions; @@ -54,6 +54,7 @@ import dagger.internal.codegen.model.DaggerAnnotation; import dagger.internal.codegen.model.Scope; import dagger.internal.codegen.xprocessing.XAnnotations; +import dagger.internal.codegen.xprocessing.XTypeNames; import java.util.Optional; import java.util.stream.Stream; import javax.inject.Inject; @@ -165,10 +166,10 @@ private Optional> getScopesWithMetadata(XElement element) { private Optional getScopeMetadata(XElement element) { return getGeneratedNameForScopeMetadata(element) .flatMap(factoryName -> Optional.ofNullable(processingEnv.findTypeElement(factoryName))) - .flatMap(factory -> Optional.ofNullable(factory.getAnnotation(TypeNames.SCOPE_METADATA))); + .flatMap(factory -> Optional.ofNullable(factory.getAnnotation(XTypeNames.SCOPE_METADATA))); } - private Optional getGeneratedNameForScopeMetadata(XElement element) { + private Optional getGeneratedNameForScopeMetadata(XElement element) { // Currently, we only support ScopeMetadata for inject-constructor types and provides methods. if (isTypeElement(element)) { return asTypeElement(element).getConstructors().stream() @@ -297,10 +298,10 @@ private Optional> getQualifiersWithMetadata(XElement e private Optional getQualifierMetadata(XElement element) { return getGeneratedNameForQualifierMetadata(element) .flatMap(name -> Optional.ofNullable(processingEnv.findTypeElement(name))) - .flatMap(type -> Optional.ofNullable(type.getAnnotation(TypeNames.QUALIFIER_METADATA))); + .flatMap(type -> Optional.ofNullable(type.getAnnotation(XTypeNames.QUALIFIER_METADATA))); } - private Optional getGeneratedNameForQualifierMetadata(XElement element) { + private Optional getGeneratedNameForQualifierMetadata(XElement element) { // Currently we only support @QualifierMetadata for @Inject fields, @Inject method parameters, // @Inject constructor parameters, @Provides methods, and @Provides method parameters. if (isField(element) && hasInjectAnnotation(element)) { diff --git a/java/dagger/internal/codegen/binding/MapKeys.java b/java/dagger/internal/codegen/binding/MapKeys.java index 0537c4cb653..a38bf8ad73c 100644 --- a/java/dagger/internal/codegen/binding/MapKeys.java +++ b/java/dagger/internal/codegen/binding/MapKeys.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.binding; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static androidx.room.compiler.processing.XTypeKt.isArray; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Iterables.getOnlyElement; @@ -177,7 +178,8 @@ private static CodeBlock directMapKeyExpression( * XProcessingEnv)} is generated. */ public static ClassName mapKeyProxyClassName(ContributionBinding binding) { - return elementBasedClassName(asExecutable(binding.bindingElement().get()), "MapKey"); + return toJavaPoet( + elementBasedClassName(asExecutable(binding.bindingElement().get()), "MapKey")); } /** @@ -226,7 +228,7 @@ public static CodeBlock getLazyClassMapKeyExpression(ContributionBinding contrib } public static ClassName lazyClassKeyProxyClassName(XMethodElement methodElement) { - return elementBasedClassName(methodElement, "_LazyMapKey"); + return toJavaPoet(elementBasedClassName(methodElement, "_LazyMapKey")); } private MapKeys() {} diff --git a/java/dagger/internal/codegen/binding/ModuleDescriptor.java b/java/dagger/internal/codegen/binding/ModuleDescriptor.java index fe912393e4b..bde718f13aa 100644 --- a/java/dagger/internal/codegen/binding/ModuleDescriptor.java +++ b/java/dagger/internal/codegen/binding/ModuleDescriptor.java @@ -267,7 +267,7 @@ private ClassName implicitlyIncludedModuleName(XTypeElement module, XMethodEleme module.getPackageName(), String.format( "%s_%s", - classFileName(module.getClassName()), + classFileName(module.asClassName()), LOWER_CAMEL.to(UPPER_CAMEL, getSimpleName(method)))); } diff --git a/java/dagger/internal/codegen/binding/SourceFiles.java b/java/dagger/internal/codegen/binding/SourceFiles.java index 0bb77f384d3..8319181bca5 100644 --- a/java/dagger/internal/codegen/binding/SourceFiles.java +++ b/java/dagger/internal/codegen/binding/SourceFiles.java @@ -16,23 +16,16 @@ package dagger.internal.codegen.binding; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static androidx.room.compiler.processing.XElementKt.isConstructor; import static com.google.common.base.CaseFormat.LOWER_CAMEL; import static com.google.common.base.CaseFormat.UPPER_CAMEL; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Verify.verify; import static dagger.internal.codegen.javapoet.TypeNames.DOUBLE_CHECK; -import static dagger.internal.codegen.javapoet.TypeNames.MAP_FACTORY; -import static dagger.internal.codegen.javapoet.TypeNames.MAP_OF_PRODUCED_PRODUCER; -import static dagger.internal.codegen.javapoet.TypeNames.MAP_OF_PRODUCER_PRODUCER; -import static dagger.internal.codegen.javapoet.TypeNames.MAP_PRODUCER; -import static dagger.internal.codegen.javapoet.TypeNames.MAP_PROVIDER_FACTORY; import static dagger.internal.codegen.javapoet.TypeNames.PRODUCER; import static dagger.internal.codegen.javapoet.TypeNames.PROVIDER; import static dagger.internal.codegen.javapoet.TypeNames.PROVIDER_OF_LAZY; -import static dagger.internal.codegen.javapoet.TypeNames.SET_FACTORY; -import static dagger.internal.codegen.javapoet.TypeNames.SET_OF_PRODUCED_PRODUCER; -import static dagger.internal.codegen.javapoet.TypeNames.SET_PRODUCER; import static dagger.internal.codegen.model.BindingKind.ASSISTED_INJECTION; import static dagger.internal.codegen.model.BindingKind.INJECTION; import static dagger.internal.codegen.xprocessing.XElements.asExecutable; @@ -40,6 +33,7 @@ import static dagger.internal.codegen.xprocessing.XElements.asTypeElement; import static dagger.internal.codegen.xprocessing.XElements.getSimpleName; import static dagger.internal.codegen.xprocessing.XTypeElements.typeVariableNames; +import static dagger.internal.codegen.xprocessing.XTypeNames.simpleName; import static javax.lang.model.SourceVersion.isName; import androidx.room.compiler.codegen.XClassName; @@ -65,6 +59,7 @@ import dagger.internal.codegen.javapoet.TypeNames; import dagger.internal.codegen.model.DependencyRequest; import dagger.internal.codegen.model.RequestKind; +import dagger.internal.codegen.xprocessing.XTypeNames; import javax.inject.Inject; import javax.lang.model.SourceVersion; @@ -157,7 +152,7 @@ public static String generatedProxyMethodName(ContributionBinding binding) { } /** Returns the generated factory or members injector name for a binding. */ - public static ClassName generatedClassNameForBinding(Binding binding) { + public static XClassName generatedClassNameForBinding(Binding binding) { switch (binding.kind()) { case ASSISTED_INJECTION: case INJECTION: @@ -182,35 +177,35 @@ public static ClassName generatedClassNameForBinding(Binding binding) { * #generatedClassNameForBinding(Binding)} instead since this method does not validate that the * given element is actually a binding element or not. */ - public static ClassName factoryNameForElement(XExecutableElement element) { + public static XClassName factoryNameForElement(XExecutableElement element) { return elementBasedClassName(element, "Factory"); } /** - * Calculates an appropriate {@link ClassName} for a generated class that is based on {@code + * Calculates an appropriate {@link XClassName} for a generated class that is based on {@code * element}, appending {@code suffix} at the end. * - *

This will always return a {@linkplain ClassName#topLevelClassName() top level class name}, - * even if {@code element}'s enclosing class is a nested type. + *

This will always return a top level class name, even if {@code element}'s enclosing class is + * a nested type. */ - public static ClassName elementBasedClassName(XExecutableElement element, String suffix) { - ClassName enclosingClassName = element.getEnclosingElement().getClassName(); + public static XClassName elementBasedClassName(XExecutableElement element, String suffix) { + XClassName enclosingClassName = element.getEnclosingElement().asClassName(); String methodName = isConstructor(element) ? "" : LOWER_CAMEL.to(UPPER_CAMEL, getSimpleName(element)); - return ClassName.get( - enclosingClassName.packageName(), + return XClassName.Companion.get( + enclosingClassName.getPackageName(), classFileName(enclosingClassName) + "_" + methodName + suffix); } public static TypeName parameterizedGeneratedTypeNameForBinding(Binding binding) { - ClassName className = generatedClassNameForBinding(binding); + ClassName className = toJavaPoet(generatedClassNameForBinding(binding)); ImmutableList typeParameters = bindingTypeElementTypeVariableNames(binding); return typeParameters.isEmpty() ? className : ParameterizedTypeName.get(className, Iterables.toArray(typeParameters, TypeName.class)); } - public static ClassName membersInjectorNameForType(XTypeElement typeElement) { + public static XClassName membersInjectorNameForType(XTypeElement typeElement) { return siblingClassName(typeElement, "_MembersInjector"); } @@ -241,19 +236,19 @@ public static String membersInjectorMethodName(InjectionSite injectionSite) { + indexString; } - public static String classFileName(ClassName className) { - return CLASS_FILE_NAME_JOINER.join(className.simpleNames()); + public static String classFileName(XClassName className) { + return CLASS_FILE_NAME_JOINER.join(className.getSimpleNames()); } - public static ClassName generatedMonitoringModuleName(XTypeElement componentElement) { + public static XClassName generatedMonitoringModuleName(XTypeElement componentElement) { return siblingClassName(componentElement, "_MonitoringModule"); } // TODO(ronshapiro): when JavaPoet migration is complete, replace the duplicated code // which could use this. - private static ClassName siblingClassName(XTypeElement typeElement, String suffix) { - ClassName className = typeElement.getClassName(); - return className.topLevelClassName().peerClass(classFileName(className) + suffix); + private static XClassName siblingClassName(XTypeElement typeElement, String suffix) { + XClassName className = typeElement.asClassName(); + return XClassName.Companion.get(className.getPackageName(), classFileName(className) + suffix); } /** @@ -266,32 +261,33 @@ private static ClassName siblingClassName(XTypeElement typeElement, String suffi * {@code Set>}. * */ - public static ClassName setFactoryClassName(MultiboundSetBinding binding) { + public static XClassName setFactoryClassName(MultiboundSetBinding binding) { switch (binding.bindingType()) { case PROVISION: - return SET_FACTORY; + return XTypeNames.SET_FACTORY; case PRODUCTION: SetType setType = SetType.from(binding.key()); return setType.elementsAreTypeOf(TypeNames.PRODUCED) - ? SET_OF_PRODUCED_PRODUCER - : SET_PRODUCER; + ? XTypeNames.SET_OF_PRODUCED_PRODUCER + : XTypeNames.SET_PRODUCER; default: throw new IllegalArgumentException(binding.bindingType().toString()); } } /** The {@link java.util.Map} factory class name appropriate for map bindings. */ - public static ClassName mapFactoryClassName(MultiboundMapBinding binding) { + public static XClassName mapFactoryClassName(MultiboundMapBinding binding) { MapType mapType = MapType.from(binding.key()); switch (binding.bindingType()) { case PROVISION: - return mapType.valuesAreTypeOf(PROVIDER) ? MAP_PROVIDER_FACTORY : MAP_FACTORY; + return mapType.valuesAreTypeOf(PROVIDER) + ? XTypeNames.MAP_PROVIDER_FACTORY : XTypeNames.MAP_FACTORY; case PRODUCTION: return mapType.valuesAreFrameworkType() ? mapType.valuesAreTypeOf(PRODUCER) - ? MAP_OF_PRODUCER_PRODUCER - : MAP_OF_PRODUCED_PRODUCER - : MAP_PRODUCER; + ? XTypeNames.MAP_OF_PRODUCER_PRODUCER + : XTypeNames.MAP_OF_PRODUCED_PRODUCER + : XTypeNames.MAP_PRODUCER; default: throw new IllegalArgumentException(binding.bindingType().toString()); } @@ -317,16 +313,15 @@ public static ImmutableList bindingTypeElementTypeVariableName */ // TODO(gak): maybe this should be a function of TypeMirrors instead of Elements? public static String simpleVariableName(XTypeElement typeElement) { - return simpleVariableName(typeElement.getClassName()); + return simpleVariableName(typeElement.asClassName()); } /** - * Returns a name to be used for variables of the given {@linkplain ClassName}. Prefer - * semantically meaningful variable names, but if none can be derived, this will produce something - * readable. + * Returns a name to be used for variables of the given {@link XClassName}. Prefer semantically + * meaningful variable names, but if none can be derived, this will produce something readable. */ - public static String simpleVariableName(ClassName className) { - String candidateName = UPPER_CAMEL.to(LOWER_CAMEL, className.simpleName()); + public static String simpleVariableName(XClassName className) { + String candidateName = UPPER_CAMEL.to(LOWER_CAMEL, simpleName(className)); String variableName = protectAgainstKeywords(candidateName); verify(isName(variableName), "'%s' was expected to be a valid variable name", variableName); return variableName; diff --git a/java/dagger/internal/codegen/componentgenerator/ComponentHjarGenerator.java b/java/dagger/internal/codegen/componentgenerator/ComponentHjarGenerator.java index 4214cbc7239..f2e2103d150 100644 --- a/java/dagger/internal/codegen/componentgenerator/ComponentHjarGenerator.java +++ b/java/dagger/internal/codegen/componentgenerator/ComponentHjarGenerator.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.componentgenerator; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static com.google.common.base.CaseFormat.LOWER_CAMEL; import static com.google.common.base.CaseFormat.UPPER_CAMEL; import static com.google.common.base.Preconditions.checkArgument; @@ -90,7 +91,7 @@ public XElement originatingElement(ComponentDescriptor input) { @Override public ImmutableList topLevelTypes(ComponentDescriptor componentDescriptor) { - ClassName generatedTypeName = getTopLevelClassName(componentDescriptor); + ClassName generatedTypeName = toJavaPoet(getTopLevelClassName(componentDescriptor)); TypeSpec.Builder generatedComponent = TypeSpec.classBuilder(generatedTypeName) .addModifiers(FINAL) diff --git a/java/dagger/internal/codegen/processingstep/AssistedFactoryProcessingStep.java b/java/dagger/internal/codegen/processingstep/AssistedFactoryProcessingStep.java index 6608d5b7c45..3d4c11b7479 100644 --- a/java/dagger/internal/codegen/processingstep/AssistedFactoryProcessingStep.java +++ b/java/dagger/internal/codegen/processingstep/AssistedFactoryProcessingStep.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.processingstep; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static com.google.common.collect.Iterables.getOnlyElement; import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.assistedFactoryMethods; import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.assistedInjectedConstructors; @@ -62,6 +63,7 @@ import dagger.internal.codegen.binding.AssistedInjectionAnnotations; import dagger.internal.codegen.binding.AssistedInjectionAnnotations.AssistedFactoryMetadata; import dagger.internal.codegen.binding.AssistedInjectionAnnotations.AssistedParameter; +import dagger.internal.codegen.binding.AssistedInjectionBinding; import dagger.internal.codegen.binding.BindingFactory; import dagger.internal.codegen.binding.MethodSignatureFormatter; import dagger.internal.codegen.javapoet.TypeNames; @@ -272,7 +274,7 @@ public XElement originatingElement(AssistedFactoryBinding binding) { public ImmutableList topLevelTypes(AssistedFactoryBinding binding) { XTypeElement factory = asTypeElement(binding.bindingElement().get()); - ClassName name = generatedClassNameForBinding(binding); + ClassName name = toJavaPoet(generatedClassNameForBinding(binding)); TypeSpec.Builder builder = TypeSpec.classBuilder(name) .addModifiers(PUBLIC, FINAL) @@ -359,13 +361,14 @@ public ImmutableList topLevelTypes(AssistedFactoryBinding bind /** Returns the generated factory {@link TypeName type} for an @AssistedInject constructor. */ private TypeName delegateFactoryTypeName(XType assistedInjectType) { + AssistedInjectionBinding binding = + bindingFactory.assistedInjectionBinding( + getOnlyElement(assistedInjectedConstructors(assistedInjectType.getTypeElement())), + Optional.empty()); + // The name of the generated factory for the assisted inject type, // e.g. an @AssistedInject Foo(...) {...} constructor will generate a Foo_Factory class. - ClassName generatedFactoryClassName = - generatedClassNameForBinding( - bindingFactory.assistedInjectionBinding( - getOnlyElement(assistedInjectedConstructors(assistedInjectType.getTypeElement())), - Optional.empty())); + ClassName generatedFactoryClassName = toJavaPoet(generatedClassNameForBinding(binding)); // Return the factory type resolved with the same type parameters as the assisted inject type. return assistedInjectType.getTypeArguments().isEmpty() diff --git a/java/dagger/internal/codegen/processingstep/MonitoringModuleGenerator.java b/java/dagger/internal/codegen/processingstep/MonitoringModuleGenerator.java index 3692144cf67..362992b7ae9 100644 --- a/java/dagger/internal/codegen/processingstep/MonitoringModuleGenerator.java +++ b/java/dagger/internal/codegen/processingstep/MonitoringModuleGenerator.java @@ -16,9 +16,11 @@ package dagger.internal.codegen.processingstep; +import static androidx.room.compiler.codegen.compat.XConverters.toJavaPoet; import static com.squareup.javapoet.MethodSpec.constructorBuilder; import static com.squareup.javapoet.MethodSpec.methodBuilder; import static com.squareup.javapoet.TypeSpec.classBuilder; +import static dagger.internal.codegen.binding.SourceFiles.generatedMonitoringModuleName; import static dagger.internal.codegen.javapoet.TypeNames.PRODUCTION_COMPONENT_MONITOR_FACTORY; import static dagger.internal.codegen.javapoet.TypeNames.providerOf; import static dagger.internal.codegen.javapoet.TypeNames.setOf; @@ -31,12 +33,12 @@ import androidx.room.compiler.processing.XProcessingEnv; import androidx.room.compiler.processing.XTypeElement; import com.google.common.collect.ImmutableList; +import com.squareup.javapoet.ClassName; import com.squareup.javapoet.MethodSpec; import com.squareup.javapoet.TypeSpec; import dagger.Module; import dagger.internal.codegen.base.SourceFileGenerator; import dagger.internal.codegen.binding.MonitoringModules; -import dagger.internal.codegen.binding.SourceFiles; import dagger.internal.codegen.javapoet.TypeNames; import dagger.multibindings.Multibinds; import javax.inject.Inject; @@ -61,9 +63,10 @@ public XElement originatingElement(XTypeElement componentElement) { @Override public ImmutableList topLevelTypes(XTypeElement componentElement) { - monitoringModules.add(SourceFiles.generatedMonitoringModuleName(componentElement)); + ClassName name = toJavaPoet(generatedMonitoringModuleName(componentElement)); + monitoringModules.add(name); return ImmutableList.of( - classBuilder(SourceFiles.generatedMonitoringModuleName(componentElement)) + classBuilder(name) .addAnnotation(Module.class) .addModifiers(ABSTRACT) .addMethod(privateConstructor()) diff --git a/java/dagger/internal/codegen/writing/AnnotationCreatorGenerator.java b/java/dagger/internal/codegen/writing/AnnotationCreatorGenerator.java index 610e4203098..08f6153f70a 100644 --- a/java/dagger/internal/codegen/writing/AnnotationCreatorGenerator.java +++ b/java/dagger/internal/codegen/writing/AnnotationCreatorGenerator.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static androidx.room.compiler.processing.XTypeKt.isArray; import static androidx.room.compiler.processing.compat.XConverters.getProcessingEnv; import static com.squareup.javapoet.MethodSpec.constructorBuilder; @@ -95,7 +96,7 @@ public XElement originatingElement(XTypeElement annotationType) { @Override public ImmutableList topLevelTypes(XTypeElement annotationType) { - ClassName generatedTypeName = getAnnotationCreatorClassName(annotationType); + ClassName generatedTypeName = toJavaPoet(getAnnotationCreatorClassName(annotationType)); TypeSpec.Builder annotationCreatorBuilder = classBuilder(generatedTypeName) .addModifiers(PUBLIC, FINAL) diff --git a/java/dagger/internal/codegen/writing/ComponentCreatorImplementationFactory.java b/java/dagger/internal/codegen/writing/ComponentCreatorImplementationFactory.java index d38feab2f9a..df55b93ceba 100644 --- a/java/dagger/internal/codegen/writing/ComponentCreatorImplementationFactory.java +++ b/java/dagger/internal/codegen/writing/ComponentCreatorImplementationFactory.java @@ -498,7 +498,7 @@ protected MethodSpec.Builder factoryMethodBuilder() { @Override protected MethodSpec.Builder setterMethodBuilder(ComponentRequirement requirement) { - String name = simpleVariableName(requirement.typeElement().getClassName()); + String name = simpleVariableName(requirement.typeElement().asClassName()); return methodBuilder(name) .addModifiers(PUBLIC) .addParameter(requirement.type().getTypeName(), name) diff --git a/java/dagger/internal/codegen/writing/ComponentImplementation.java b/java/dagger/internal/codegen/writing/ComponentImplementation.java index e11d3a3d47c..4583396c75b 100644 --- a/java/dagger/internal/codegen/writing/ComponentImplementation.java +++ b/java/dagger/internal/codegen/writing/ComponentImplementation.java @@ -16,6 +16,8 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; +import static androidx.room.compiler.codegen.compat.XConverters.toXPoet; import static com.google.common.base.CaseFormat.LOWER_CAMEL; import static com.google.common.base.CaseFormat.UPPER_CAMEL; import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE; @@ -298,7 +300,8 @@ private static ImmutableList> bindingPartitions( this.processingEnv = processingEnv; // The first group of keys belong to the component itself. We call this the componentShard. - this.componentShard = new ShardImplementation(componentNames.get(graph.componentPath())); + this.componentShard = + new ShardImplementation(toJavaPoet(componentNames.get(graph.componentPath()))); // Claim the method names for all local and inherited methods on the component type. XTypeElements.getAllNonPrivateInstanceMethods(graph.componentTypeElement()).stream() @@ -383,8 +386,8 @@ public ImmutableList creatorComponentFields() { ClassName fieldType = componentImpl.name(); String fieldName = componentImpl.isNested() - ? simpleVariableName(componentImpl.name()) - : simpleVariableName(component); + ? simpleVariableName(toXPoet(componentImpl.name())) + : simpleVariableName(toXPoet(component)); FieldSpec.Builder field = FieldSpec.builder( fieldType, @@ -433,7 +436,7 @@ private boolean isNested() { * generated class unless this is a top-level component, in which case it will be nested. */ public ClassName getCreatorName() { - return componentNames.getCreatorName(graph.componentPath()); + return toJavaPoet(componentNames.getCreatorName(graph.componentPath())); } /** Generates the component and returns the resulting {@link TypeSpec}. */ @@ -588,7 +591,8 @@ public ClassName name() { * {@link Key}. */ ClassName getSubcomponentCreatorSimpleName(Key creatorKey) { - return componentNames.getSubcomponentCreatorName(graph.componentPath(), creatorKey); + return toJavaPoet( + componentNames.getSubcomponentCreatorName(graph.componentPath(), creatorKey)); } /** diff --git a/java/dagger/internal/codegen/writing/ComponentNames.java b/java/dagger/internal/codegen/writing/ComponentNames.java index b5f2f2f99a0..79344c7b12b 100644 --- a/java/dagger/internal/codegen/writing/ComponentNames.java +++ b/java/dagger/internal/codegen/writing/ComponentNames.java @@ -22,6 +22,7 @@ import static dagger.internal.codegen.extension.DaggerCollectors.onlyElement; import static java.lang.String.format; +import androidx.room.compiler.codegen.XClassName; import com.google.common.base.CharMatcher; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableMap; @@ -50,16 +51,16 @@ */ public final class ComponentNames { /** Returns the class name for the top-level generated class. */ - public static ClassName getTopLevelClassName(ComponentDescriptor componentDescriptor) { + public static XClassName getTopLevelClassName(ComponentDescriptor componentDescriptor) { checkState(!componentDescriptor.isSubcomponent()); - ClassName componentName = componentDescriptor.typeElement().getClassName(); - return ClassName.get(componentName.packageName(), "Dagger" + classFileName(componentName)); + XClassName componentName = componentDescriptor.typeElement().asClassName(); + return XClassName.Companion.get(componentName.getPackageName(), "Dagger" + classFileName(componentName)); } private static final Splitter QUALIFIED_NAME_SPLITTER = Splitter.on('.'); private final CompilerOptions compilerOptions; - private final ClassName topLevelClassName; + private final XClassName topLevelClassName; private final ImmutableMap namesByPath; private final ImmutableMap creatorNamesByPath; private final ImmutableMultimap pathsByCreatorKey; @@ -75,7 +76,7 @@ public static ClassName getTopLevelClassName(ComponentDescriptor componentDescri } /** Returns the simple component name for the given {@link ComponentDescriptor}. */ - ClassName get(ComponentPath componentPath) { + XClassName get(ComponentPath componentPath) { return compilerOptions.generatedClassExtendsComponent() && componentPath.atRoot() ? topLevelClassName : topLevelClassName.nestedClass(namesByPath.get(componentPath) + "Impl"); @@ -85,7 +86,7 @@ ClassName get(ComponentPath componentPath) { * Returns the component descriptor for the component with the given subcomponent creator {@link * Key}. */ - ClassName getSubcomponentCreatorName(ComponentPath componentPath, Key creatorKey) { + XClassName getSubcomponentCreatorName(ComponentPath componentPath, Key creatorKey) { checkArgument(pathsByCreatorKey.containsKey(creatorKey)); // First, find the subcomponent path corresponding to the subcomponent creator key. // The key may correspond to multiple paths, so we need to find the one under this component. @@ -100,7 +101,7 @@ ClassName getSubcomponentCreatorName(ComponentPath componentPath, Key creatorKey * Returns the simple name for the subcomponent creator implementation for the given {@link * ComponentDescriptor}. */ - ClassName getCreatorName(ComponentPath componentPath) { + XClassName getCreatorName(ComponentPath componentPath) { checkArgument(creatorNamesByPath.containsKey(componentPath)); return topLevelClassName.nestedClass(creatorNamesByPath.get(componentPath)); } diff --git a/java/dagger/internal/codegen/writing/ComponentWrapperImplementation.java b/java/dagger/internal/codegen/writing/ComponentWrapperImplementation.java index a5be3a4f4d0..3f8894d351d 100644 --- a/java/dagger/internal/codegen/writing/ComponentWrapperImplementation.java +++ b/java/dagger/internal/codegen/writing/ComponentWrapperImplementation.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static com.squareup.javapoet.MethodSpec.constructorBuilder; import static com.squareup.javapoet.TypeSpec.classBuilder; import static dagger.internal.codegen.writing.ComponentNames.getTopLevelClassName; @@ -56,7 +57,7 @@ public final class ComponentWrapperImplementation implements GeneratedImplementa @Inject ComponentWrapperImplementation(@TopLevel BindingGraph graph) { this.graph = graph; - this.name = ComponentNames.getTopLevelClassName(graph.componentDescriptor()); + this.name = toJavaPoet(ComponentNames.getTopLevelClassName(graph.componentDescriptor())); } @Override @@ -92,7 +93,8 @@ public void addTypeSupplier(Supplier typeSpecSupplier) { @Override public TypeSpec generate() { TypeSpec.Builder builder = - classBuilder(getTopLevelClassName(graph.componentDescriptor())).addModifiers(FINAL); + classBuilder(toJavaPoet(getTopLevelClassName(graph.componentDescriptor()))) + .addModifiers(FINAL); if (graph.componentTypeElement().isPublic()) { builder.addModifiers(PUBLIC); diff --git a/java/dagger/internal/codegen/writing/FactoryGenerator.java b/java/dagger/internal/codegen/writing/FactoryGenerator.java index 818283a42d3..a6a4a712eb3 100644 --- a/java/dagger/internal/codegen/writing/FactoryGenerator.java +++ b/java/dagger/internal/codegen/writing/FactoryGenerator.java @@ -133,7 +133,7 @@ public ImmutableList topLevelTypes(ContributionBinding binding private TypeSpec.Builder factoryBuilder(ContributionBinding binding) { TypeSpec.Builder factoryBuilder = - classBuilder(generatedClassNameForBinding(binding)) + classBuilder(toJavaPoet(generatedClassNameForBinding(binding))) .addModifiers(PUBLIC, FINAL) .addTypeVariables(bindingTypeElementTypeVariableNames(binding)) .addAnnotation(scopeMetadataAnnotation(binding)) @@ -163,7 +163,7 @@ private TypeSpec.Builder factoryBuilder(ContributionBinding binding) { // new FooModule_ProvidesFooFactory(); // } private TypeSpec staticInstanceHolderType(ContributionBinding binding) { - ClassName generatedClassName = generatedClassNameForBinding(binding); + ClassName generatedClassName = toJavaPoet(generatedClassNameForBinding(binding)); FieldSpec.Builder instanceHolderFieldBuilder = FieldSpec.builder(generatedClassName, "INSTANCE", STATIC, FINAL) .initializer("new $T()", generatedClassName); @@ -178,7 +178,7 @@ private TypeSpec staticInstanceHolderType(ContributionBinding binding) { } private static ClassName instanceHolderClassName(ContributionBinding binding) { - return generatedClassNameForBinding(binding).nestedClass("InstanceHolder"); + return toJavaPoet(generatedClassNameForBinding(binding).nestedClass("InstanceHolder")); } // public FooModule_ProvidesFooFactory( @@ -310,7 +310,7 @@ private MethodSpec getMethod(ContributionBinding binding, FactoryFields factoryF sourceFiles.frameworkTypeUsageStatement( CodeBlock.of("$N", factoryFields.get(request)), request.kind()), param -> assistedParameters.get(param).name, - generatedClassNameForBinding(binding), + toJavaPoet(generatedClassNameForBinding(binding)), factoryFields.moduleField.map(module -> CodeBlock.of("$N", module)), compilerOptions); @@ -328,7 +328,7 @@ private MethodSpec getMethod(ContributionBinding binding, FactoryFields factoryF .addCode( InjectionSiteMethod.invokeAll( injectionSites(binding), - generatedClassNameForBinding(binding), + toJavaPoet(generatedClassNameForBinding(binding)), instance, binding.key().type().xprocessing(), sourceFiles.frameworkFieldUsages( diff --git a/java/dagger/internal/codegen/writing/FrameworkFieldInitializer.java b/java/dagger/internal/codegen/writing/FrameworkFieldInitializer.java index 26416550ecd..28dbaba8b23 100644 --- a/java/dagger/internal/codegen/writing/FrameworkFieldInitializer.java +++ b/java/dagger/internal/codegen/writing/FrameworkFieldInitializer.java @@ -24,12 +24,11 @@ import static javax.lang.model.element.Modifier.PRIVATE; import androidx.room.compiler.codegen.XClassName; +import androidx.room.compiler.codegen.XTypeName; import androidx.room.compiler.processing.XType; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; import com.squareup.javapoet.FieldSpec; -import com.squareup.javapoet.ParameterizedTypeName; -import com.squareup.javapoet.TypeName; import dagger.internal.DelegateFactory; import dagger.internal.codegen.binding.BindingType; import dagger.internal.codegen.binding.ContributionBinding; @@ -145,26 +144,27 @@ private FieldSpec getOrCreateField() { FrameworkField.forBinding( binding, frameworkInstanceCreationExpression.alternativeFrameworkClass()); - TypeName fieldType = useRawType - ? toJavaPoet(contributionBindingField.type().getRawTypeName()) - : toJavaPoet(contributionBindingField.type()); + XTypeName fieldType = useRawType + ? contributionBindingField.type().getRawTypeName() + : contributionBindingField.type(); if (binding.kind() == BindingKind.ASSISTED_INJECTION) { // An assisted injection factory doesn't extend Provider, so we reference the generated // factory type directly (i.e. Foo_Factory instead of Provider>). - TypeName[] typeParameters = + XTypeName[] typeParameters = binding.key().type().xprocessing().getTypeArguments().stream() - .map(XType::getTypeName) - .toArray(TypeName[]::new); + .map(XType::asTypeName) + .toArray(XTypeName[]::new); fieldType = typeParameters.length == 0 ? generatedClassNameForBinding(binding) - : ParameterizedTypeName.get(generatedClassNameForBinding(binding), typeParameters); + : generatedClassNameForBinding(binding).parametrizedBy(typeParameters); } FieldSpec.Builder contributionField = FieldSpec.builder( - fieldType, shardImplementation.getUniqueFieldName(contributionBindingField.name())); + toJavaPoet(fieldType), + shardImplementation.getUniqueFieldName(contributionBindingField.name())); contributionField.addModifiers(PRIVATE); if (useRawType) { contributionField.addAnnotation(AnnotationSpecs.suppressWarnings(RAWTYPES)); diff --git a/java/dagger/internal/codegen/writing/InjectionMethods.java b/java/dagger/internal/codegen/writing/InjectionMethods.java index 721cc8e983d..360adf1bfd4 100644 --- a/java/dagger/internal/codegen/writing/InjectionMethods.java +++ b/java/dagger/internal/codegen/writing/InjectionMethods.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static androidx.room.compiler.processing.XElementKt.isMethodParameter; import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.isAssistedParameter; import static dagger.internal.codegen.binding.SourceFiles.generatedClassNameForBinding; @@ -106,7 +107,7 @@ static CodeBlock invoke( invokeArguments(binding, dependencyUsage, uniqueAssistedParameterName) .forEach(arguments::add); - ClassName enclosingClass = generatedClassNameForBinding(binding); + ClassName enclosingClass = toJavaPoet(generatedClassNameForBinding(binding)); String methodName = generatedProxyMethodName(binding); return invokeMethod(methodName, arguments.build(), enclosingClass, requestingClass); } @@ -222,7 +223,8 @@ private static CodeBlock invoke( .map(dependencyUsage) .collect(toImmutableList())) .build(); - ClassName enclosingClass = membersInjectorNameForType(injectionSite.enclosingTypeElement()); + ClassName enclosingClass = + toJavaPoet(membersInjectorNameForType(injectionSite.enclosingTypeElement())); String methodName = membersInjectorMethodName(injectionSite); return invokeMethod(methodName, arguments, enclosingClass, generatedTypeName); } diff --git a/java/dagger/internal/codegen/writing/InjectionOrProvisionProviderCreationExpression.java b/java/dagger/internal/codegen/writing/InjectionOrProvisionProviderCreationExpression.java index 27e017dba2b..3d8fd86b24b 100644 --- a/java/dagger/internal/codegen/writing/InjectionOrProvisionProviderCreationExpression.java +++ b/java/dagger/internal/codegen/writing/InjectionOrProvisionProviderCreationExpression.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static com.google.common.base.Preconditions.checkNotNull; import static dagger.internal.codegen.binding.SourceFiles.generatedClassNameForBinding; import static dagger.internal.codegen.extension.DaggerCollectors.toOptional; @@ -66,7 +67,7 @@ final class InjectionOrProvisionProviderCreationExpression @Override public CodeBlock creationExpression() { - ClassName factoryImpl = generatedClassNameForBinding(binding); + ClassName factoryImpl = toJavaPoet(generatedClassNameForBinding(binding)); CodeBlock createFactory = CodeBlock.of( "$T.$L($L)", diff --git a/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java b/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java index 892eba963aa..0ac7212283e 100644 --- a/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java +++ b/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static com.google.common.base.Preconditions.checkNotNull; import static dagger.internal.codegen.binding.MapKeys.getLazyClassMapKeyExpression; import static dagger.internal.codegen.binding.MapKeys.getMapKeyExpression; @@ -62,7 +63,7 @@ final class MapFactoryCreationExpression extends MultibindingFactoryCreationExpr @Override public CodeBlock creationExpression() { - ClassName mapFactoryClassName = mapFactoryClassName(binding); + ClassName mapFactoryClassName = toJavaPoet(mapFactoryClassName(binding)); CodeBlock.Builder builder = CodeBlock.builder().add("$T.", mapFactoryClassName); TypeName valueTypeName = TypeName.OBJECT; if (!useRawType()) { diff --git a/java/dagger/internal/codegen/writing/MembersInjectorGenerator.java b/java/dagger/internal/codegen/writing/MembersInjectorGenerator.java index a9eacd51fb5..1b52eb03d6b 100644 --- a/java/dagger/internal/codegen/writing/MembersInjectorGenerator.java +++ b/java/dagger/internal/codegen/writing/MembersInjectorGenerator.java @@ -16,7 +16,7 @@ package dagger.internal.codegen.writing; -import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; +import static androidx.room.compiler.codegen.compat.XConverters.toJavaPoet; import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.Iterables.getOnlyElement; import static com.squareup.javapoet.MethodSpec.constructorBuilder; @@ -119,7 +119,8 @@ public ImmutableList topLevelTypes(MembersInjectionBinding bin "tried to generate a MembersInjector for a binding of a resolved generic type: %s", binding); - ClassName generatedTypeName = membersInjectorNameForType(binding.membersInjectedType()); + ClassName generatedTypeName = + toJavaPoet(membersInjectorNameForType(binding.membersInjectedType())); ImmutableList typeParameters = bindingTypeElementTypeVariableNames(binding); ImmutableMap frameworkFields = frameworkFields(binding); TypeSpec.Builder injectorTypeBuilder = @@ -323,7 +324,7 @@ private MethodSpec injectMembersMethod( .addCode( InjectionSiteMethod.invokeAll( binding.injectionSites(), - membersInjectorNameForType(binding.membersInjectedType()), + toJavaPoet(membersInjectorNameForType(binding.membersInjectedType())), CodeBlock.of("instance"), instanceType, dependencyCodeBlocks::get)) @@ -352,7 +353,8 @@ private AnnotationSpec qualifierMetadataAnnotation(MembersInjectionBinding bindi private static ImmutableMap frameworkFields( MembersInjectionBinding binding) { UniqueNameSet fieldNames = new UniqueNameSet(); - ClassName membersInjectorTypeName = membersInjectorNameForType(binding.membersInjectedType()); + ClassName membersInjectorTypeName = + toJavaPoet(membersInjectorNameForType(binding.membersInjectedType())); ImmutableMap.Builder builder = ImmutableMap.builder(); generateBindingFieldsForDependencies(binding) .forEach( diff --git a/java/dagger/internal/codegen/writing/MembersInjectorProviderCreationExpression.java b/java/dagger/internal/codegen/writing/MembersInjectorProviderCreationExpression.java index 7465820e613..5b9d348e511 100644 --- a/java/dagger/internal/codegen/writing/MembersInjectorProviderCreationExpression.java +++ b/java/dagger/internal/codegen/writing/MembersInjectorProviderCreationExpression.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.collect.Iterables.getOnlyElement; import static dagger.internal.codegen.binding.SourceFiles.membersInjectorNameForType; @@ -73,7 +74,7 @@ public CodeBlock creationExpression() { membersInjector = CodeBlock.of( "$T.create($L)", - membersInjectorNameForType(injectedTypeElement), + toJavaPoet(membersInjectorNameForType(injectedTypeElement)), componentRequestRepresentations.getCreateMethodArgumentsCodeBlock( binding, shardImplementation.name())); } diff --git a/java/dagger/internal/codegen/writing/ModuleProxies.java b/java/dagger/internal/codegen/writing/ModuleProxies.java index eaf57ee805a..772cc636232 100644 --- a/java/dagger/internal/codegen/writing/ModuleProxies.java +++ b/java/dagger/internal/codegen/writing/ModuleProxies.java @@ -16,9 +16,11 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static com.squareup.javapoet.MethodSpec.constructorBuilder; import static com.squareup.javapoet.MethodSpec.methodBuilder; import static com.squareup.javapoet.TypeSpec.classBuilder; +import static dagger.internal.codegen.binding.SourceFiles.classFileName; import static dagger.internal.codegen.langmodel.Accessibility.isElementAccessibleFrom; import static dagger.internal.codegen.xprocessing.XTypeElements.isNested; import static javax.lang.model.element.Modifier.FINAL; @@ -26,6 +28,7 @@ import static javax.lang.model.element.Modifier.PUBLIC; import static javax.lang.model.element.Modifier.STATIC; +import androidx.room.compiler.codegen.XClassName; import androidx.room.compiler.processing.XConstructorElement; import androidx.room.compiler.processing.XElement; import androidx.room.compiler.processing.XFiler; @@ -37,7 +40,6 @@ import com.squareup.javapoet.TypeSpec; import dagger.internal.codegen.base.ModuleKind; import dagger.internal.codegen.base.SourceFileGenerator; -import dagger.internal.codegen.binding.SourceFiles; import dagger.internal.codegen.langmodel.Accessibility; import java.util.Optional; import javax.inject.Inject; @@ -86,10 +88,10 @@ private TypeSpec.Builder buildProxy(XTypeElement moduleElement) { /** The name of the class that hosts the module constructor proxy method. */ private static ClassName constructorProxyTypeName(XTypeElement moduleElement) { ModuleKind.checkIsModule(moduleElement); - ClassName moduleClassName = moduleElement.getClassName(); - return moduleClassName + XClassName moduleClassName = moduleElement.asClassName(); + return toJavaPoet(moduleClassName) .topLevelClassName() - .peerClass(SourceFiles.classFileName(moduleClassName) + "_Proxy"); + .peerClass(classFileName(moduleClassName) + "_Proxy"); } /** diff --git a/java/dagger/internal/codegen/writing/ProducerCreationExpression.java b/java/dagger/internal/codegen/writing/ProducerCreationExpression.java index a26c8841cd8..4f15f632e5f 100644 --- a/java/dagger/internal/codegen/writing/ProducerCreationExpression.java +++ b/java/dagger/internal/codegen/writing/ProducerCreationExpression.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static com.google.common.base.Preconditions.checkNotNull; import static dagger.internal.codegen.binding.SourceFiles.generatedClassNameForBinding; @@ -52,7 +53,7 @@ final class ProducerCreationExpression implements FrameworkInstanceCreationExpre public CodeBlock creationExpression() { return CodeBlock.of( "$T.create($L)", - generatedClassNameForBinding(binding), + toJavaPoet(generatedClassNameForBinding(binding)), componentRequestRepresentations.getCreateMethodArgumentsCodeBlock( binding, shardImplementation.name())); } diff --git a/java/dagger/internal/codegen/writing/ProducerFactoryGenerator.java b/java/dagger/internal/codegen/writing/ProducerFactoryGenerator.java index d38da63a2e0..a588c2dca5c 100644 --- a/java/dagger/internal/codegen/writing/ProducerFactoryGenerator.java +++ b/java/dagger/internal/codegen/writing/ProducerFactoryGenerator.java @@ -114,7 +114,7 @@ public ImmutableList topLevelTypes(ProductionBinding binding) FactoryFields factoryFields = FactoryFields.create(binding); TypeSpec.Builder factoryBuilder = - classBuilder(generatedClassNameForBinding(binding)) + classBuilder(toJavaPoet(generatedClassNameForBinding(binding))) .superclass( ParameterizedTypeName.get( TypeNames.ABSTRACT_PRODUCES_METHOD_PRODUCER, @@ -158,7 +158,7 @@ private MethodSpec constructorMethod(ProductionBinding binding, FactoryFields fa constructorBuilder.addStatement( "super($N, $L, $N)", factoryFields.monitorField, - producerTokenConstruction(generatedClassNameForBinding(binding), binding), + producerTokenConstruction(toJavaPoet(generatedClassNameForBinding(binding)), binding), factoryFields.executorField); factoryFields.getAll() .forEach( diff --git a/java/dagger/internal/codegen/writing/SetFactoryCreationExpression.java b/java/dagger/internal/codegen/writing/SetFactoryCreationExpression.java index e5e464922e9..25b9fe18bbd 100644 --- a/java/dagger/internal/codegen/writing/SetFactoryCreationExpression.java +++ b/java/dagger/internal/codegen/writing/SetFactoryCreationExpression.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet; import static com.google.common.base.Preconditions.checkNotNull; import static dagger.internal.codegen.binding.SourceFiles.setFactoryClassName; @@ -49,7 +50,8 @@ final class SetFactoryCreationExpression extends MultibindingFactoryCreationExpr @Override public CodeBlock creationExpression() { - CodeBlock.Builder builder = CodeBlock.builder().add("$T.", setFactoryClassName(binding)); + CodeBlock.Builder builder = + CodeBlock.builder().add("$T.", toJavaPoet(setFactoryClassName(binding))); if (!useRawType()) { SetType setType = SetType.from(binding.key()); builder.add( diff --git a/java/dagger/internal/codegen/writing/StaticMemberSelects.java b/java/dagger/internal/codegen/writing/StaticMemberSelects.java index b664f8e716d..7b72316bd56 100644 --- a/java/dagger/internal/codegen/writing/StaticMemberSelects.java +++ b/java/dagger/internal/codegen/writing/StaticMemberSelects.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.writing; +import static androidx.room.compiler.codegen.compat.XConverters.toJavaPoet; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static dagger.internal.codegen.binding.SourceFiles.bindingTypeElementTypeVariableNames; @@ -63,7 +64,7 @@ static MemberSelect emptyMapFactory(MultiboundMapBinding binding) { */ static MemberSelect emptySetFactory(MultiboundSetBinding binding) { return new ParameterizedStaticMethod( - setFactoryClassName(binding), + toJavaPoet(setFactoryClassName(binding)), ImmutableList.of(SetType.from(binding.key()).elementType()), CodeBlock.of("empty()"), FACTORY); @@ -83,7 +84,7 @@ static MemberSelect factoryCreateNoArgumentMethod(Binding binding) { "%s should have no dependencies and be unscoped to create a no argument factory.", binding); - ClassName factoryName = generatedClassNameForBinding(binding); + ClassName factoryName = toJavaPoet(generatedClassNameForBinding(binding)); XType keyType = binding.key().type().xprocessing(); if (isDeclared(keyType)) { ImmutableList typeVariables = bindingTypeElementTypeVariableNames(binding); diff --git a/java/dagger/internal/codegen/xprocessing/XTypeNames.java b/java/dagger/internal/codegen/xprocessing/XTypeNames.java index b9f1136259f..d9c7213c002 100644 --- a/java/dagger/internal/codegen/xprocessing/XTypeNames.java +++ b/java/dagger/internal/codegen/xprocessing/XTypeNames.java @@ -16,6 +16,8 @@ package dagger.internal.codegen.xprocessing; +import static com.google.common.collect.Iterables.getLast; + import androidx.room.compiler.codegen.XClassName; import androidx.room.compiler.codegen.XTypeName; import androidx.room.compiler.processing.XType; @@ -283,5 +285,9 @@ public static boolean isFutureType(XTypeName typeName) { return FUTURE_TYPES.contains(typeName.getRawTypeName()); } + public static String simpleName(XClassName className) { + return getLast(className.getSimpleNames()); + } + private XTypeNames() {} } diff --git a/javatests/dagger/internal/codegen/SourceFilesTest.java b/javatests/dagger/internal/codegen/SourceFilesTest.java index c2f7dfd6b98..55e4ce5777f 100644 --- a/javatests/dagger/internal/codegen/SourceFilesTest.java +++ b/javatests/dagger/internal/codegen/SourceFilesTest.java @@ -17,9 +17,8 @@ package dagger.internal.codegen; import static com.google.common.truth.Truth.assertThat; -import static dagger.internal.codegen.binding.SourceFiles.simpleVariableName; -import com.squareup.javapoet.ClassName; +import androidx.room.compiler.codegen.XClassName; import dagger.internal.codegen.binding.SourceFiles; import java.util.List; import org.junit.Test; @@ -34,15 +33,15 @@ private static final class Int {} @Test public void testSimpleVariableName_typeCollisions() { // a handful of boxed types - assertThat(simpleVariableName(ClassName.get(Long.class))).isEqualTo("l"); - assertThat(simpleVariableName(ClassName.get(Double.class))).isEqualTo("d"); + assertThat(simpleVariableName(Long.class)).isEqualTo("l"); + assertThat(simpleVariableName(Double.class)).isEqualTo("d"); // not a boxed type type, but a custom type might collide - assertThat(simpleVariableName(ClassName.get(Int.class))).isEqualTo("i"); + assertThat(simpleVariableName(Int.class)).isEqualTo("i"); // void is the weird pseudo-boxed type - assertThat(simpleVariableName(ClassName.get(Void.class))).isEqualTo("v"); + assertThat(simpleVariableName(Void.class)).isEqualTo("v"); // reflective types - assertThat(simpleVariableName(ClassName.get(Class.class))).isEqualTo("clazz"); - assertThat(simpleVariableName(ClassName.get(Package.class))).isEqualTo("pkg"); + assertThat(simpleVariableName(Class.class)).isEqualTo("clazz"); + assertThat(simpleVariableName(Package.class)).isEqualTo("pkg"); } private static final class For {} @@ -51,13 +50,18 @@ private static final class Goto {} @Test public void testSimpleVariableName_randomKeywords() { - assertThat(simpleVariableName(ClassName.get(For.class))).isEqualTo("for_"); - assertThat(simpleVariableName(ClassName.get(Goto.class))).isEqualTo("goto_"); + assertThat(simpleVariableName(For.class)).isEqualTo("for_"); + assertThat(simpleVariableName(Goto.class)).isEqualTo("goto_"); } @Test public void testSimpleVariableName() { - assertThat(simpleVariableName(ClassName.get(Object.class))).isEqualTo("object"); - assertThat(simpleVariableName(ClassName.get(List.class))).isEqualTo("list"); + assertThat(simpleVariableName(Object.class)).isEqualTo("object"); + assertThat(simpleVariableName(List.class)).isEqualTo("list"); + } + + private static String simpleVariableName(Class clazz) { + return SourceFiles.simpleVariableName( + XClassName.Companion.get(clazz.getPackageName(), clazz.getSimpleName())); } }