Skip to content

Commit

Permalink
Fix issues caused by XProcessing getName().
Browse files Browse the repository at this point in the history
This CL replaces all usages of XProcessing getName() with a utility class XElements#getSimpleName(XElement) which ensures we use the javac simple name for now.

Fixes #3065

RELNOTES=N/A
PiperOrigin-RevId: 412072237
  • Loading branch information
bcorso authored and Dagger Team committed Nov 24, 2021
1 parent 5c1164a commit 88c9209
Show file tree
Hide file tree
Showing 27 changed files with 98 additions and 34 deletions.
Expand Up @@ -26,6 +26,7 @@
import static dagger.internal.codegen.javapoet.TypeNames.INSTANCE_FACTORY;
import static dagger.internal.codegen.javapoet.TypeNames.providerOf;
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XMethodElements.hasTypeParameters;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -195,7 +196,7 @@ ValidationReport validate(XTypeElement factory) {
metadata.factory().getQualifiedName(),
metadata.factoryMethod(),
metadata.factory().getQualifiedName(),
metadata.factoryMethod().getName(),
getSimpleName(metadata.factoryMethod()),
metadata.assistedInjectAssistedParameters().stream()
.map(AssistedParameter::type)
.map(Object::toString)
Expand Down Expand Up @@ -298,7 +299,7 @@ public ImmutableList<TypeSpec.Builder> topLevelTypes(ProvisionBinding binding) {
// use the parameter names of the @AssistedFactory method.
metadata.assistedInjectAssistedParameters().stream()
.map(metadata.assistedFactoryAssistedParametersMap()::get)
.map(param -> CodeBlock.of("$L", param.getName()))
.map(param -> CodeBlock.of("$L", getSimpleName(param)))
.collect(toParametersCodeBlock()))
.build())
.addMethod(
Expand Down
3 changes: 2 additions & 1 deletion java/dagger/internal/codegen/AssistedProcessingStep.java
Expand Up @@ -22,6 +22,7 @@
import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.isAssistedFactoryType;
import static dagger.internal.codegen.xprocessing.XElements.asMethod;
import static dagger.internal.codegen.xprocessing.XElements.closestEnclosingTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;

import androidx.room.compiler.processing.XExecutableElement;
import androidx.room.compiler.processing.XExecutableParameterElement;
Expand Down Expand Up @@ -110,7 +111,7 @@ private boolean isKotlinDataClassCopyMethod(XExecutableElement executableElement
// an @Assisted annotation that has no affect, which is already true for many of Dagger's other
// annotations.
return isMethod(executableElement)
&& asMethod(executableElement).getName().contentEquals("copy")
&& getSimpleName(asMethod(executableElement)).contentEquals("copy")
&& closestEnclosingTypeElement(executableElement.getEnclosingElement()).isDataClass();
}
}
Expand Up @@ -24,6 +24,7 @@
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.xprocessing.XElements.asConstructor;
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static javax.lang.model.util.ElementFilter.constructorsIn;
import static kotlin.streams.jdk8.StreamsKt.asStream;

Expand Down Expand Up @@ -117,7 +118,7 @@ private static ImmutableList<ParameterSpec> assistedParameterSpecs(
XType paramType = paramTypes.get(i);
if (isAssistedParameter(paramElement)) {
assistedParameterSpecs.add(
ParameterSpec.builder(paramType.getTypeName(), paramElement.getName()).build());
ParameterSpec.builder(paramType.getTypeName(), getSimpleName(paramElement)).build());
}
}
return assistedParameterSpecs.build();
Expand Down
Expand Up @@ -22,6 +22,7 @@
import static dagger.internal.codegen.base.MoreAnnotationMirrors.getTypeListValue;
import static dagger.internal.codegen.binding.ComponentCreatorAnnotation.subcomponentCreatorAnnotations;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.xprocessing.XAnnotations.getClassName;
import static dagger.internal.codegen.xprocessing.XElements.hasAnyAnnotation;

import androidx.room.compiler.processing.XAnnotation;
Expand Down Expand Up @@ -67,7 +68,7 @@ public static ImmutableList<TypeMirror> getModuleInjects(AnnotationMirror module
/** Returns the first type that specifies this' nullability, or empty if none. */
public static Optional<XAnnotation> getNullableAnnotation(XElement element) {
return element.getAllAnnotations().stream()
.filter(annotation -> annotation.getName().contentEquals("Nullable"))
.filter(annotation -> getClassName(annotation).simpleName().contentEquals("Nullable"))
.findFirst();
}

Expand Down
4 changes: 3 additions & 1 deletion java/dagger/internal/codegen/binding/ErrorMessages.java
Expand Up @@ -16,6 +16,8 @@

package dagger.internal.codegen.binding;

import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;

import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XType;
import androidx.room.compiler.processing.XTypeElement;
Expand Down Expand Up @@ -209,7 +211,7 @@ public final String factoryMethodReturnsSupertypeWithMissingMethods(
+ "order to provide type-safe access to these methods, override %2$s() to return "
+ "%4$s",
componentBuilder.getQualifiedName(),
buildMethod.getName(),
getSimpleName(buildMethod),
returnType.getTypeName(),
component.getQualifiedName(),
Joiner.on(", ").join(additionalMethods));
Expand Down
6 changes: 5 additions & 1 deletion java/dagger/internal/codegen/binding/MapKeys.java
Expand Up @@ -27,6 +27,7 @@
import static com.squareup.javapoet.MethodSpec.methodBuilder;
import static dagger.internal.codegen.base.MapKeyAccessibility.isMapKeyPubliclyAccessible;
import static dagger.internal.codegen.binding.SourceFiles.elementBasedClassName;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import static dagger.internal.codegen.xprocessing.XTypes.isPrimitive;
import static javax.lang.model.element.Modifier.PUBLIC;
Expand Down Expand Up @@ -135,7 +136,10 @@ public static XType getUnwrappedMapKeyType(XType mapKeyAnnotationType) {
XType annotationValueType = annotationValueMethod.getReturnType();
if (isArray(annotationValueType)) {
throw new IllegalArgumentException(
mapKeyAnnotationType + "." + annotationValueMethod.getName() + " cannot be an array");
mapKeyAnnotationType
+ "."
+ getSimpleName(annotationValueMethod)
+ " cannot be an array");
}
return isPrimitive(annotationValueType) ? annotationValueType.boxed() : annotationValueType;
}
Expand Down
4 changes: 3 additions & 1 deletion java/dagger/internal/codegen/binding/ModuleDescriptor.java
Expand Up @@ -30,6 +30,7 @@
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.langmodel.DaggerElements.checkTypePresent;
import static dagger.internal.codegen.langmodel.DaggerElements.getMethodDescriptor;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import static javax.lang.model.util.ElementFilter.methodsIn;

Expand Down Expand Up @@ -270,7 +271,8 @@ private ClassName implicitlyIncludedModuleName(XTypeElement module, XMethodEleme
module.getPackageName(),
String.format(
"%s_%s",
classFileName(module.getClassName()), LOWER_CAMEL.to(UPPER_CAMEL, method.getName())));
classFileName(module.getClassName()),
LOWER_CAMEL.to(UPPER_CAMEL, getSimpleName(method))));
}

@Override
Expand Down
Expand Up @@ -18,6 +18,7 @@

import static dagger.internal.codegen.binding.ConfigurationAnnotations.getSubcomponentCreator;
import static dagger.internal.codegen.extension.DaggerCollectors.toOptional;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XTypeElement;
Expand Down Expand Up @@ -71,7 +72,7 @@ ImmutableSet<SubcomponentDeclaration> forModule(XTypeElement module) {
ModuleAnnotation moduleAnnotation = ModuleAnnotation.moduleAnnotation(module).get();
XElement subcomponentAttribute =
moduleAnnotation.annotation().getType().getTypeElement().getDeclaredMethods().stream()
.filter(method -> method.getName().contentEquals("subcomponents"))
.filter(method -> getSimpleName(method).contentEquals("subcomponents"))
.collect(toOptional())
.get();

Expand Down
Expand Up @@ -26,6 +26,7 @@
import static dagger.internal.codegen.javapoet.TypeSpecs.addSupertype;
import static dagger.internal.codegen.langmodel.Accessibility.isElementAccessibleFrom;
import static dagger.internal.codegen.writing.ComponentNames.getRootComponentClassName;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XTypeElements.getAllUnimplementedMethods;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
Expand Down Expand Up @@ -212,7 +213,7 @@ private static boolean isBindsInstance(XMethodElement method) {

private static MethodSpec builderSetterMethod(
XTypeElement componentRequirement, ClassName builderClass) {
String simpleName = UPPER_CAMEL.to(LOWER_CAMEL, componentRequirement.getName());
String simpleName = UPPER_CAMEL.to(LOWER_CAMEL, getSimpleName(componentRequirement));
return MethodSpec.methodBuilder(simpleName)
.addModifiers(PUBLIC)
.addParameter(componentRequirement.getClassName(), simpleName)
Expand Down
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.collect.Iterables.getOnlyElement;
import static dagger.internal.codegen.base.Util.reentrantComputeIfAbsent;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XElements.hasAnyAnnotation;
import static java.util.stream.Collectors.joining;

Expand Down Expand Up @@ -105,7 +106,7 @@ private ValidationReport validateUncached(XMethodElement method) {
report.addError(
String.format(
"%s is annotated with more than one of (%s)",
method.getName(),
getSimpleName(method),
methodAnnotations().stream().map(ClassName::canonicalName).collect(joining(", "))),
method);
break;
Expand Down
Expand Up @@ -32,6 +32,7 @@
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSetMultimap;
import static dagger.internal.codegen.xprocessing.XElements.asMethod;
import static dagger.internal.codegen.xprocessing.XElements.asMethodParameter;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static javax.tools.Diagnostic.Kind.ERROR;
Expand Down Expand Up @@ -376,7 +377,7 @@ private String formatParameter(XExecutableParameterElement parameter, XType cont
XType parameterType = resolveParameterType(parameter, container);
return joiner
.add(stripCommonTypePrefixes(parameterType.getTypeName().toString()))
.add(parameter.getName())
.add(getSimpleName(parameter))
.toString();
}

Expand Down
Expand Up @@ -25,6 +25,7 @@
import static dagger.internal.codegen.base.Scopes.getReadableSource;
import static dagger.internal.codegen.base.Scopes.uniqueScopeOf;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;

import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XTypeElement;
Expand Down Expand Up @@ -127,7 +128,8 @@ private void validateFactoryMethodParameters(
String.format(
"%s is present in %s. A subcomponent cannot use an instance of a "
+ "module that differs from its parent.",
moduleType.getName(), existingModuleToOwners.get(moduleType).getQualifiedName()),
getSimpleName(moduleType),
existingModuleToOwners.get(moduleType).getQualifiedName()),
factoryMethodParameter);
}
}
Expand Down
Expand Up @@ -38,6 +38,7 @@
import static dagger.internal.codegen.xprocessing.XElements.asMethod;
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.getAnyAnnotation;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XTypeElements.getAllUnimplementedMethods;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import static java.util.Comparator.comparing;
Expand Down Expand Up @@ -452,7 +453,8 @@ private void validateNoConflictingEntryPoints() {
methodsIn(elements.getAllMembers(toJavac(component))).stream()
.map(method -> asMethod(toXProcessing(method, processingEnv)))
.filter(method -> isEntryPoint(method, method.asMemberOf(component.getType())))
.forEach(method -> addMethodUnlessOverridden(method, entryPoints.get(method.getName())));
.forEach(
method -> addMethodUnlessOverridden(method, entryPoints.get(getSimpleName(method))));

asMap(entryPoints).values().stream()
.filter(methods -> distinctKeys(methods).size() > 1)
Expand Down
Expand Up @@ -25,6 +25,7 @@
import static dagger.internal.codegen.binding.SourceFiles.membersInjectorNameForType;
import static dagger.internal.codegen.xprocessing.XElements.asField;
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import static dagger.internal.codegen.xprocessing.XTypes.isTypeOf;
import static dagger.internal.codegen.xprocessing.XTypes.isWildcard;
Expand Down Expand Up @@ -191,7 +192,7 @@ void checkNotProducer(ValidationReport.Builder report, XVariableElement requestE
report.addError(
String.format(
"%s may only be injected in @Produces methods",
requestType.getTypeElement().getName()),
getSimpleName(requestType.getTypeElement())),
requestElement);
}
}
Expand Down
15 changes: 9 additions & 6 deletions java/dagger/internal/codegen/validation/ModuleValidator.java
Expand Up @@ -26,7 +26,9 @@
import static dagger.internal.codegen.binding.ConfigurationAnnotations.getSubcomponentCreator;
import static dagger.internal.codegen.extension.DaggerCollectors.toOptional;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.xprocessing.XAnnotations.getClassName;
import static dagger.internal.codegen.xprocessing.XElements.getAnnotatedAnnotations;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XElements.hasAnyAnnotation;
import static dagger.internal.codegen.xprocessing.XTypeElements.hasTypeParameters;
import static dagger.internal.codegen.xprocessing.XTypeElements.isEffectivelyPrivate;
Expand Down Expand Up @@ -58,6 +60,7 @@
import dagger.internal.codegen.binding.MethodSignatureFormatter;
import dagger.internal.codegen.binding.ModuleKind;
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.xprocessing.XElements;
import dagger.spi.model.BindingGraph;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -195,14 +198,14 @@ && areEquivalentTypes(contributesAndroidInjector.get(), annotation.getType())) {
validateModuleVisibility(module, moduleKind, builder);

ImmutableListMultimap<String, XMethodElement> bindingMethodsByName =
Multimaps.index(bindingMethods, XMethodElement::getName);
Multimaps.index(bindingMethods, XElements::getSimpleName);

validateMethodsWithSameName(builder, bindingMethodsByName);
if (!module.isInterface()) {
validateBindingMethodOverrides(
module,
builder,
Multimaps.index(moduleMethods, XMethodElement::getName),
Multimaps.index(moduleMethods, XElements::getSimpleName),
bindingMethodsByName);
}
validateModifiers(module, builder);
Expand Down Expand Up @@ -292,7 +295,7 @@ private static String moduleSubcomponentsDoesntHaveCreator(
+ "@%3$s.subcomponents",
subcomponent.getQualifiedName(),
subcomponentAnnotation(subcomponent).get().simpleName(),
moduleAnnotation.getName());
getClassName(moduleAnnotation).simpleName());
}

enum ModuleMethodKind {
Expand Down Expand Up @@ -465,7 +468,7 @@ private void validateBindingMethodOverrides(
currentClass = currentClass.getSuperType().getTypeElement();
List<XMethodElement> superclassMethods = currentClass.getDeclaredMethods();
for (XMethodElement superclassMethod : superclassMethods) {
String name = superclassMethod.getName();
String name = getSimpleName(superclassMethod);
// For each method in the superclass, confirm our binding methods don't override it
for (XMethodElement bindingMethod : bindingMethodsByName.get(name)) {
if (visitedMethods.add(bindingMethod)
Expand All @@ -490,7 +493,7 @@ private void validateBindingMethodOverrides(
}
}
// TODO(b/202521399): Add a test for cases that add to this map.
allMethodsByName.put(superclassMethod.getName(), superclassMethod);
allMethodsByName.put(getSimpleName(superclassMethod), superclassMethod);
}
}
}
Expand Down Expand Up @@ -603,7 +606,7 @@ private void validateCompanionModule(
}

ImmutableListMultimap<String, XMethodElement> bindingMethodsByName =
Multimaps.index(companionBindingMethods, XMethodElement::getName);
Multimaps.index(companionBindingMethods, XElements::getSimpleName);
validateMethodsWithSameName(builder, bindingMethodsByName);

// If there are provision methods, then check the visibility. Companion objects are composed by
Expand Down
Expand Up @@ -90,7 +90,7 @@ private ImmutableMap<XElement, ImmutableSet<ClassName>> inverse(
checkState(
annotationClassNames.keySet().containsAll(elementsByAnnotation.keySet()),
"Unexpected annotations for %s: %s",
this.getClass().getName(),
this.getClass().getCanonicalName(),
difference(elementsByAnnotation.keySet(), annotationClassNames.keySet()));

ImmutableSetMultimap.Builder<XElement, ClassName> builder = ImmutableSetMultimap.builder();
Expand Down
Expand Up @@ -22,6 +22,7 @@
import static dagger.internal.codegen.binding.AnnotationExpression.createMethodName;
import static dagger.internal.codegen.binding.AnnotationExpression.getAnnotationCreatorClassName;
import static dagger.internal.codegen.javapoet.CodeBlocks.makeParametersCodeBlock;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PUBLIC;
Expand Down Expand Up @@ -114,7 +115,7 @@ private MethodSpec buildCreateMethod(

ImmutableList.Builder<CodeBlock> parameters = ImmutableList.builder();
for (XMethodElement annotationMember : annotationElement.getDeclaredMethods()) {
String parameterName = annotationMember.getName();
String parameterName = getSimpleName(annotationMember);
TypeName parameterType = annotationMember.getReturnType().getTypeName();
createMethod.addParameter(parameterType, parameterName);
parameters.add(CodeBlock.of("$L", parameterName));
Expand Down
Expand Up @@ -22,6 +22,7 @@
import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.assistedFactoryMethod;
import static dagger.internal.codegen.writing.AssistedInjectionParameters.assistedFactoryParameterSpecs;
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;

import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XType;
Expand Down Expand Up @@ -92,7 +93,7 @@ private TypeSpec anonymousfactoryImpl(
TypeSpec.Builder builder =
TypeSpec.anonymousClassBuilder("")
.addMethod(
MethodSpec.methodBuilder(factoryMethod.getName())
MethodSpec.methodBuilder(getSimpleName(factoryMethod))
.addModifiers(factoryOverride.modifiers)
.addTypeVariables(factoryOverride.typeVariables)
.returns(factoryOverride.returnType)
Expand Down

0 comments on commit 88c9209

Please sign in to comment.