Skip to content

Commit 570d124

Browse files
authored
Merge pull request square#444 from google/moe_writing_branch_from_f53c3f9f3b293085727489fa39fb1134a4d36aaf
Moe sync 08/23
2 parents 3009ccc + d434756 commit 570d124

13 files changed

+251
-276
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ private Optional<CodeBlock> initializeContributionBinding(BindingKey bindingKey)
839839
case DELEGATE:
840840
CodeBlock delegatingCodeBlock = CodeBlock.of(
841841
"($T) $L",
842-
binding.frameworkClass(),
842+
binding.bindingType().frameworkClass(),
843843
getMemberSelect(
844844
Iterables.getOnlyElement(binding.dependencies()).bindingKey())
845845
.getExpressionFor(name));

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@
5050
*/
5151
abstract class Binding extends BindingDeclaration implements HasBindingType {
5252

53-
/**
54-
* Returns the framework class associated with this binding.
55-
*/
56-
Class<?> frameworkClass() {
57-
return bindingType().frameworkClass();
58-
}
59-
6053
/** The {@link Key} that is provided by this binding. */
6154
@Override
6255
public abstract Key key();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.google.common.collect.TreeTraverser;
5757
import dagger.Component;
5858
import dagger.Reusable;
59+
import dagger.Subcomponent;
5960
import dagger.internal.codegen.ComponentDescriptor.ComponentMethodDescriptor;
6061
import dagger.internal.codegen.Key.HasKey;
6162
import dagger.producers.Produced;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.google.common.base.Function;
2020
import com.google.common.base.Predicate;
2121
import com.google.common.base.Predicates;
22+
import com.google.common.collect.ImmutableSet;
23+
import com.google.common.collect.Sets;
2224
import dagger.MembersInjector;
2325
import dagger.producers.Producer;
2426
import javax.inject.Provider;
@@ -37,6 +39,9 @@ enum BindingType {
3739
PRODUCTION(Producer.class),
3840
;
3941

42+
static final ImmutableSet<BindingType> CONTRIBUTION_TYPES =
43+
Sets.immutableEnumSet(PROVISION, PRODUCTION);
44+
4045
/** An object that is associated with a {@link BindingType}. */
4146
interface HasBindingType {
4247
/** The binding type of this object. */
@@ -45,7 +50,7 @@ interface HasBindingType {
4550

4651
private final Class<?> frameworkClass;
4752

48-
private BindingType(Class<?> frameworkClass) {
53+
BindingType(Class<?> frameworkClass) {
4954
this.frameworkClass = frameworkClass;
5055
}
5156

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (C) 2014 The Dagger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dagger.internal.codegen;
18+
19+
import static com.google.common.collect.Iterables.getOnlyElement;
20+
import static dagger.internal.codegen.BindingType.CONTRIBUTION_TYPES;
21+
import static dagger.internal.codegen.BindingType.MEMBERS_INJECTION;
22+
import static dagger.internal.codegen.BindingType.PRODUCTION;
23+
import static dagger.internal.codegen.BindingType.PROVISION;
24+
25+
import com.google.common.base.Function;
26+
import com.google.common.collect.FluentIterable;
27+
import com.google.common.collect.ImmutableSet;
28+
import dagger.producers.Producer;
29+
import javax.inject.Provider;
30+
31+
/**
32+
* A mapper for associating a {@link DependencyRequest.Kind} to a {@link BindingType}, dependent on
33+
* the type of code to be generated (e.g., for {@link Provider} or {@link Producer}).
34+
*/
35+
enum BindingTypeMapper {
36+
FOR_PROVIDER() {
37+
@Override public BindingType getBindingType(DependencyRequest.Kind requestKind) {
38+
switch (requestKind) {
39+
case INSTANCE:
40+
case PROVIDER:
41+
case PROVIDER_OF_LAZY:
42+
case LAZY:
43+
return PROVISION;
44+
case MEMBERS_INJECTOR:
45+
return MEMBERS_INJECTION;
46+
case PRODUCED:
47+
case PRODUCER:
48+
throw new IllegalArgumentException(requestKind.toString());
49+
default:
50+
throw new AssertionError(requestKind);
51+
}
52+
}
53+
},
54+
FOR_PRODUCER() {
55+
@Override public BindingType getBindingType(DependencyRequest.Kind requestKind) {
56+
switch (requestKind) {
57+
case INSTANCE:
58+
case PRODUCED:
59+
case PRODUCER:
60+
return PRODUCTION;
61+
case PROVIDER:
62+
case PROVIDER_OF_LAZY:
63+
case LAZY:
64+
return PROVISION;
65+
case MEMBERS_INJECTOR:
66+
return MEMBERS_INJECTION;
67+
default:
68+
throw new AssertionError(requestKind);
69+
}
70+
}
71+
};
72+
73+
static BindingTypeMapper forBindingType(BindingType bindingType) {
74+
return bindingType.equals(PRODUCTION) ? FOR_PRODUCER : FOR_PROVIDER;
75+
}
76+
77+
abstract BindingType getBindingType(DependencyRequest.Kind requestKind);
78+
79+
/**
80+
* Returns the {@link BindingType} to use for a collection of requests of the same
81+
* {@link BindingKey}. This allows factories to only take a single argument for multiple requests
82+
* of the same key.
83+
*/
84+
BindingType getBindingType(Iterable<DependencyRequest> requests) {
85+
ImmutableSet<BindingType> classes = FluentIterable.from(requests)
86+
.transform(new Function<DependencyRequest, BindingType>() {
87+
@Override public BindingType apply(DependencyRequest request) {
88+
return getBindingType(request.kind());
89+
}
90+
})
91+
.toSet();
92+
if (classes.size() == 1) {
93+
return getOnlyElement(classes);
94+
} else if (classes.equals(CONTRIBUTION_TYPES)) {
95+
return PROVISION;
96+
} else {
97+
throw new IllegalArgumentException("Bad set of framework classes: " + classes);
98+
}
99+
}
100+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import static com.google.auto.common.MoreElements.getAnnotationMirror;
2020
import static dagger.internal.codegen.ConfigurationAnnotations.enclosedBuilders;
21+
import static dagger.internal.codegen.ConfigurationAnnotations.getComponentDependencies;
2122
import static dagger.internal.codegen.ConfigurationAnnotations.getComponentModules;
2223
import static dagger.internal.codegen.ConfigurationAnnotations.getTransitiveModules;
24+
import static dagger.internal.codegen.ConfigurationAnnotations.validateComponentDependencies;
2325
import static dagger.internal.codegen.ErrorMessages.COMPONENT_ANNOTATED_REUSABLE;
2426
import static javax.lang.model.element.ElementKind.CLASS;
2527
import static javax.lang.model.element.ElementKind.INTERFACE;
@@ -239,6 +241,9 @@ public ComponentValidationReport validate(final TypeElement subject,
239241

240242
AnnotationMirror componentMirror =
241243
getAnnotationMirror(subject, componentKind.annotationType()).get();
244+
if (componentKind.isTopLevel()) {
245+
validateComponentDependencies(builder, getComponentDependencies(componentMirror));
246+
}
242247
ImmutableList<TypeMirror> moduleTypes = getComponentModules(componentMirror);
243248
moduleValidator.validateReferencedModules(
244249
subject, builder, moduleTypes, componentKind.moduleKinds());

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import javax.lang.model.util.ElementFilter;
4949
import javax.lang.model.util.Elements;
5050
import javax.lang.model.util.SimpleAnnotationValueVisitor6;
51+
import javax.lang.model.util.SimpleTypeVisitor6;
5152
import javax.lang.model.util.Types;
5253

5354
/**
@@ -110,6 +111,30 @@ static ImmutableList<TypeMirror> convertClassArrayToListOfTypes(
110111
return TO_LIST_OF_TYPES.visit(getAnnotationValue(annotationMirror, elementName), elementName);
111112
}
112113

114+
static <T extends Element> void validateComponentDependencies(
115+
ValidationReport.Builder<T> report, Iterable<TypeMirror> types) {
116+
validateTypesAreDeclared(report, types, "component dependency");
117+
}
118+
119+
private static <T extends Element> void validateTypesAreDeclared(
120+
final ValidationReport.Builder<T> report, Iterable<TypeMirror> types, final String typeName) {
121+
for (TypeMirror type : types) {
122+
type.accept(new SimpleTypeVisitor6<Void, Void>(){
123+
@Override
124+
protected Void defaultAction(TypeMirror e, Void aVoid) {
125+
report.addError(String.format("%s is not a valid %s type", e, typeName));
126+
return null;
127+
}
128+
129+
@Override
130+
public Void visitDeclared(DeclaredType t, Void aVoid) {
131+
// Declared types are valid
132+
return null;
133+
}
134+
}, null);
135+
}
136+
}
137+
113138
private static final AnnotationValueVisitor<ImmutableList<TypeMirror>, String> TO_LIST_OF_TYPES =
114139
new SimpleAnnotationValueVisitor6<ImmutableList<TypeMirror>, String>() {
115140
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ FactoryCreationStrategy factoryCreationStrategy() {
214214
final TypeMirror factoryType() {
215215
switch (contributionType()) {
216216
case MAP:
217-
return MapType.from(key()).unwrappedValueType(frameworkClass());
217+
return MapType.from(key()).unwrappedValueType(bindingType().frameworkClass());
218218
case SET:
219219
return SetType.from(key()).elementType();
220220
case SET_VALUES:

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

Lines changed: 0 additions & 104 deletions
This file was deleted.

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ abstract class FrameworkDependency {
5757
*/
5858
abstract BindingKey bindingKey();
5959

60-
/**
61-
* The framework class to use for these requests.
62-
*/
63-
abstract Class<?> frameworkClass();
60+
/** The binding type of the framework dependency. */
61+
abstract BindingType bindingType();
62+
63+
/** The framework class to use for these requests. */
64+
final Class<?> frameworkClass() {
65+
return bindingType().frameworkClass();
66+
}
6467

6568
/**
6669
* The dependency requests that are all satisfied by one framework instance.
@@ -99,8 +102,8 @@ abstract class FrameworkDependency {
99102
* instances of Binding, because it really depends on the order of the binding's dependencies,
100103
* and two equal instances of Binding may have the same dependencies in a different order. */
101104
static ImmutableSet<FrameworkDependency> frameworkDependenciesForBinding(Binding binding) {
102-
DependencyRequestMapper dependencyRequestMapper =
103-
DependencyRequestMapper.forBindingType(binding.bindingType());
105+
BindingTypeMapper bindingTypeMapper =
106+
BindingTypeMapper.forBindingType(binding.bindingType());
104107
ImmutableSet.Builder<FrameworkDependency> frameworkDependencies = ImmutableSet.builder();
105108
for (Collection<DependencyRequest> requests : groupByUnresolvedKey(binding)) {
106109
frameworkDependencies.add(
@@ -109,7 +112,7 @@ static ImmutableSet<FrameworkDependency> frameworkDependenciesForBinding(Binding
109112
FluentIterable.from(requests)
110113
.transform(DependencyRequest.BINDING_KEY_FUNCTION)
111114
.toSet()),
112-
dependencyRequestMapper.getFrameworkClass(requests),
115+
bindingTypeMapper.getBindingType(requests),
113116
ImmutableSet.copyOf(requests)));
114117
}
115118
return frameworkDependencies.build();

0 commit comments

Comments
 (0)