Modifiable methods should always return publicly accessible types in case that their eventual subclass implementations need to implement the method in a package in which the type isn't accessible.
It's also possible that the return type isn't accessible in the base class implementation.
RELNOTES=n/a
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=221797313
diff --git a/java/dagger/internal/codegen/InjectionMethods.java b/java/dagger/internal/codegen/InjectionMethods.java
index 5d6e0c0..a419024 100644
--- a/java/dagger/internal/codegen/InjectionMethods.java
+++ b/java/dagger/internal/codegen/InjectionMethods.java
@@ -18,6 +18,7 @@
import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
+import static com.google.common.base.Preconditions.checkArgument;
import static com.squareup.javapoet.MethodSpec.methodBuilder;
import static dagger.internal.codegen.Accessibility.isElementAccessibleFrom;
import static dagger.internal.codegen.Accessibility.isRawTypeAccessible;
@@ -174,16 +175,32 @@
* requires the use of an injection method.
*/
static boolean requiresInjectionMethod(
- ProvisionBinding binding, CompilerOptions compilerOptions, String callingPackage) {
+ ProvisionBinding binding,
+ ImmutableList<Expression> arguments,
+ CompilerOptions compilerOptions,
+ String callingPackage,
+ DaggerTypes types) {
ExecutableElement method = MoreElements.asExecutable(binding.bindingElement().get());
return !binding.injectionSites().isEmpty()
|| binding.shouldCheckForNull(compilerOptions)
|| !isElementAccessibleFrom(method, callingPackage)
- || method
- .getParameters()
- .stream()
- .map(VariableElement::asType)
- .anyMatch(type -> !isRawTypeAccessible(type, callingPackage));
+ || !areParametersAssignable(method, arguments, types)
+ // This check should be removable once we drop support for -source 7
+ || method.getParameters().stream()
+ .map(VariableElement::asType)
+ .anyMatch(type -> !isRawTypeAccessible(type, callingPackage));
+ }
+
+ private static boolean areParametersAssignable(
+ ExecutableElement element, ImmutableList<Expression> arguments, DaggerTypes types) {
+ List<? extends VariableElement> parameters = element.getParameters();
+ checkArgument(parameters.size() == arguments.size());
+ for (int i = 0; i < parameters.size(); i++) {
+ if (!types.isAssignable(arguments.get(i).type(), parameters.get(i).asType())) {
+ return false;
+ }
+ }
+ return true;
}
/**