static boolean isValidMembersInjectionKey(Key key) { return !key.qualifier().isPresent() && !key.multibindingContributionIdentifier().isPresent() && key.type().getKind().equals(TypeKind.DECLARED); }
Key forType(TypeMirror type) { return Key.builder(type).build(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OptionalBindingDeclaration) { OptionalBindingDeclaration that = (OptionalBindingDeclaration) o; return (this.bindingElement.equals(that.bindingElement())) && (this.contributingModule.equals(that.contributingModule())) && (this.key.equals(that.key())); } return false; }
/** * Returns {@code true} if this is valid as an implicit key (that is, if it's valid for a * just-in-time binding by discovering an {@code @Inject} constructor). */ static boolean isValidImplicitProvisionKey(Key key, DaggerTypes types) { return isValidImplicitProvisionKey(key.qualifier(), key.type(), types); }
/** * Returns a {@link SetType} for {@code key}'s {@link Key#type() type}. * * @throws IllegalArgumentException if {@code key.type()} is not a {@link Set} type */ static SetType from(Key key) { return from (key.type()); } }
private boolean breaksCycle(DependencyEdge edge, BindingGraph graph) { if (edge.dependencyRequest().key().multibindingContributionIdentifier().isPresent()) { return false; } if (breaksCycle(edge.dependencyRequest().key().type(), edge.dependencyRequest().kind())) { return true; } Node target = graph.network().incidentNodes(edge).target(); if (target instanceof dagger.model.Binding && ((dagger.model.Binding) target).kind().equals(BindingKind.OPTIONAL)) { /* For @BindsOptionalOf bindings, unwrap the type inside the Optional. If the unwrapped type * 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 false; }
@Override public int hashCode() { int h = 1; h *= 1000003; h ^= this.key.hashCode(); h *= 1000003; h ^= this.frameworkType.hashCode(); return h; }
/** * A multimap of those {@code declarations} that are multibinding contribution declarations, * indexed by the key of the set or map to which they contribute. */ static <T extends BindingDeclaration> ImmutableSetMultimap<Key, T> multibindingContributionsByMultibindingKey( Iterable<T> declarations) { ImmutableSetMultimap.Builder<Key, T> builder = ImmutableSetMultimap.builder(); for (T declaration : declarations) { if (declaration.key().multibindingContributionIdentifier().isPresent()) { builder.put( declaration .key() .toBuilder() .multibindingContributionIdentifier(Optional.empty()) .build(), declaration); } } return builder.build(); } }
/** * If {@code key}'s type is {@code Optional<T>} for some {@code T}, returns a key with the same * qualifier whose type is {@linkplain RequestKinds#extractKeyType(RequestKind, TypeMirror)} * extracted} from {@code T}. */ Optional<Key> unwrapOptional(Key key) { if (!OptionalType.isOptional(key)) { return Optional.empty(); } TypeMirror optionalValueType = OptionalType.from(key).valueType(); return Optional.of( key.toBuilder() .type(extractKeyType(getRequestKind(optionalValueType), optionalValueType)) .build()); } }
private String inconsistentMapKeyAnnotationTypesErrorMessage( ImmutableSetMultimap<Equivalence.Wrapper<DeclaredType>, ContributionBinding> contributionsByMapKeyAnnotationType, Key mapBindingKey) { StringBuilder message = new StringBuilder(mapBindingKey.toString()) .append(" uses more than one @MapKey annotation type"); Multimaps.asMap(contributionsByMapKeyAnnotationType) .forEach( (annotationType, contributions) -> { message.append('\n').append(INDENT).append(annotationType.get()).append(':'); bindingDeclarationFormatter.formatIndentedList(message, contributions, 2); }); return message.toString(); }
/** Creates a synthetic dependency request for one individual {@code multibindingContribution}. */ private DependencyRequest forMultibindingContribution( Key multibindingKey, ContributionBinding multibindingContribution) { checkArgument( multibindingContribution.key().multibindingContributionIdentifier().isPresent(), "multibindingContribution's key must have a multibinding contribution identifier: %s", multibindingContribution); return DependencyRequest.builder() .kind(multibindingContributionRequestKind(multibindingKey, multibindingContribution)) .key(multibindingContribution.key()) .build(); }
@Override protected TypeMirror contributedType() { return request.key().type(); } }
@Override public int hashCode() { int h = 1; h *= 1000003; h ^= this.bindingElement.hashCode(); h *= 1000003; h ^= this.contributingModule.hashCode(); h *= 1000003; h ^= this.key.hashCode(); h *= 1000003; h ^= this.subcomponentType.hashCode(); h *= 1000003; h ^= this.moduleAnnotation.hashCode(); return h; }
/** * If {@code key}'s type is {@code Set<WrappingClass<Bar>>}, returns a key with type {@code Set * <Bar>} with the same qualifier. Otherwise returns {@link Optional#empty()}. */ Optional<Key> unwrapSetKey(Key key, Class<?> wrappingClass) { if (SetType.isSet(key)) { SetType setType = SetType.from(key); if (!setType.isRawType() && setType.elementsAreTypeOf(wrappingClass)) { return Optional.of( key.toBuilder().type(setOf(setType.unwrappedElementType(wrappingClass))).build()); } } return Optional.empty(); }
private static ImmutableSet<InjectionSite> injectionSites(Binding binding) { if (binding instanceof ProvisionBinding) { return ((ProvisionBinding) binding).injectionSites(); } else if (binding instanceof MembersInjectionBinding) { return ((MembersInjectionBinding) binding).injectionSites(); } throw new IllegalArgumentException(binding.key().toString()); } }
/** * Returns a {@link OptionalType} for {@code key}'s {@link Key#type() type}. * * @throws IllegalArgumentException if {@code key.type()} is not an {@code Optional} type */ static OptionalType from(Key key) { return from(key.type()); } }
static String name(Key key) { if (key.multibindingContributionIdentifier().isPresent()) { return key.multibindingContributionIdentifier().get().bindingElement(); } StringBuilder builder = new StringBuilder(); if (key.qualifier().isPresent()) { // TODO(gak): Use a better name for fields with qualifiers with members. builder.append(key.qualifier().get().getAnnotationType().asElement().getSimpleName()); } key.type().accept(TYPE_NAMER, builder); return protectAgainstKeywords(UPPER_CAMEL.to(LOWER_CAMEL, builder.toString())); } }
@Override public String visitVariable(VariableElement variable, DependencyRequest request) { TypeMirror requestedType = requestType(request.kind(), request.key().type(), types); return INDENT + formatQualifier(request.key().qualifier()) + requestedType + " is injected at\n" + DOUBLE_INDENT + elementToString(variable); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof BindingRequest) { BindingRequest that = (BindingRequest) o; return (this.key.equals(that.key())) && (this.requestKind.equals(that.requestKind())) && (this.frameworkType.equals(that.frameworkType())); } return false; }