diff --git a/dagger-compiler/main/java/dagger/internal/codegen/writing/FactoryGenerator.java b/dagger-compiler/main/java/dagger/internal/codegen/writing/FactoryGenerator.java index e7d4f27d076..c679cef4f3a 100644 --- a/dagger-compiler/main/java/dagger/internal/codegen/writing/FactoryGenerator.java +++ b/dagger-compiler/main/java/dagger/internal/codegen/writing/FactoryGenerator.java @@ -42,6 +42,7 @@ import static dagger.internal.codegen.xprocessing.XElements.asMethod; import static dagger.internal.codegen.xprocessing.XElements.asTypeElement; import static dagger.internal.codegen.xprocessing.XFunSpecs.constructorBuilder; +import static dagger.internal.codegen.xprocessing.XFunSpecs.getMethodReferenceName; import static dagger.internal.codegen.xprocessing.XFunSpecs.methodBuilder; import static dagger.internal.codegen.xprocessing.XTypeElements.typeVariableNames; import static dagger.internal.codegen.xprocessing.XTypeNames.factoryOf; @@ -409,7 +410,8 @@ private XFunSpec staticProxyMethodForProvision(ProvisionBinding binding) { } XCodeBlock arguments = copyParameters(builder, parameterNameSet, method.getParameters(), compilerOptions); - XCodeBlock invocation = XCodeBlock.of("%L.%N(%L)", module, referenceName(method), arguments); + XCodeBlock invocation = + XCodeBlock.of("%L.%N(%L)", module, getMethodReferenceName(method), arguments); Nullability nullability = Nullability.of(method); return builder @@ -419,24 +421,6 @@ private XFunSpec staticProxyMethodForProvision(ProvisionBinding binding) { .build(); } - /** - * Returns the name that should be used to reference the given binding method. - * - *
To ensure we properly handle internal visibility, we handle the reference differently - * depending on whether we're generating Java or Kotlin. - * - *
When generating Java, we use the (mangled) JVM name rather than the source name because Java - * sources do not have access to the source name of an internal element (even if they're in the - * same build unit). - * - *
When generating Kotlin, we use the source name rather than the JVM name because Kotlin - * sources do not have access to the (mangled) JVM name of an internal element, which should be - * fine since the generated factory should always be in the same build unit as the binding method. - */ - private String referenceName(XMethodElement method) { - return method.getJvmName(); - } - private XCodeBlock maybeWrapInCheckForNull(ProvisionBinding binding, XCodeBlock codeBlock) { return binding.shouldCheckForNull(compilerOptions) ? XCodeBlock.of( diff --git a/dagger-compiler/main/java/dagger/internal/codegen/writing/InjectionMethods.java b/dagger-compiler/main/java/dagger/internal/codegen/writing/InjectionMethods.java index 0d186027e63..a9eb8eb65d0 100644 --- a/dagger-compiler/main/java/dagger/internal/codegen/writing/InjectionMethods.java +++ b/dagger-compiler/main/java/dagger/internal/codegen/writing/InjectionMethods.java @@ -440,7 +440,7 @@ private static XCodeBlock copyParameterInternal( nullability, compilerOptions)); return isTypeNameAccessible - ? XCodeBlock.of("%L", name) - : XCodeBlock.ofCast(typeName, XCodeBlock.of("%L", name)); + ? XCodeBlock.of("%N", name) + : XCodeBlock.ofCast(typeName, XCodeBlock.of("%N", name)); } } diff --git a/dagger-compiler/main/java/dagger/internal/codegen/writing/MembersInjectorGenerator.java b/dagger-compiler/main/java/dagger/internal/codegen/writing/MembersInjectorGenerator.java index 328a9eb5115..1ca04f8bdab 100644 --- a/dagger-compiler/main/java/dagger/internal/codegen/writing/MembersInjectorGenerator.java +++ b/dagger-compiler/main/java/dagger/internal/codegen/writing/MembersInjectorGenerator.java @@ -42,6 +42,7 @@ import static dagger.internal.codegen.xprocessing.XElements.asTypeElement; import static dagger.internal.codegen.xprocessing.XElements.getSimpleName; import static dagger.internal.codegen.xprocessing.XFunSpecs.constructorBuilder; +import static dagger.internal.codegen.xprocessing.XFunSpecs.getMethodReferenceName; import static dagger.internal.codegen.xprocessing.XFunSpecs.methodBuilder; import static dagger.internal.codegen.xprocessing.XTypeElements.typeVariableNames; import static dagger.internal.codegen.xprocessing.XTypeNames.membersInjectorOf; @@ -181,7 +182,9 @@ private XFunSpec methodInjectionMethod(XMethodElement method, String methodName) XCodeBlock instance = copyInstance(builder, parameterNameSet, enclosingType.getType()); XCodeBlock arguments = copyParameters(builder, parameterNameSet, method.getParameters(), compilerOptions); - return builder.addStatement("%L.%N(%L)", instance, method.getJvmName(), arguments).build(); + return builder + .addStatement("%L.%N(%L)", instance, getMethodReferenceName(method), arguments) + .build(); } // Example: diff --git a/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XFunSpecs.java b/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XFunSpecs.java index c168c754f2b..5c64fca7509 100644 --- a/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XFunSpecs.java +++ b/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XFunSpecs.java @@ -48,6 +48,20 @@ // TODO(bcorso): Consider moving these methods into XProcessing library. /** A utility class for {@link XFunSpec} helper methods. */ public final class XFunSpecs { + /** + * Returns the name to use when referencing the given method based on the language of the + * generated code. + * + *
When referencing a method from a generated Java source, the method's JVM name is used. When
+ * referencing a method from a generated Kotlin source, the method's source name is used. This is
+ * particularly important for cases when a Kotlin method is annotated with {@code @JvmName} or has
+ * internal visibility since the source name won't be available to Java and the JVM name won't be
+ * available to Kotlin.
+ */
+ @SuppressWarnings("deprecation")
+ public static String getMethodReferenceName(XMethodElement method) {
+ return method.getJvmName();
+ }
/** Returns a {@link Builder} that overrides the given method. */
public static Builder overriding(
diff --git a/java/dagger/testing/compile/CompilerTests.java b/java/dagger/testing/compile/CompilerTests.java
index b66795db38d..5f66ad3628c 100644
--- a/java/dagger/testing/compile/CompilerTests.java
+++ b/java/dagger/testing/compile/CompilerTests.java
@@ -117,11 +117,17 @@ public static Source.JavaSource javaSource(String fileName, String... srcLines)
/** Returns a new {@link Source} with the content transformed by the given function. */
public static Source transformContent(
Source source, Function