Skip to content

Commit 5426d64

Browse files
bcorsoDagger Team
authored andcommitted
Add more info to error messages for cases reported in #3450.
This CL adds a more specific error message for cases which we don't ever expect to ever fail but somehow do, as reported in #3450. This should help users figure out narrow down what exactly in the source code is causing the failure. RELNOTES=N/A PiperOrigin-RevId: 804605184
1 parent c35a799 commit 5426d64

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

dagger-compiler/main/java/dagger/internal/codegen/binding/AssistedInjectionAnnotations.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
2323
import static dagger.internal.codegen.xprocessing.XElements.asConstructor;
2424
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
25+
import static dagger.internal.codegen.xprocessing.XElements.toStableString;
2526

2627
import androidx.room.compiler.codegen.XParameterSpec;
2728
import androidx.room.compiler.processing.XConstructorElement;
@@ -54,7 +55,22 @@
5455
public final class AssistedInjectionAnnotations {
5556
/** Returns the factory method for the given factory {@link XTypeElement}. */
5657
public static XMethodElement assistedFactoryMethod(XTypeElement factory) {
57-
return getOnlyElement(assistedFactoryMethods(factory));
58+
ImmutableSet<XMethodElement> factoryMethods = assistedFactoryMethods(factory);
59+
if (factoryMethods.size() != 1) {
60+
// TODO(bcorso): This check can be removed (and rely on Iterables.getOnlyElement() below) once
61+
// https://github.com/google/dagger/issues/3450#issuecomment-3108716712 is fixed. For now, we
62+
// use a more verbose, custom error message with more information to make it easier to debug.
63+
throw new IllegalStateException(
64+
"Expected exactly one factory method for " + toStableString(factory) + " but found: "
65+
+ factoryMethods.stream()
66+
.map(
67+
method ->
68+
toStableString(method.getEnclosingElement())
69+
+ "#"
70+
+ toStableString(method))
71+
.collect(toImmutableList()));
72+
}
73+
return getOnlyElement(factoryMethods);
5874
}
5975

6076
/** Returns the list of abstract factory methods for the given factory {@link XTypeElement}. */

dagger-compiler/main/java/dagger/internal/codegen/binding/BindsTypeChecker.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package dagger.internal.codegen.binding;
1818

1919
import static com.google.common.collect.Iterables.getOnlyElement;
20-
import static dagger.internal.codegen.extension.DaggerCollectors.onlyElement;
20+
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
2121
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
22+
import static dagger.internal.codegen.xprocessing.XElements.toStableString;
2223
import static dagger.internal.codegen.xprocessing.XProcessingEnvs.getUnboundedWildcardType;
2324
import static dagger.internal.codegen.xprocessing.XTypes.isAssignableTo;
2425
import static dagger.internal.codegen.xprocessing.XTypes.rewrapType;
2526

2627
import androidx.room.compiler.codegen.XTypeName;
28+
import androidx.room.compiler.processing.XMethodElement;
2729
import androidx.room.compiler.processing.XProcessingEnv;
2830
import androidx.room.compiler.processing.XType;
2931
import androidx.room.compiler.processing.XTypeElement;
@@ -87,12 +89,26 @@ private XType desiredAssignableType(XType leftHandSide, ContributionType contrib
8789
}
8890

8991
private ImmutableList<XType> methodParameterTypes(XType type, String methodName) {
90-
return ImmutableList.copyOf(
92+
ImmutableList<XMethodElement> methods =
9193
XTypeElements.getAllMethods(type.getTypeElement()).stream()
9294
.filter(method -> methodName.contentEquals(getSimpleName(method)))
93-
.collect(onlyElement())
94-
.asMemberOf(type)
95-
.getParameterTypes());
95+
.collect(toImmutableList());
96+
if (methods.size() != 1) {
97+
// TODO(bcorso): This check can be removed (and rely on Iterables.getOnlyElement() below) once
98+
// https://github.com/google/dagger/issues/3450#issuecomment-3108716712 is fixed. For now, we
99+
// use a more verbose, custom error message with more information to make it easier to debug.
100+
throw new IllegalStateException(
101+
"Expected exactly one factory method for " + toStableString(type.getTypeElement())
102+
+ " but found: "
103+
+ methods.stream()
104+
.map(
105+
method ->
106+
toStableString(method.getEnclosingElement())
107+
+ "#"
108+
+ toStableString(method))
109+
.collect(toImmutableList()));
110+
}
111+
return ImmutableList.copyOf(getOnlyElement(methods).asMemberOf(type).getParameterTypes());
96112
}
97113

98114
private XType methodParameterType(XType type, String methodName) {

0 commit comments

Comments
 (0)