Skip to content

Commit

Permalink
Migrating ClassName to XClassName as part of the migration to XPoet.
Browse files Browse the repository at this point in the history
There's more to migrate, this is only a first pass migrating most of SourceFiles and the APIs that call into it.

RELNOTES=N/A
PiperOrigin-RevId: 710093844
  • Loading branch information
bcorso authored and Dagger Team committed Dec 27, 2024
1 parent e9e8deb commit 3ac3681
Show file tree
Hide file tree
Showing 29 changed files with 169 additions and 130 deletions.
17 changes: 9 additions & 8 deletions java/dagger/internal/codegen/binding/AnnotationExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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()
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -245,7 +246,8 @@ private ImmutableSet<ModuleDescriptor> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 6 additions & 5 deletions java/dagger/internal/codegen/binding/InjectionAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -165,10 +166,10 @@ private Optional<ImmutableSet<Scope>> getScopesWithMetadata(XElement element) {
private Optional<XAnnotation> 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<ClassName> getGeneratedNameForScopeMetadata(XElement element) {
private Optional<XClassName> getGeneratedNameForScopeMetadata(XElement element) {
// Currently, we only support ScopeMetadata for inject-constructor types and provides methods.
if (isTypeElement(element)) {
return asTypeElement(element).getConstructors().stream()
Expand Down Expand Up @@ -297,10 +298,10 @@ private Optional<ImmutableSet<XAnnotation>> getQualifiersWithMetadata(XElement e
private Optional<XAnnotation> 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<ClassName> getGeneratedNameForQualifierMetadata(XElement element) {
private Optional<XClassName> 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)) {
Expand Down
6 changes: 4 additions & 2 deletions java/dagger/internal/codegen/binding/MapKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}

/**
Expand Down Expand Up @@ -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() {}
Expand Down
2 changes: 1 addition & 1 deletion java/dagger/internal/codegen/binding/ModuleDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))));
}

Expand Down
75 changes: 35 additions & 40 deletions java/dagger/internal/codegen/binding/SourceFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,24 @@

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;
import static dagger.internal.codegen.xprocessing.XElements.asMethod;
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;
Expand All @@ -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;

Expand Down Expand Up @@ -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:
Expand All @@ -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.
*
* <p>This will always return a {@linkplain ClassName#topLevelClassName() top level class name},
* even if {@code element}'s enclosing class is a nested type.
* <p>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<TypeVariableName> 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");
}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -266,32 +261,33 @@ private static ClassName siblingClassName(XTypeElement typeElement, String suffi
* {@code Set<Produced<T>>}.
* </ul>
*/
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());
}
Expand All @@ -317,16 +313,15 @@ public static ImmutableList<TypeVariableName> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,7 +91,7 @@ public XElement originatingElement(ComponentDescriptor input) {

@Override
public ImmutableList<TypeSpec.Builder> topLevelTypes(ComponentDescriptor componentDescriptor) {
ClassName generatedTypeName = getTopLevelClassName(componentDescriptor);
ClassName generatedTypeName = toJavaPoet(getTopLevelClassName(componentDescriptor));
TypeSpec.Builder generatedComponent =
TypeSpec.classBuilder(generatedTypeName)
.addModifiers(FINAL)
Expand Down
Loading

0 comments on commit 3ac3681

Please sign in to comment.