Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.collect.Iterables.getOnlyElement;
import static dagger.internal.codegen.base.Util.reentrantComputeIfAbsent;
import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.assistedInjectedConstructors;
import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.isAssistedParameter;
import static dagger.internal.codegen.binding.InjectionAnnotations.injectedConstructors;
import static dagger.internal.codegen.binding.SourceFiles.factoryNameForElement;
import static dagger.internal.codegen.binding.SourceFiles.membersInjectorNameForType;
Expand Down Expand Up @@ -204,6 +205,8 @@ private ValidationReport validateConstructor(XConstructorElement constructorElem
.map(XAnnotations::asClassName)
.get();

boolean isAssistedInjectConstructor = XTypeNames.ASSISTED_INJECT.equals(injectAnnotation);

if (constructorElement.isPrivate()) {
builder.addError(
"Dagger does not support injection into private constructors", constructorElement);
Expand Down Expand Up @@ -243,6 +246,9 @@ private ValidationReport validateConstructor(XConstructorElement constructorElem
for (XExecutableParameterElement parameter : constructorElement.getParameters()) {
superficialValidation.validateTypeOf(parameter);
validateDependencyRequest(builder, parameter);
if (!isAssistedInjectConstructor && isAssistedParameter(parameter)) {
builder.addError(String.format("@Assisted parameter \"%s\" can only be used within an @AssistedInject-annotated constructor.", parameter.getName()), parameter);
}
}

if (throwsCheckedExceptions(constructorElement)) {
Expand Down Expand Up @@ -277,7 +283,7 @@ private ValidationReport validateConstructor(XConstructorElement constructorElem
// Note: superficial validation of the annotations is done as part of getting the scopes.
ImmutableSet<Scope> scopes =
injectionAnnotations.getScopes(constructorElement.getEnclosingElement());
if (injectAnnotation.equals(XTypeNames.ASSISTED_INJECT)) {
if (isAssistedInjectConstructor) {
for (Scope scope : scopes) {
builder.addError(
"A type with an @AssistedInject-annotated constructor cannot be scoped",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,9 +762,10 @@ public void testMultipleInjectAnnotations() {
.withProcessingOptions(compilerMode.processorOptions())
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorCount(2);
subject.hasErrorContaining(
"Constructors cannot be annotated with both @Inject and @AssistedInject");
subject.hasErrorContaining("@Assisted parameter \"i\" can only be used within an @AssistedInject-annotated constructor.");
});
}

Expand Down