Skip to content

Commit 5cba295

Browse files
authored
Merge pull request square#437 from google/moe_sync_0810
Moe sync 08/10
2 parents efc1fb2 + 6fc2b5a commit 5cba295

19 files changed

+298
-204
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ language: android
33
jdk:
44
- oraclejdk7
55
- openjdk7
6+
- oraclejdk8
67

78
android:
89
components:

compiler/src/main/java/dagger/internal/codegen/AbstractComponentWriter.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -659,26 +659,29 @@ private void implementInterfaceMethods() {
659659
for (ComponentMethodDescriptor componentMethod :
660660
graph.componentDescriptor().componentMethods()) {
661661
if (componentMethod.dependencyRequest().isPresent()) {
662-
DependencyRequest interfaceRequest = componentMethod.dependencyRequest().get();
663-
ExecutableElement requestElement =
664-
MoreElements.asExecutable(interfaceRequest.requestElement());
665-
ExecutableType requestType = MoreTypes.asExecutable(types.asMemberOf(
666-
MoreTypes.asDeclared(componentDefinitionType().asType()), requestElement));
667-
MethodSignature signature = MethodSignature.fromExecutableType(
668-
requestElement.getSimpleName().toString(), requestType);
662+
ExecutableElement methodElement =
663+
MoreElements.asExecutable(componentMethod.methodElement());
664+
ExecutableType requestType =
665+
MoreTypes.asExecutable(
666+
types.asMemberOf(
667+
MoreTypes.asDeclared(componentDefinitionType().asType()), methodElement));
668+
MethodSignature signature =
669+
MethodSignature.fromExecutableType(
670+
methodElement.getSimpleName().toString(), requestType);
669671
if (!interfaceMethods.contains(signature)) {
670672
interfaceMethods.add(signature);
671673
MethodSpec.Builder interfaceMethod =
672-
methodBuilder(requestElement.getSimpleName().toString())
674+
methodBuilder(methodElement.getSimpleName().toString())
673675
.addAnnotation(Override.class)
674676
.addModifiers(PUBLIC)
675677
.returns(TypeName.get(requestType.getReturnType()));
678+
DependencyRequest interfaceRequest = componentMethod.dependencyRequest().get();
676679
BindingKey bindingKey = interfaceRequest.bindingKey();
677680
MemberSelect memberSelect = getMemberSelect(bindingKey);
678681
CodeBlock memberSelectCodeBlock = memberSelect.getExpressionFor(name);
679682
switch (interfaceRequest.kind()) {
680683
case MEMBERS_INJECTOR:
681-
List<? extends VariableElement> parameters = requestElement.getParameters();
684+
List<? extends VariableElement> parameters = methodElement.getParameters();
682685
if (parameters.isEmpty()) {
683686
// we're returning the framework type
684687
interfaceMethod.addStatement("return $L", memberSelectCodeBlock);

compiler/src/main/java/dagger/internal/codegen/Binding.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.collect.ImmutableSet;
2525
import com.google.common.collect.Lists;
26+
import com.google.common.collect.Sets;
2627
import dagger.internal.codegen.BindingType.HasBindingType;
2728
import java.util.List;
2829
import java.util.Set;
@@ -61,15 +62,31 @@ Class<?> frameworkClass() {
6162
public abstract Key key();
6263

6364
/**
64-
* The explicit set of {@link DependencyRequest dependencies} required to satisfy this binding.
65+
* The explicit set of {@link DependencyRequest dependencies} required to satisfy this binding as
66+
* defined by the user-defined injection sites.
6567
*/
6668
abstract ImmutableSet<DependencyRequest> dependencies();
6769

6870
/**
69-
* The set of {@link DependencyRequest dependencies} required to satisfy this binding. This is a
70-
* superset of {@link #dependencies()}. This returns an unmodifiable set.
71+
* The set of {@link DependencyRequest dependencies} that are added by the framework rather than a
72+
* user-defined injection site. This returns an unmodifiable set.
7173
*/
72-
abstract Set<DependencyRequest> implicitDependencies();
74+
// TODO(gak): this will eventually get migrated to FrameworkDependency
75+
Set<DependencyRequest> frameworkDependencies() {
76+
return ImmutableSet.of();
77+
}
78+
79+
/**
80+
* The set of {@link DependencyRequest dependencies} required to satisfy this binding. This is the
81+
* union of {@link #dependencies()} and {@link #frameworkDependencies()}. This returns an
82+
* unmodifiable set.
83+
*/
84+
final Set<DependencyRequest> implicitDependencies() {
85+
Set<DependencyRequest> frameworkDependencies = frameworkDependencies();
86+
return frameworkDependencies.isEmpty()
87+
? dependencies()
88+
: Sets.union(frameworkDependencies, dependencies());
89+
}
7390

7491
/**
7592
* Returns the name of the package in which this binding must be managed. E.g.: a binding
@@ -129,7 +146,7 @@ public Void visitWildcard(WildcardType t, ImmutableSet.Builder<String> p) {
129146
}
130147

131148
/**
132-
* if this binding's key's type parameters are different from those of the
149+
* If this binding's key's type parameters are different from those of the
133150
* {@link #bindingTypeElement()}, this is the binding for the {@link #bindingTypeElement()}'s
134151
* unresolved type.
135152
*/

compiler/src/main/java/dagger/internal/codegen/BindingGraph.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -547,28 +547,49 @@ private ImmutableSet<ContributionBinding> createDelegateBindings(
547547
ImmutableSet<DelegateDeclaration> delegateDeclarations) {
548548
ImmutableSet.Builder<ContributionBinding> builder = ImmutableSet.builder();
549549
for (DelegateDeclaration delegateDeclaration : delegateDeclarations) {
550-
DependencyRequest delegateRequest = delegateDeclaration.delegateRequest();
551-
ResolvedBindings resolvedDelegate = lookUpBindings(delegateRequest);
552-
for (ContributionBinding explicitDelegate : resolvedDelegate.contributionBindings()) {
553-
switch (explicitDelegate.bindingType()) {
554-
case PRODUCTION:
555-
builder.add(
556-
productionBindingFactory.delegate(
557-
delegateDeclaration, (ProductionBinding) explicitDelegate));
558-
break;
559-
case PROVISION:
560-
builder.add(
561-
provisionBindingFactory.delegate(
562-
delegateDeclaration, (ProvisionBinding) explicitDelegate));
563-
break;
564-
default:
565-
throw new AssertionError();
566-
}
567-
}
550+
builder.add(createDelegateBinding(delegateDeclaration));
568551
}
569552
return builder.build();
570553
}
571554

555+
/**
556+
* Creates one (and only one) delegate binding for a delegate declaration, based on the
557+
* resolved bindings of the right-hand-side of a {@link dagger.Binds} method. If there are
558+
* duplicate bindings for the dependency key, there should still be only one binding for the
559+
* delegate key.
560+
*/
561+
private ContributionBinding createDelegateBinding(DelegateDeclaration delegateDeclaration) {
562+
ResolvedBindings resolvedDelegate = lookUpBindings(delegateDeclaration.delegateRequest());
563+
if (resolvedDelegate.contributionBindings().isEmpty()) {
564+
// This is guaranteed to result in a missing binding error, so it doesn't matter if the
565+
// binding is a Provision or Production, except if it is a @IntoMap method, in which
566+
// case the key will be of type Map<K, Provider<V>>, which will be "upgraded" into a
567+
// Map<K, Producer<V>> if it's requested in a ProductionComponent. This may result in a
568+
// strange error, that the RHS needs to be provided with an @Inject or @Provides
569+
// annotated method, but a user should be able to figure out if a @Produces annotation
570+
// is needed.
571+
// TODO(gak): revisit how we model missing delegates if/when we clean up how we model
572+
// binding declarations
573+
return provisionBindingFactory.missingDelegate(delegateDeclaration);
574+
}
575+
// It doesn't matter which of these is selected, since they will later on produce a
576+
// duplicate binding error.
577+
// TODO(ronshapiro): Once compile-testing has a CompilationResult, add a test which asserts
578+
// that a duplicate binding for the RHS does not result in a duplicate binding for the LHS.
579+
ContributionBinding explicitDelegate =
580+
resolvedDelegate.contributionBindings().iterator().next();
581+
switch (explicitDelegate.bindingType()) {
582+
case PRODUCTION:
583+
return productionBindingFactory.delegate(
584+
delegateDeclaration, (ProductionBinding) explicitDelegate);
585+
case PROVISION:
586+
return provisionBindingFactory.delegate(
587+
delegateDeclaration, (ProvisionBinding) explicitDelegate);
588+
default:
589+
throw new AssertionError("bindingType: " + explicitDelegate);
590+
}
591+
}
592+
572593
private ImmutableSetMultimap<ComponentDescriptor, ContributionBinding>
573594
indexBindingsByOwningComponent(
574595
DependencyRequest request, Iterable<? extends ContributionBinding> bindings) {

compiler/src/main/java/dagger/internal/codegen/BindingGraphValidator.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static final class DependencyPath {
160160

161161
/** The entry point. */
162162
Element entryPointElement() {
163-
return path.getFirst().dependencyRequest().requestElement();
163+
return path.getFirst().dependencyRequest().requestElement().get();
164164
}
165165

166166
/** The current dependency request, which is a transitive dependency of the entry point. */
@@ -392,8 +392,7 @@ private void validateResolvedBindings(DependencyPath path) {
392392
throw new AssertionError(
393393
"contribution binding keys should never have members injection bindings");
394394
}
395-
validateNullability(
396-
path.currentDependencyRequest(), resolvedBindings.contributionBindings());
395+
validateNullability(path, resolvedBindings.contributionBindings());
397396
if (resolvedBindings.contributionBindings().size() > 1) {
398397
reportDuplicateBindings(path);
399398
return;
@@ -517,9 +516,12 @@ private ImmutableListMultimap<ContributionType, BindingDeclaration> declarations
517516
.build();
518517
}
519518

520-
/** Ensures that if the request isn't nullable, then each contribution is also not nullable. */
521-
private void validateNullability(DependencyRequest request, Set<ContributionBinding> bindings) {
522-
if (request.isNullable()) {
519+
/**
520+
* Ensures that if the current request isn't nullable, then each contribution is also not
521+
* nullable.
522+
*/
523+
private void validateNullability(DependencyPath path, Set<ContributionBinding> bindings) {
524+
if (path.currentDependencyRequest().isNullable()) {
523525
return;
524526
}
525527

@@ -528,16 +530,16 @@ private void validateNullability(DependencyRequest request, Set<ContributionBind
528530
* (Maybe this happens if the code was already compiled before this point?)
529531
* ... we manually print out the request in that case, otherwise the error
530532
* message is kind of useless. */
531-
String typeName = TypeName.get(request.key().type()).toString();
533+
String typeName = TypeName.get(path.currentDependencyRequest().key().type()).toString();
532534

533535
for (ContributionBinding binding : bindings) {
534536
if (binding.nullableType().isPresent()) {
535537
reportBuilder.addItem(
536538
nullableToNonNullable(typeName, bindingDeclarationFormatter.format(binding))
537539
+ "\n at: "
538-
+ dependencyRequestFormatter.format(request),
540+
+ dependencyRequestFormatter.toDependencyTrace(path),
539541
compilerOptions.nullableValidationKind(),
540-
request.requestElement());
542+
path.entryPointElement());
541543
}
542544
}
543545
}
@@ -583,10 +585,9 @@ private void validateMapKeyAnnotationTypes(
583585
}
584586
}
585587

586-
/**
587-
* Reports errors if a members injection binding is invalid.
588-
*/
589-
private void validateMembersInjectionBinding(Binding binding, final DependencyPath path) {
588+
/** Reports errors if a members injection binding is invalid. */
589+
private void validateMembersInjectionBinding(
590+
final MembersInjectionBinding binding, final DependencyPath path) {
590591
binding
591592
.key()
592593
.type()
@@ -595,8 +596,7 @@ private void validateMembersInjectionBinding(Binding binding, final DependencyPa
595596
@Override
596597
protected Void defaultAction(TypeMirror e, Void p) {
597598
reportBuilder.addError(
598-
"Invalid members injection request.",
599-
path.currentDependencyRequest().requestElement());
599+
"Invalid members injection request.", binding.membersInjectedType());
600600
return null;
601601
}
602602

@@ -1180,7 +1180,7 @@ private ImmutableSet<DependencyRequest> providersBreakingCycle(
11801180
return FluentIterable.from(cycle)
11811181
.skip(1)
11821182
.transform(ResolvedRequest.DEPENDENCY_REQUEST)
1183-
.filter(not(DependencyRequest.IS_SYNTHETIC))
1183+
.filter(DependencyRequest.HAS_REQUEST_ELEMENT)
11841184
.filter(
11851185
new Predicate<DependencyRequest>() {
11861186
@Override

compiler/src/main/java/dagger/internal/codegen/ContributionBinding.java

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static dagger.internal.codegen.ContributionBinding.FactoryCreationStrategy.CLASS_CONSTRUCTOR;
2121
import static dagger.internal.codegen.ContributionBinding.FactoryCreationStrategy.DELEGATE;
2222
import static dagger.internal.codegen.ContributionBinding.FactoryCreationStrategy.ENUM_INSTANCE;
23-
import static dagger.internal.codegen.ContributionBinding.Kind.IS_SYNTHETIC_KIND;
2423
import static dagger.internal.codegen.MapKeys.unwrapValue;
2524
import static dagger.internal.codegen.MoreAnnotationMirrors.unwrapOptionalEquivalence;
2625

@@ -33,7 +32,6 @@
3332
import com.google.common.base.Predicates;
3433
import com.google.common.collect.ImmutableSetMultimap;
3534
import com.google.common.collect.Multimaps;
36-
import com.google.common.collect.Sets;
3735
import com.google.common.util.concurrent.ListenableFuture;
3836
import com.google.errorprone.annotations.CanIgnoreReturnValue;
3937
import dagger.Component;
@@ -59,28 +57,9 @@
5957
*/
6058
abstract class ContributionBinding extends Binding implements HasContributionType {
6159

62-
@Override
63-
Set<DependencyRequest> implicitDependencies() {
64-
// Optimization: If we don't need the memberInjectionRequest, don't create more objects.
65-
if (!membersInjectionRequest().isPresent()) {
66-
return dependencies();
67-
} else {
68-
// Optimization: Avoid creating an ImmutableSet+Builder just to union two things together.
69-
return Sets.union(membersInjectionRequest().asSet(), dependencies());
70-
}
71-
}
72-
7360
/** Returns the type that specifies this' nullability, absent if not nullable. */
7461
abstract Optional<DeclaredType> nullableType();
7562

76-
/**
77-
* Returns whether this binding is synthetic, i.e., not explicitly tied to code, but generated
78-
* implicitly by the framework.
79-
*/
80-
boolean isSyntheticBinding() {
81-
return IS_SYNTHETIC_KIND.apply(bindingKind());
82-
}
83-
8463
/**
8564
* A function that returns the kind of a binding.
8665
*/
@@ -92,9 +71,6 @@ public Kind apply(ContributionBinding binding) {
9271
}
9372
};
9473

95-
/** If this provision requires members injection, this will be the corresponding request. */
96-
abstract Optional<DependencyRequest> membersInjectionRequest();
97-
9874
abstract Optional<Equivalence.Wrapper<AnnotationMirror>> wrappedMapKey();
9975

10076
final Optional<AnnotationMirror> mapKey() {
@@ -166,17 +142,6 @@ enum Kind {
166142
COMPONENT_PRODUCTION,
167143
;
168144

169-
/**
170-
* A predicate that tests whether a kind is for synthetic bindings.
171-
*/
172-
static final Predicate<Kind> IS_SYNTHETIC_KIND =
173-
Predicates.in(
174-
immutableEnumSet(
175-
SYNTHETIC_MAP,
176-
SYNTHETIC_MULTIBOUND_SET,
177-
SYNTHETIC_MULTIBOUND_MAP,
178-
SYNTHETIC_DELEGATE_BINDING));
179-
180145
/**
181146
* A predicate that tests whether a kind is for synthetic multibindings.
182147
*/
@@ -320,8 +285,6 @@ abstract static class Builder<B extends Builder<B>> {
320285

321286
abstract B nullableType(Optional<DeclaredType> nullableType);
322287

323-
abstract B membersInjectionRequest(Optional<DependencyRequest> membersInjectionRequest);
324-
325288
abstract B wrappedMapKey(Optional<Equivalence.Wrapper<AnnotationMirror>> wrappedMapKey);
326289

327290
abstract B bindingKind(ContributionBinding.Kind kind);

0 commit comments

Comments
 (0)