Skip to content

Commit

Permalink
Replace DaggerElements and DaggerTypes with XProcessingEnv.
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 444692995
  • Loading branch information
bcorso authored and Dagger Team committed Apr 26, 2022
1 parent 117d54e commit 6357c0e
Show file tree
Hide file tree
Showing 42 changed files with 278 additions and 915 deletions.
10 changes: 5 additions & 5 deletions java/dagger/internal/codegen/AssistedFactoryProcessingStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static dagger.internal.codegen.javapoet.CodeBlocks.toParametersCodeBlock;
import static dagger.internal.codegen.javapoet.TypeNames.INSTANCE_FACTORY;
import static dagger.internal.codegen.javapoet.TypeNames.providerOf;
import static dagger.internal.codegen.langmodel.Accessibility.accessibleType;
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XMethodElements.hasTypeParameters;
Expand Down Expand Up @@ -62,7 +63,6 @@
import dagger.internal.codegen.binding.BindingFactory;
import dagger.internal.codegen.binding.ProvisionBinding;
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.langmodel.DaggerTypes;
import dagger.internal.codegen.validation.SuperficialValidator;
import dagger.internal.codegen.validation.TypeCheckingProcessingStep;
import dagger.internal.codegen.validation.ValidationReport;
Expand All @@ -77,7 +77,6 @@ final class AssistedFactoryProcessingStep extends TypeCheckingProcessingStep<XTy
private final XProcessingEnv processingEnv;
private final XMessager messager;
private final XFiler filer;
private final DaggerTypes types;
private final BindingFactory bindingFactory;
private final SuperficialValidator superficialValidator;

Expand All @@ -86,13 +85,11 @@ final class AssistedFactoryProcessingStep extends TypeCheckingProcessingStep<XTy
XProcessingEnv processingEnv,
XMessager messager,
XFiler filer,
DaggerTypes types,
BindingFactory bindingFactory,
SuperficialValidator superficialValidator) {
this.processingEnv = processingEnv;
this.messager = messager;
this.filer = filer;
this.types = types;
this.bindingFactory = bindingFactory;
this.superficialValidator = superficialValidator;
}
Expand Down Expand Up @@ -323,7 +320,10 @@ public ImmutableList<TypeSpec.Builder> topLevelTypes(ProvisionBinding binding) {
INSTANCE_FACTORY,
// Java 7 type inference requires the method call provide the exact type here.
isPreJava8SourceVersion(processingEnv)
? CodeBlock.of("<$T>", types.accessibleType(metadata.factoryType(), name))
? CodeBlock.of(
"<$T>",
accessibleType(metadata.factoryType(), name, processingEnv)
.getTypeName())
: CodeBlock.of(""),
name,
delegateFactoryParam)
Expand Down
23 changes: 0 additions & 23 deletions java/dagger/internal/codegen/ProcessingEnvironmentModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@
import dagger.Module;
import dagger.Provides;
import dagger.Reusable;
import dagger.internal.codegen.base.ClearableCache;
import dagger.internal.codegen.compileroption.CompilerOptions;
import dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions;
import dagger.internal.codegen.compileroption.ProcessingOptions;
import dagger.internal.codegen.langmodel.DaggerElements;
import dagger.internal.codegen.langmodel.DaggerTypes;
import dagger.multibindings.IntoSet;
import java.util.Map;
import javax.inject.Singleton;

/** Bindings that depend on the {@link XProcessingEnv}. */
@Module
Expand Down Expand Up @@ -61,22 +56,4 @@ static XFiler filer(CompilerOptions compilerOptions, XProcessingEnv xProcessingE
: XConverters.toXProcessing(
new FormattingFiler(XConverters.toJavac(xProcessingEnv.getFiler())), xProcessingEnv);
}

@Provides
@Singleton
static DaggerElements daggerElements(XProcessingEnv xProcessingEnv) {
return new DaggerElements(
XConverters.toJavac(xProcessingEnv).getElementUtils(),
XConverters.toJavac(xProcessingEnv).getTypeUtils());
}

@Provides
@Singleton
static DaggerTypes daggerTypes(XProcessingEnv xProcessingEnv, DaggerElements elements) {
return new DaggerTypes(XConverters.toJavac(xProcessingEnv).getTypeUtils(), elements);
}

@Binds
@IntoSet
ClearableCache daggerElementAsClearableCache(DaggerElements elements);
}
2 changes: 1 addition & 1 deletion java/dagger/internal/codegen/base/RequestKinds.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import static dagger.internal.codegen.javapoet.TypeNames.producedOf;
import static dagger.internal.codegen.javapoet.TypeNames.producerOf;
import static dagger.internal.codegen.javapoet.TypeNames.providerOf;
import static dagger.internal.codegen.langmodel.DaggerTypes.checkTypePresent;
import static dagger.internal.codegen.xprocessing.XProcessingEnvs.wrapType;
import static dagger.internal.codegen.xprocessing.XTypes.checkTypePresent;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import static dagger.internal.codegen.xprocessing.XTypes.isTypeOf;
import static dagger.internal.codegen.xprocessing.XTypes.unwrapType;
Expand Down
19 changes: 10 additions & 9 deletions java/dagger/internal/codegen/binding/BindingFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static androidx.room.compiler.processing.XElementKt.isMethod;
import static androidx.room.compiler.processing.XElementKt.isTypeElement;
import static androidx.room.compiler.processing.XElementKt.isVariableElement;
import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
Expand All @@ -31,6 +30,8 @@
import static dagger.internal.codegen.xprocessing.XElements.asMethod;
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.asVariable;
import static dagger.internal.codegen.xprocessing.XProcessingEnvs.erasure;
import static dagger.internal.codegen.xprocessing.XProcessingEnvs.isSameType;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import static dagger.spi.model.BindingKind.ASSISTED_FACTORY;
import static dagger.spi.model.BindingKind.ASSISTED_INJECTION;
Expand All @@ -53,6 +54,7 @@
import androidx.room.compiler.processing.XExecutableParameterElement;
import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XMethodType;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XType;
import androidx.room.compiler.processing.XTypeElement;
import androidx.room.compiler.processing.XVariableElement;
Expand All @@ -68,7 +70,6 @@
import dagger.internal.codegen.binding.MembersInjectionBinding.InjectionSite;
import dagger.internal.codegen.binding.ProductionBinding.ProductionKind;
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.langmodel.DaggerTypes;
import dagger.spi.model.BindingKind;
import dagger.spi.model.DaggerAnnotation;
import dagger.spi.model.DaggerType;
Expand All @@ -81,20 +82,20 @@

/** A factory for {@link Binding} objects. */
public final class BindingFactory {
private final DaggerTypes types;
private final XProcessingEnv processingEnv;
private final KeyFactory keyFactory;
private final DependencyRequestFactory dependencyRequestFactory;
private final InjectionSiteFactory injectionSiteFactory;
private final InjectionAnnotations injectionAnnotations;

@Inject
BindingFactory(
DaggerTypes types,
XProcessingEnv processingEnv,
KeyFactory keyFactory,
DependencyRequestFactory dependencyRequestFactory,
InjectionSiteFactory injectionSiteFactory,
InjectionAnnotations injectionAnnotations) {
this.types = types;
this.processingEnv = processingEnv;
this.keyFactory = keyFactory;
this.dependencyRequestFactory = dependencyRequestFactory;
this.injectionSiteFactory = injectionSiteFactory;
Expand Down Expand Up @@ -231,7 +232,7 @@ B setMethodBindingProperties(
Key key,
BiFunction<XMethodElement, XTypeElement, C> create) {
XMethodType methodType = method.asMemberOf(contributedBy.getType());
if (!types.isSameType(toJavac(methodType), toJavac(method.getExecutableType()))) {
if (!isSameType(methodType, method.getExecutableType(), processingEnv)) {
checkState(isTypeElement(method.getEnclosingElement()));
builder.unresolved(create.apply(method, asTypeElement(method.getEnclosingElement())));
}
Expand Down Expand Up @@ -536,10 +537,10 @@ public MembersInjectionBinding membersInjectionBinding(XType type, Optional<XTyp

private void checkIsSameErasedType(XType type1, XType type2) {
checkState(
types.isSameType(types.erasure(toJavac(type1)), types.erasure(toJavac(type2))),
erasure(type1, processingEnv).isSameType(erasure(type2, processingEnv)),
"erased expected type: %s, erased actual type: %s",
types.erasure(toJavac(type1)),
types.erasure(toJavac(type2)));
erasure(type1, processingEnv),
erasure(type2, processingEnv));
}

private static boolean hasNonDefaultTypeParameters(XType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import static dagger.internal.codegen.base.ComponentCreatorAnnotation.getCreatorAnnotations;
import static dagger.internal.codegen.base.ModuleAnnotation.moduleAnnotations;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.xprocessing.XProcessingEnvs.isSubtype;
import static dagger.internal.codegen.xprocessing.XTypeElements.getAllUnimplementedMethods;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XExecutableParameterElement;
import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XMethodType;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XType;
import androidx.room.compiler.processing.XTypeElement;
import com.google.auto.value.AutoValue;
Expand All @@ -39,7 +41,6 @@
import dagger.internal.codegen.base.ComponentCreatorAnnotation;
import dagger.internal.codegen.base.ComponentCreatorKind;
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.langmodel.DaggerTypes;
import dagger.spi.model.DependencyRequest;
import java.util.List;

Expand Down Expand Up @@ -152,15 +153,17 @@ final XElement elementForRequirement(ComponentRequirement requirement) {

/** Creates a new {@link ComponentCreatorDescriptor} for the given creator {@code type}. */
public static ComponentCreatorDescriptor create(
XTypeElement creator, DaggerTypes types, DependencyRequestFactory dependencyRequestFactory) {
XTypeElement creator,
XProcessingEnv processingEnv,
DependencyRequestFactory dependencyRequestFactory) {
XType componentType = creator.getEnclosingTypeElement().getType();

ImmutableSetMultimap.Builder<ComponentRequirement, XMethodElement> setterMethods =
ImmutableSetMultimap.builder();
XMethodElement factoryMethod = null;
for (XMethodElement method : getAllUnimplementedMethods(creator)) {
XMethodType resolvedMethodType = method.asMemberOf(creator.getType());
if (types.isSubtype(componentType, resolvedMethodType.getReturnType())) {
if (isSubtype(componentType, resolvedMethodType.getReturnType(), processingEnv)) {
verify(factoryMethod == null); // validation should have ensured there's only 1.
factoryMethod = method;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static com.google.common.base.Preconditions.checkState;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.langmodel.DaggerTypes.isFutureType;
import static dagger.internal.codegen.javapoet.TypeNames.isFutureType;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XTypes.isPrimitive;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import dagger.internal.codegen.base.DaggerSuperficialValidation;
import dagger.internal.codegen.base.ModuleAnnotation;
import dagger.internal.codegen.binding.ComponentDescriptor.ComponentMethodDescriptor;
import dagger.internal.codegen.langmodel.DaggerTypes;
import dagger.internal.codegen.xprocessing.XTypeElements;
import dagger.spi.model.Scope;
import java.util.Optional;
Expand All @@ -52,7 +51,6 @@
/** A factory for {@link ComponentDescriptor}s. */
public final class ComponentDescriptorFactory {
private final XProcessingEnv processingEnv;
private final DaggerTypes types;
private final DependencyRequestFactory dependencyRequestFactory;
private final ModuleDescriptor.Factory moduleDescriptorFactory;
private final InjectionAnnotations injectionAnnotations;
Expand All @@ -61,13 +59,11 @@ public final class ComponentDescriptorFactory {
@Inject
ComponentDescriptorFactory(
XProcessingEnv processingEnv,
DaggerTypes types,
DependencyRequestFactory dependencyRequestFactory,
ModuleDescriptor.Factory moduleDescriptorFactory,
InjectionAnnotations injectionAnnotations,
DaggerSuperficialValidation superficialValidation) {
this.processingEnv = processingEnv;
this.types = types;
this.dependencyRequestFactory = dependencyRequestFactory;
this.moduleDescriptorFactory = moduleDescriptorFactory;
this.injectionAnnotations = injectionAnnotations;
Expand Down Expand Up @@ -166,7 +162,7 @@ private ComponentDescriptor create(
? Optional.empty()
: Optional.of(
ComponentCreatorDescriptor.create(
getOnlyElement(enclosedCreators), types, dependencyRequestFactory));
getOnlyElement(enclosedCreators), processingEnv, dependencyRequestFactory));

ImmutableSet<Scope> scopes = injectionAnnotations.getScopes(typeElement);
if (componentAnnotation.isProduction()) {
Expand Down
25 changes: 12 additions & 13 deletions java/dagger/internal/codegen/binding/InjectionSiteFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,28 @@

import static androidx.room.compiler.processing.XElementKt.isField;
import static androidx.room.compiler.processing.XElementKt.isMethod;
import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static com.google.common.base.Preconditions.checkArgument;
import static dagger.internal.codegen.langmodel.DaggerElements.DECLARATION_ORDER;
import static dagger.internal.codegen.binding.SourceFiles.DECLARATION_ORDER;
import static dagger.internal.codegen.xprocessing.XElements.asField;
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 static dagger.internal.codegen.xprocessing.XProcessingEnvs.javacOverrides;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import static dagger.internal.codegen.xprocessing.XTypes.nonObjectSuperclass;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.STATIC;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XFieldElement;
import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XMethodType;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XType;
import androidx.room.compiler.processing.XTypeElement;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.SetMultimap;
import dagger.internal.codegen.binding.MembersInjectionBinding.InjectionSite;
import dagger.internal.codegen.langmodel.DaggerElements;
import dagger.internal.codegen.xprocessing.XElements;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
Expand All @@ -52,12 +51,13 @@
/** A factory for {@link Binding} objects. */
final class InjectionSiteFactory {

private final DaggerElements elements;
private final XProcessingEnv processingEnv;
private final DependencyRequestFactory dependencyRequestFactory;

@Inject
InjectionSiteFactory(DaggerElements elements, DependencyRequestFactory dependencyRequestFactory) {
this.elements = elements;
InjectionSiteFactory(
XProcessingEnv processingEnv, DependencyRequestFactory dependencyRequestFactory) {
this.processingEnv = processingEnv;
this.dependencyRequestFactory = dependencyRequestFactory;
}

Expand Down Expand Up @@ -88,7 +88,7 @@ ImmutableSortedSet<InjectionSite> getInjectionSites(XType type) {
.thenComparing(InjectionSite::kind)
// then sort by whichever element comes first in the parent
// this isn't necessary, but makes the processor nice and predictable
.thenComparing(injectionSite -> toJavac(injectionSite.element()), DECLARATION_ORDER),
.thenComparing(InjectionSite::element, DECLARATION_ORDER),
injectionSites);
}

Expand Down Expand Up @@ -117,8 +117,7 @@ public Optional<InjectionSite> visitMethod(XMethodElement method, XType containe
XTypeElement enclosingType = closestEnclosingTypeElement(method);
for (XMethodElement subclassMethod : subclassMethodMap.get(getSimpleName(method))) {
if (method != subclassMethod
&& elements.overrides(
toJavac(subclassMethod), toJavac(method), toJavac(enclosingType))) {
&& javacOverrides(subclassMethod, method, enclosingType, processingEnv)) {
return Optional.empty();
}
}
Expand All @@ -142,8 +141,8 @@ public Optional<InjectionSite> visitField(XFieldElement field, XType container)

private boolean shouldBeInjected(XElement injectionSite) {
return InjectionAnnotations.hasInjectAnnotation(injectionSite)
&& !toJavac(injectionSite).getModifiers().contains(PRIVATE)
&& !toJavac(injectionSite).getModifiers().contains(STATIC);
&& !XElements.isPrivate(injectionSite)
&& !XElements.isStatic(injectionSite);
}
}
}
2 changes: 1 addition & 1 deletion java/dagger/internal/codegen/binding/KeyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.extension.Optionals.firstPresent;
import static dagger.internal.codegen.langmodel.DaggerTypes.isFutureType;
import static dagger.internal.codegen.javapoet.TypeNames.isFutureType;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import static dagger.internal.codegen.xprocessing.XTypes.unwrapType;
import static java.util.Arrays.asList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package dagger.internal.codegen.binding;

import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.langmodel.DaggerTypes.isFutureType;
import static dagger.internal.codegen.javapoet.TypeNames.isFutureType;

import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XType;
Expand Down
Loading

0 comments on commit 6357c0e

Please sign in to comment.