Skip to content

Commit

Permalink
Migrate ModuleDescriptor.Factory to XProcessing.
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 405473969
  • Loading branch information
bcorso authored and Dagger Team committed Oct 25, 2021
1 parent d3e0cbb commit 8cfbc2c
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 148 deletions.
10 changes: 7 additions & 3 deletions java/dagger/internal/codegen/binding/BindingGraphFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.isAssistedFactoryType;
import static dagger.internal.codegen.binding.ComponentDescriptor.isComponentContributionMethod;
import static dagger.internal.codegen.binding.SourceFiles.generatedMonitoringModuleName;
import static dagger.internal.codegen.langmodel.DaggerElements.checkTypePresent;
import static dagger.spi.model.BindingKind.ASSISTED_INJECTION;
import static dagger.spi.model.BindingKind.DELEGATE;
import static dagger.spi.model.BindingKind.INJECTION;
Expand All @@ -36,6 +37,7 @@
import static java.util.function.Predicate.isEqual;
import static javax.lang.model.util.ElementFilter.methodsIn;

import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XTypeElement;
import com.google.auto.common.MoreTypes;
import com.google.common.collect.HashMultimap;
Expand Down Expand Up @@ -80,6 +82,7 @@
@Singleton
public final class BindingGraphFactory implements ClearableCache {

private final XProcessingEnv processingEnv;
private final DaggerElements elements;
private final InjectBindingRegistry injectBindingRegistry;
private final KeyFactory keyFactory;
Expand All @@ -91,13 +94,15 @@ public final class BindingGraphFactory implements ClearableCache {

@Inject
BindingGraphFactory(
XProcessingEnv processingEnv,
DaggerElements elements,
InjectBindingRegistry injectBindingRegistry,
KeyFactory keyFactory,
BindingFactory bindingFactory,
ModuleDescriptor.Factory moduleDescriptorFactory,
BindingGraphConverter bindingGraphConverter,
CompilerOptions compilerOptions) {
this.processingEnv = processingEnv;
this.elements = elements;
this.injectBindingRegistry = injectBindingRegistry;
this.keyFactory = keyFactory;
Expand Down Expand Up @@ -287,14 +292,13 @@ private boolean shouldIncludeImplicitProductionModules(
*/
private ModuleDescriptor descriptorForMonitoringModule(XTypeElement componentDefinitionType) {
return moduleDescriptorFactory.create(
elements.checkTypePresent(
generatedMonitoringModuleName(componentDefinitionType).toString()));
checkTypePresent(processingEnv, generatedMonitoringModuleName(componentDefinitionType)));
}

/** Returns a descriptor {@link ProductionExecutorModule}. */
private ModuleDescriptor descriptorForProductionExecutorModule() {
return moduleDescriptorFactory.create(
elements.getTypeElement(TypeNames.PRODUCTION_EXECTUTOR_MODULE));
processingEnv.findTypeElement(TypeNames.PRODUCTION_EXECTUTOR_MODULE));
}

/** Indexes {@code bindingDeclarations} by {@link BindingDeclaration#key()}. */
Expand Down
57 changes: 34 additions & 23 deletions java/dagger/internal/codegen/binding/KeyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package dagger.internal.codegen.binding;

import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static com.google.auto.common.MoreElements.isAnnotationPresent;
import static com.google.auto.common.MoreTypes.asExecutable;
import static com.google.auto.common.MoreTypes.isType;
import static com.google.common.base.Preconditions.checkArgument;
Expand All @@ -29,15 +28,19 @@
import static dagger.internal.codegen.binding.MapKeys.mapKeyType;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.extension.Optionals.firstPresent;
import static dagger.internal.codegen.langmodel.DaggerElements.isAnnotationPresent;
import static dagger.internal.codegen.langmodel.DaggerTypes.isFutureType;
import static dagger.internal.codegen.langmodel.DaggerTypes.unwrapType;
import static dagger.spi.model.DaggerAnnotation.fromJava;
import static dagger.spi.model.DaggerType.fromJava;
import static java.util.Arrays.asList;
import static javax.lang.model.element.ElementKind.METHOD;

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.common.MoreTypes;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
Expand Down Expand Up @@ -104,8 +107,11 @@ private DeclaredType mapOf(TypeMirror keyType, TypeMirror valueType) {

/** Returns {@code Map<KeyType, FrameworkType<ValueType>>}. */
private TypeMirror mapOfFrameworkType(
TypeMirror keyType, TypeElement frameworkType, TypeMirror valueType) {
return mapOf(keyType, types.getDeclaredType(frameworkType, boxPrimitives(valueType)));
TypeMirror keyType, ClassName frameworkClassName, TypeMirror valueType) {
return mapOf(
keyType,
types.getDeclaredType(
elements.getTypeElement(frameworkClassName), boxPrimitives(valueType)));
}

Key forComponentMethod(ExecutableElement componentMethod) {
Expand Down Expand Up @@ -140,39 +146,44 @@ public Key forSubcomponentCreator(TypeMirror creatorType) {
}

public Key forProvidesMethod(ExecutableElement method, TypeElement contributingModule) {
return forBindingMethod(
method, contributingModule, Optional.of(elements.getTypeElement(TypeNames.PROVIDER)));
return forBindingMethod(method, contributingModule, Optional.of(TypeNames.PROVIDER));
}

public Key forProducesMethod(ExecutableElement method, TypeElement contributingModule) {
return forBindingMethod(
method, contributingModule, Optional.of(elements.getTypeElement(TypeNames.PRODUCER)));
return forBindingMethod(method, contributingModule, Optional.of(TypeNames.PRODUCER));
}

/** Returns the key bound by a {@link Binds} method. */
Key forBindsMethod(ExecutableElement method, TypeElement contributingModule) {
checkArgument(isAnnotationPresent(method, Binds.class));
checkArgument(isAnnotationPresent(method, TypeNames.BINDS));
return forBindingMethod(method, contributingModule, Optional.empty());
}

/** Returns the base key bound by a {@link BindsOptionalOf} method. */
Key forBindsOptionalOfMethod(ExecutableElement method, TypeElement contributingModule) {
checkArgument(isAnnotationPresent(method, BindsOptionalOf.class));
Key forBindsOptionalOfMethod(XMethodElement method, XTypeElement contributingModule) {
checkArgument(method.hasAnnotation(TypeNames.BINDS_OPTIONAL_OF));
return forBindingMethod(method, contributingModule, Optional.empty());
}

private Key forBindingMethod(
XMethodElement method,
XTypeElement contributingModule,
Optional<ClassName> frameworkClassName) {
return forBindingMethod(toJavac(method), toJavac(contributingModule), frameworkClassName);
}

private Key forBindingMethod(
ExecutableElement method,
TypeElement contributingModule,
Optional<TypeElement> frameworkType) {
Optional<ClassName> frameworkClassName) {
checkArgument(method.getKind().equals(METHOD));
ExecutableType methodType =
MoreTypes.asExecutable(
types.asMemberOf(MoreTypes.asDeclared(contributingModule.asType()), method));
ContributionType contributionType = ContributionType.fromBindingElement(method);
TypeMirror returnType = methodType.getReturnType();
if (frameworkType.isPresent()
&& frameworkType.get().equals(elements.getTypeElement(TypeNames.PRODUCER))
if (frameworkClassName.isPresent()
&& frameworkClassName.get().equals(TypeNames.PRODUCER)
&& isType(returnType)) {
if (isFutureType(methodType.getReturnType())) {
returnType = getOnlyElement(MoreTypes.asDeclared(returnType).getTypeArguments());
Expand All @@ -186,7 +197,8 @@ && isType(returnType)) {
}
}
}
TypeMirror keyType = bindingMethodKeyType(returnType, method, contributionType, frameworkType);
TypeMirror keyType =
bindingMethodKeyType(returnType, method, contributionType, frameworkClassName);
Key key = forMethod(method, keyType);
return contributionType.equals(ContributionType.UNIQUE)
? key
Expand All @@ -202,33 +214,32 @@ && isType(returnType)) {
* <p>The key's type is either {@code Set<T>} or {@code Map<K, Provider<V>>}. The latter works
* even for maps used by {@code Producer}s.
*/
Key forMultibindsMethod(ExecutableType executableType, ExecutableElement method) {
checkArgument(method.getKind().equals(METHOD), "%s must be a method", method);
TypeMirror returnType = executableType.getReturnType();
Key forMultibindsMethod(XMethodElement method, XMethodType methodType) {
XType returnType = method.getReturnType();
TypeMirror keyType =
MapType.isMap(returnType)
? mapOfFrameworkType(
MapType.from(returnType).keyType(),
elements.getTypeElement(TypeNames.PROVIDER),
TypeNames.PROVIDER,
MapType.from(returnType).valueType())
: returnType;
return forMethod(method, keyType);
: toJavac(returnType);
return forMethod(toJavac(method), keyType);
}

private TypeMirror bindingMethodKeyType(
TypeMirror returnType,
ExecutableElement method,
ContributionType contributionType,
Optional<TypeElement> frameworkType) {
Optional<ClassName> frameworkClassName) {
switch (contributionType) {
case UNIQUE:
return returnType;
case SET:
return setOf(returnType);
case MAP:
TypeMirror mapKeyType = mapKeyType(getMapKey(method).get(), types);
return frameworkType.isPresent()
? mapOfFrameworkType(mapKeyType, frameworkType.get(), returnType)
return frameworkClassName.isPresent()
? mapOfFrameworkType(mapKeyType, frameworkClassName.get(), returnType)
: mapOf(mapKeyType, returnType);
case SET_VALUES:
// TODO(gak): do we want to allow people to use "covariant return" here?
Expand Down
Loading

0 comments on commit 8cfbc2c

Please sign in to comment.