Skip to content

Commit

Permalink
Refactor RequestKinds.extractKeyType(RequestKind, TypeMirror) to Requ…
Browse files Browse the repository at this point in the history
…estKinds.extractKeyType(TypeMirror)

RequestKinds.extractKeyType(RequestKind, TypeMirror) currently requires passing in the "correct" RequestKind for the given TypeMirror. This CL refactors the method so that the RequestKind is no longer needed because it's calculated internally. This makes the method easier to use, and we no longer have to check that the user passed in a valid RequestKind.

RELNOTES=N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=286925810
  • Loading branch information
bcorso authored and cpovirk committed Dec 26, 2019
1 parent 405d8f9 commit 545f851
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
23 changes: 15 additions & 8 deletions java/dagger/internal/codegen/base/RequestKinds.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@
import static com.google.auto.common.MoreTypes.isType;
import static com.google.auto.common.MoreTypes.isTypeOf;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.getOnlyElement;
import static dagger.internal.codegen.javapoet.TypeNames.lazyOf;
import static dagger.internal.codegen.javapoet.TypeNames.listenableFutureOf;
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.model.RequestKind.INSTANCE;
import static dagger.model.RequestKind.LAZY;
import static dagger.model.RequestKind.PRODUCED;
import static dagger.model.RequestKind.PRODUCER;
import static dagger.model.RequestKind.PROVIDER;
import static dagger.model.RequestKind.PROVIDER_OF_LAZY;
import static javax.lang.model.type.TypeKind.DECLARED;

import com.google.auto.common.MoreTypes;
import com.google.common.base.Equivalence;
Expand Down Expand Up @@ -117,15 +116,20 @@ public static RequestKind getRequestKind(TypeMirror type) {

public static RequestKind getRequestKindUncached(TypeMirror type) {
checkTypePresent(type);
if (!type.getKind().equals(DECLARED) || asDeclared(type).getTypeArguments().isEmpty()) {
// If the type is not a declared type (i.e. class or interface) with type arguments, then we
// know it can't be a parameterized type of one of the framework classes, so return INSTANCE.
return RequestKind.INSTANCE;
}
for (RequestKind kind : FRAMEWORK_CLASSES.keySet()) {
if (matchesKind(kind, type)) {
if (isTypeOf(frameworkClass(kind), type)) {
if (kind.equals(PROVIDER) && matchesKind(LAZY, extractKeyType(kind, type))) {
return PROVIDER_OF_LAZY;
}
return kind;
}
}
return INSTANCE;
return RequestKind.INSTANCE;
}

/**
Expand All @@ -147,16 +151,19 @@ && isTypeOf(frameworkClass(kind), type)
* @throws IllegalArgumentException if {@code type} is not wrapped with {@code requestKind}'s
* framework class(es).
*/
public static TypeMirror extractKeyType(RequestKind requestKind, TypeMirror type) {
checkTypePresent(type);
public static TypeMirror extractKeyType(TypeMirror type) {
return extractKeyType(getRequestKind(type), type);
}

private static TypeMirror extractKeyType(RequestKind requestKind, TypeMirror type) {
switch (requestKind) {
case INSTANCE:
return type;
case PROVIDER_OF_LAZY:
return extractKeyType(LAZY, extractKeyType(PROVIDER, type));
default:
checkArgument(isType(type) && isTypeOf(frameworkClass(requestKind), type));
return getOnlyElement(MoreTypes.asDeclared(type).getTypeArguments());
checkArgument(isType(type));
return DaggerTypes.unwrapType(type);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static dagger.internal.codegen.base.RequestKinds.frameworkClass;
import static dagger.internal.codegen.base.RequestKinds.getRequestKind;
import static dagger.internal.codegen.binding.ConfigurationAnnotations.getNullableType;
import static dagger.internal.codegen.langmodel.DaggerTypes.unwrapType;
import static dagger.model.RequestKind.FUTURE;
import static dagger.model.RequestKind.INSTANCE;
import static dagger.model.RequestKind.MEMBERS_INJECTION;
Expand All @@ -36,7 +37,6 @@
import dagger.Lazy;
import dagger.internal.codegen.base.MapType;
import dagger.internal.codegen.base.OptionalType;
import dagger.internal.codegen.langmodel.DaggerTypes;
import dagger.model.DependencyRequest;
import dagger.model.Key;
import dagger.model.RequestKind;
Expand All @@ -60,14 +60,11 @@
*/
public final class DependencyRequestFactory {
private final KeyFactory keyFactory;
private final DaggerTypes types;
private final InjectionAnnotations injectionAnnotations;

@Inject
DependencyRequestFactory(
KeyFactory keyFactory, DaggerTypes types, InjectionAnnotations injectionAnnotations) {
DependencyRequestFactory(KeyFactory keyFactory, InjectionAnnotations injectionAnnotations) {
this.keyFactory = keyFactory;
this.types = types;
this.injectionAnnotations = injectionAnnotations;
}

Expand Down Expand Up @@ -167,7 +164,7 @@ public DependencyRequest forComponentProductionMethod(
if (isTypeOf(ListenableFuture.class, type)) {
return DependencyRequest.builder()
.kind(FUTURE)
.key(keyFactory.forQualifiedType(qualifier, types.unwrapType(type)))
.key(keyFactory.forQualifiedType(qualifier, unwrapType(type)))
.requestElement(productionMethod)
.build();
} else {
Expand Down Expand Up @@ -224,7 +221,7 @@ private DependencyRequest newDependencyRequest(
RequestKind requestKind = getRequestKind(type);
return DependencyRequest.builder()
.kind(requestKind)
.key(keyFactory.forQualifiedType(qualifier, extractKeyType(requestKind, type)))
.key(keyFactory.forQualifiedType(qualifier, extractKeyType(type)))
.requestElement(requestElement)
.isNullable(allowsNull(requestKind, getNullableType(requestElement)))
.build();
Expand Down
9 changes: 3 additions & 6 deletions java/dagger/internal/codegen/binding/KeyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.getOnlyElement;
import static dagger.internal.codegen.base.RequestKinds.extractKeyType;
import static dagger.internal.codegen.base.RequestKinds.getRequestKind;
import static dagger.internal.codegen.binding.MapKeys.getMapKey;
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.DaggerTypes.isFutureType;
import static dagger.internal.codegen.langmodel.DaggerTypes.unwrapType;
import static java.util.Arrays.asList;
import static javax.lang.model.element.ElementKind.METHOD;

Expand Down Expand Up @@ -172,7 +172,7 @@ && isType(returnType)) {
if (isFutureType(setType.elementType())) {
returnType =
types.getDeclaredType(
elements.getTypeElement(Set.class), types.unwrapType(setType.elementType()));
elements.getTypeElement(Set.class), unwrapType(setType.elementType()));
}
}
}
Expand Down Expand Up @@ -429,9 +429,6 @@ Optional<Key> unwrapOptional(Key key) {
}

TypeMirror optionalValueType = OptionalType.from(key).valueType();
return Optional.of(
key.toBuilder()
.type(extractKeyType(getRequestKind(optionalValueType), optionalValueType))
.build());
return Optional.of(key.toBuilder().type(extractKeyType(optionalValueType)).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private boolean breaksCycle(DependencyEdge edge, BindingGraph graph) {
* breaks the cycle, so does the optional binding. */
TypeMirror optionalValueType = OptionalType.from(edge.dependencyRequest().key()).valueType();
RequestKind requestKind = getRequestKind(optionalValueType);
return breaksCycle(extractKeyType(requestKind, optionalValueType), requestKind);
return breaksCycle(extractKeyType(optionalValueType), requestKind);
}
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions java/dagger/internal/codegen/langmodel/DaggerTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public Iterable<TypeMirror> supertypes(TypeMirror type) {
* @throws IllegalArgumentException if {@code type} is not a declared type or has zero or more
* than one type arguments.
*/
public TypeMirror unwrapType(TypeMirror type) {
public static TypeMirror unwrapType(TypeMirror type) {
TypeMirror unwrapped = unwrapTypeOrDefault(type, null);
checkArgument(unwrapped != null, "%s is a raw type", type);
return unwrapped;
Expand All @@ -101,7 +101,7 @@ public TypeMirror unwrapTypeOrObject(TypeMirror type) {
return unwrapTypeOrDefault(type, elements.getTypeElement(Object.class).asType());
}

private TypeMirror unwrapTypeOrDefault(TypeMirror type, TypeMirror defaultType) {
private static TypeMirror unwrapTypeOrDefault(TypeMirror type, TypeMirror defaultType) {
DeclaredType declaredType = MoreTypes.asDeclared(type);
TypeElement typeElement = MoreElements.asType(declaredType.asElement());
checkArgument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.google.auto.common.MoreElements.asType;
import static com.google.auto.common.MoreElements.asVariable;
import static dagger.internal.codegen.base.RequestKinds.extractKeyType;
import static dagger.internal.codegen.base.RequestKinds.getRequestKind;
import static dagger.internal.codegen.binding.SourceFiles.membersInjectorNameForType;
import static javax.lang.model.type.TypeKind.WILDCARD;

Expand Down Expand Up @@ -100,7 +99,7 @@ private void checkQualifiers(ValidationReport.Builder<?> report, Element request

private void checkType(
ValidationReport.Builder<?> report, Element requestElement, TypeMirror requestType) {
TypeMirror keyType = extractKeyType(getRequestKind(requestType), requestType);
TypeMirror keyType = extractKeyType(requestType);
if (keyType.getKind().equals(WILDCARD)) {
// TODO(ronshapiro): Explore creating this message using RequestKinds.
report.addError(
Expand Down

0 comments on commit 545f851

Please sign in to comment.