Skip to content

Commit e2e4e9e

Browse files
authored
Merge pull request square#449 from google/moe_writing_branch_from_d434756d2aa3144dc68759128d0dba162e251742
Moe sync 08/25
2 parents 570d124 + 1c070ab commit e2e4e9e

15 files changed

+136
-82
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ the GitHub project's master branch.
138138

139139
## License
140140

141-
Copyright 2012 Square, Inc.
142-
Copyright 2012 Google, Inc.
141+
Copyright 2012 The Dagger Authors
143142

144143
Licensed under the Apache License, Version 2.0 (the "License");
145144
you may not use this file except in compliance with the License.

compiler/src/it/functional-tests/src/main/java/test/membersinject/MembersInjectComponent.java

+1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ interface MembersInjectComponent {
2424
void inject(ChildOfStringArray subfoo);
2525
void inject(ChildOfArrayOfParentOfStringArray subfoo);
2626
void inject(ChildOfPrimitiveIntArray subfoo);
27+
void inject(RawFrameworkTypes rawFrameworkTypes);
2728

2829
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) 2016 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 test.membersinject;
18+
19+
import dagger.Lazy;
20+
import dagger.MembersInjector;
21+
import javax.inject.Provider;
22+
23+
// https://github.com/google/dagger/issues/419
24+
@SuppressWarnings({"rawtypes", "unused"})
25+
class RawFrameworkTypes {
26+
void nonInjectMethodWithARawProvider(Provider rawProvider) {}
27+
void nonInjectMethodWithARawLazy(Lazy rawLazy) {}
28+
void nonInjectMethodWithARawMembersInjector(MembersInjector rawMembersInjector) {}
29+
}

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

-27
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717
package dagger.internal.codegen;
1818

1919
import static dagger.internal.codegen.Util.ENCLOSING_TYPE_ELEMENT;
20-
import static javax.lang.model.element.Modifier.ABSTRACT;
21-
import static javax.lang.model.element.Modifier.STATIC;
2220

2321
import com.google.common.base.Function;
2422
import com.google.common.base.Optional;
2523
import com.google.common.base.Predicate;
2624
import dagger.internal.codegen.Key.HasKey;
2725
import java.util.Set;
2826
import javax.lang.model.element.Element;
29-
import javax.lang.model.element.Modifier;
3027
import javax.lang.model.element.TypeElement;
3128

3229
/** An object that declares or specifies a binding. */
@@ -72,30 +69,6 @@ public Set<TypeElement> apply(BindingDeclaration bindingDeclaration) {
7269
}
7370
};
7471

75-
/**
76-
* {@code true} if {@link #contributingModule()} is present and this is a nonabstract instance
77-
* method.
78-
*/
79-
boolean requiresModuleInstance() {
80-
if (!bindingElement().isPresent() || !contributingModule().isPresent()) {
81-
return false;
82-
}
83-
Set<Modifier> modifiers = bindingElement().get().getModifiers();
84-
return !modifiers.contains(ABSTRACT) && !modifiers.contains(STATIC);
85-
}
86-
87-
/**
88-
* A predicate that passes for binding declarations for which {@link #requiresModuleInstance()} is
89-
* {@code true}.
90-
*/
91-
static final Predicate<BindingDeclaration> REQUIRES_MODULE_INSTANCE =
92-
new Predicate<BindingDeclaration>() {
93-
@Override
94-
public boolean apply(BindingDeclaration bindingDeclaration) {
95-
return bindingDeclaration.requiresModuleInstance();
96-
}
97-
};
98-
9972
/**
10073
* A predicate that passes for binding declarations for which {@link #bindingElement()} is
10174
* present.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ ImmutableSet<TypeElement> componentRequirements() {
129129
.preOrderTraversal(this)
130130
.transformAndConcat(RESOLVED_BINDINGS)
131131
.transformAndConcat(ResolvedBindings.CONTRIBUTION_BINDINGS)
132-
.filter(BindingDeclaration.REQUIRES_MODULE_INSTANCE)
132+
.filter(ContributionBinding.REQUIRES_MODULE_INSTANCE)
133133
.transformAndConcat(BindingDeclaration.CONTRIBUTING_MODULE)
134134
.filter(in(ownedModuleTypes()))
135135
.append(componentDescriptor().dependencies())

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,7 @@ public final ImmutableSet<Element> process(
151151
}
152152

153153
private void generateComponent(BindingGraph bindingGraph) {
154-
try {
155-
componentGenerator.generate(bindingGraph);
156-
} catch (SourceFileGenerationException e) {
157-
e.printMessageTo(messager);
158-
}
154+
componentGenerator.generate(bindingGraph, messager);
159155
}
160156

161157
private ImmutableSet<Element> getElementsFromAnnotations(

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

+27
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static dagger.internal.codegen.ContributionBinding.FactoryCreationStrategy.ENUM_INSTANCE;
2323
import static dagger.internal.codegen.MapKeys.unwrapValue;
2424
import static dagger.internal.codegen.MoreAnnotationMirrors.unwrapOptionalEquivalence;
25+
import static javax.lang.model.element.Modifier.ABSTRACT;
26+
import static javax.lang.model.element.Modifier.STATIC;
2527

2628
import com.google.auto.common.MoreTypes;
2729
import com.google.common.base.Equivalence;
@@ -44,6 +46,7 @@
4446
import javax.lang.model.element.AnnotationMirror;
4547
import javax.lang.model.element.AnnotationValue;
4648
import javax.lang.model.element.Element;
49+
import javax.lang.model.element.Modifier;
4750
import javax.lang.model.element.TypeElement;
4851
import javax.lang.model.type.DeclaredType;
4952
import javax.lang.model.type.TypeMirror;
@@ -167,6 +170,30 @@ static Kind forMultibindingKey(Key key) {
167170
*/
168171
protected abstract Kind bindingKind();
169172

173+
/**
174+
* {@code true} if {@link #contributingModule()} is present and this is a nonabstract instance
175+
* method.
176+
*/
177+
boolean requiresModuleInstance() {
178+
if (!bindingElement().isPresent() || !contributingModule().isPresent()) {
179+
return false;
180+
}
181+
Set<Modifier> modifiers = bindingElement().get().getModifiers();
182+
return !modifiers.contains(ABSTRACT) && !modifiers.contains(STATIC);
183+
}
184+
185+
/**
186+
* A predicate that passes for binding declarations for which {@link #requiresModuleInstance()} is
187+
* {@code true}.
188+
*/
189+
static final Predicate<ContributionBinding> REQUIRES_MODULE_INSTANCE =
190+
new Predicate<ContributionBinding>() {
191+
@Override
192+
public boolean apply(ContributionBinding bindingDeclaration) {
193+
return bindingDeclaration.requiresModuleInstance();
194+
}
195+
};
196+
170197
/**
171198
* The strategy for getting an instance of a factory for a {@link ContributionBinding}.
172199
*/

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,14 @@ enum Kind {
115115
* this kind that represents the dependency request.
116116
*/
117117
Optional<KindAndType> from(TypeMirror type) {
118-
return frameworkClass.isPresent() && isType(type) && isTypeOf(frameworkClass.get(), type)
119-
? Optional.of(this.ofType(getOnlyElement(asDeclared(type).getTypeArguments())))
120-
: Optional.<KindAndType>absent();
118+
if (frameworkClass.isPresent() && isType(type) && isTypeOf(frameworkClass.get(), type)) {
119+
List<? extends TypeMirror> typeArguments = asDeclared(type).getTypeArguments();
120+
if (typeArguments.isEmpty()) {
121+
return Optional.absent();
122+
}
123+
return Optional.of(this.ofType(getOnlyElement(typeArguments)));
124+
}
125+
return Optional.<KindAndType>absent();
121126
}
122127

123128
/**

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

+12-19
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919
import static dagger.internal.codegen.MapKeyGenerator.MapKeyCreatorSpecification.unwrappedMapKeyWithAnnotationValue;
2020
import static dagger.internal.codegen.MapKeyGenerator.MapKeyCreatorSpecification.wrappedMapKey;
2121
import static dagger.internal.codegen.MapKeys.getUnwrappedMapKeyType;
22+
import static javax.lang.model.element.ElementKind.ANNOTATION_TYPE;
23+
import static javax.lang.model.util.ElementFilter.typesIn;
2224

2325
import com.google.auto.common.BasicAnnotationProcessor;
24-
import com.google.auto.common.MoreElements;
2526
import com.google.auto.common.MoreTypes;
2627
import com.google.common.collect.ImmutableSet;
2728
import com.google.common.collect.SetMultimap;
2829
import dagger.MapKey;
29-
import dagger.internal.codegen.MapKeyGenerator.MapKeyCreatorSpecification;
3030
import java.lang.annotation.Annotation;
3131
import java.util.Set;
3232
import javax.annotation.processing.Messager;
3333
import javax.lang.model.element.Element;
34-
import javax.lang.model.element.ElementKind;
34+
import javax.lang.model.element.TypeElement;
3535
import javax.lang.model.type.DeclaredType;
3636
import javax.lang.model.util.Types;
3737

@@ -67,33 +67,26 @@ public Set<Class<? extends Annotation>> annotations() {
6767
@Override
6868
public Set<Element> process(
6969
SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
70-
for (Element element : elementsByAnnotation.get(MapKey.class)) {
71-
ValidationReport<Element> mapKeyReport = mapKeyValidator.validate(element);
70+
for (TypeElement mapKeyAnnotation : typesIn(elementsByAnnotation.get(MapKey.class))) {
71+
ValidationReport<Element> mapKeyReport = mapKeyValidator.validate(mapKeyAnnotation);
7272
mapKeyReport.printMessagesTo(messager);
7373

7474
if (mapKeyReport.isClean()) {
75-
MapKey mapkey = element.getAnnotation(MapKey.class);
75+
MapKey mapkey = mapKeyAnnotation.getAnnotation(MapKey.class);
7676
if (mapkey.unwrapValue()) {
7777
DeclaredType keyType =
78-
getUnwrappedMapKeyType(MoreTypes.asDeclared(element.asType()), types);
79-
if (keyType.asElement().getKind() == ElementKind.ANNOTATION_TYPE) {
80-
writeCreatorClass(
78+
getUnwrappedMapKeyType(MoreTypes.asDeclared(mapKeyAnnotation.asType()), types);
79+
if (keyType.asElement().getKind().equals(ANNOTATION_TYPE)) {
80+
mapKeyGenerator.generate(
8181
unwrappedMapKeyWithAnnotationValue(
82-
MoreElements.asType(element), MoreTypes.asTypeElement(keyType)));
82+
mapKeyAnnotation, MoreTypes.asTypeElement(keyType)),
83+
messager);
8384
}
8485
} else {
85-
writeCreatorClass(wrappedMapKey(MoreElements.asType(element)));
86+
mapKeyGenerator.generate(wrappedMapKey(mapKeyAnnotation), messager);
8687
}
8788
}
8889
}
8990
return ImmutableSet.of();
9091
}
91-
92-
private void writeCreatorClass(MapKeyCreatorSpecification mapKeyCreatorType) {
93-
try {
94-
mapKeyGenerator.generate(mapKeyCreatorType);
95-
} catch (SourceFileGenerationException e) {
96-
e.printMessageTo(messager);
97-
}
98-
}
9992
}

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

+7-13
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ public Set<Element> process(
148148
for (ModuleMethodFactoryGenerator generator : moduleMethodFactoryGenerators) {
149149
for (ExecutableElement method :
150150
elementsWithAnnotation(moduleMethods, generator.factoryMethodAnnotation())) {
151-
try {
152-
generator.generate(method, moduleElement);
153-
} catch (SourceFileGenerationException e) {
154-
e.printMessageTo(messager);
155-
}
151+
generator.generate(method, moduleElement, messager);
156152
}
157153
}
158154
}
@@ -197,8 +193,7 @@ interface ModuleMethodFactoryGenerator {
197193
Class<? extends Annotation> factoryMethodAnnotation();
198194

199195
/** Generates the factory source file for the given method and module. */
200-
void generate(ExecutableElement method, TypeElement moduleElement)
201-
throws SourceFileGenerationException;
196+
void generate(ExecutableElement method, TypeElement moduleElement, Messager messager);
202197
}
203198

204199
private static final class ProvisionModuleMethodFactoryGenerator
@@ -219,9 +214,9 @@ public Class<? extends Annotation> factoryMethodAnnotation() {
219214
}
220215

221216
@Override
222-
public void generate(ExecutableElement method, TypeElement moduleElement)
223-
throws SourceFileGenerationException {
224-
factoryGenerator.generate(provisionBindingFactory.forProvidesMethod(method, moduleElement));
217+
public void generate(ExecutableElement method, TypeElement moduleElement, Messager messager) {
218+
factoryGenerator.generate(
219+
provisionBindingFactory.forProvidesMethod(method, moduleElement), messager);
225220
}
226221
}
227222

@@ -244,10 +239,9 @@ public Class<? extends Annotation> factoryMethodAnnotation() {
244239
}
245240

246241
@Override
247-
public void generate(ExecutableElement method, TypeElement moduleElement)
248-
throws SourceFileGenerationException {
242+
public void generate(ExecutableElement method, TypeElement moduleElement, Messager messager) {
249243
producerFactoryGenerator.generate(
250-
productionBindingFactory.forProducesMethod(method, moduleElement));
244+
productionBindingFactory.forProducesMethod(method, moduleElement), messager);
251245
}
252246
}
253247
}

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ public Set<? extends Class<? extends Annotation>> annotations() {
5050
public Set<Element> process(
5151
SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
5252
for (Element element : elementsByAnnotation.values()) {
53-
try {
54-
monitoringModuleGenerator.generate(MoreElements.asType(element));
55-
} catch (SourceFileGenerationException e) {
56-
e.printMessageTo(messager);
57-
}
53+
monitoringModuleGenerator.generate(MoreElements.asType(element), messager);
5854
}
5955
return ImmutableSet.of();
6056
}

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ public Set<? extends Class<? extends Annotation>> annotations() {
5050
public Set<Element> process(
5151
SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
5252
for (Element element : elementsByAnnotation.values()) {
53-
try {
54-
productionExecutorModuleGenerator.generate(MoreElements.asType(element));
55-
} catch (SourceFileGenerationException e) {
56-
e.printMessageTo(messager);
57-
}
53+
productionExecutorModuleGenerator.generate(MoreElements.asType(element), messager);
5854
}
5955
return ImmutableSet.of();
6056
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.io.Writer;
3434
import javax.annotation.Generated;
3535
import javax.annotation.processing.Filer;
36+
import javax.annotation.processing.Messager;
3637
import javax.lang.model.element.Element;
3738
import javax.lang.model.util.Elements;
3839
import javax.tools.JavaFileObject;
@@ -61,6 +62,18 @@ abstract class SourceFileGenerator<T> {
6162
generatedAnnotationAvailable = elements.getTypeElement("javax.annotation.Generated") != null;
6263
}
6364

65+
/**
66+
* Generates a source file to be compiled for {@code T}. Writes any generation exception to {@code
67+
* messager} and does not throw.
68+
*/
69+
void generate(T input, Messager messager) {
70+
try {
71+
generate(input);
72+
} catch (SourceFileGenerationException e) {
73+
e.printMessageTo(messager);
74+
}
75+
}
76+
6477
/** Generates a source file to be compiled for {@code T}. */
6578
void generate(T input) throws SourceFileGenerationException {
6679
ClassName generatedTypeName = nameGeneratedType(input);

compiler/src/test/java/dagger/internal/codegen/MembersInjectionTest.java

+32
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.truth.Truth.assertAbout;
2020
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
21+
import static com.google.testing.compile.JavaSourcesSubject.assertThat;
2122
import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;
2223
import static dagger.internal.codegen.ErrorMessages.INJECT_INTO_PRIVATE_CLASS;
2324
import static dagger.internal.codegen.GeneratedLines.GENERATED_ANNOTATION;
@@ -1082,4 +1083,35 @@ public void fieldInjectionForShadowedMember() {
10821083
.processedWith(new ComponentProcessor())
10831084
.compilesWithoutError();
10841085
}
1086+
1087+
@Test public void rawFrameworkTypes() {
1088+
JavaFileObject file =
1089+
JavaFileObjects.forSourceLines(
1090+
"test.RawFrameworkTypes",
1091+
"package test;",
1092+
"",
1093+
"import dagger.Component;",
1094+
"import javax.inject.Inject;",
1095+
"import javax.inject.Provider;",
1096+
"",
1097+
"class RawProviderField {",
1098+
" @Inject Provider fieldWithRawProvider;",
1099+
"}",
1100+
"",
1101+
"class RawProviderParameter {",
1102+
" @Inject void methodInjection(Provider rawProviderParameter) {}",
1103+
"}",
1104+
"",
1105+
"@Component",
1106+
"interface C {",
1107+
" void inject(RawProviderField rawProviderField);",
1108+
" void inject(RawProviderParameter rawProviderParameter);",
1109+
"}");
1110+
assertThat(file)
1111+
.processedWith(new ComponentProcessor())
1112+
.failsToCompile()
1113+
.withErrorContaining("javax.inject.Provider cannot be provided").in(file).onLine(17)
1114+
.and()
1115+
.withErrorContaining("javax.inject.Provider cannot be provided").in(file).onLine(18);
1116+
}
10851117
}

0 commit comments

Comments
 (0)