Skip to content

Commit 1900560

Browse files
bcorsoDagger Team
authored andcommitted
Internal changes
RELNOTES=N/A PiperOrigin-RevId: 865058516
1 parent 8c69595 commit 1900560

File tree

58 files changed

+1968
-1776
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1968
-1776
lines changed

dagger-compiler/main/java/dagger/internal/codegen/writing/FactoryGenerator.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import static dagger.internal.codegen.xprocessing.XElements.asMethod;
4343
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
4444
import static dagger.internal.codegen.xprocessing.XFunSpecs.constructorBuilder;
45+
import static dagger.internal.codegen.xprocessing.XFunSpecs.getMethodReferenceName;
4546
import static dagger.internal.codegen.xprocessing.XFunSpecs.methodBuilder;
4647
import static dagger.internal.codegen.xprocessing.XTypeElements.typeVariableNames;
4748
import static dagger.internal.codegen.xprocessing.XTypeNames.factoryOf;
@@ -409,7 +410,8 @@ private XFunSpec staticProxyMethodForProvision(ProvisionBinding binding) {
409410
}
410411
XCodeBlock arguments =
411412
copyParameters(builder, parameterNameSet, method.getParameters(), compilerOptions);
412-
XCodeBlock invocation = XCodeBlock.of("%L.%N(%L)", module, referenceName(method), arguments);
413+
XCodeBlock invocation =
414+
XCodeBlock.of("%L.%N(%L)", module, getMethodReferenceName(method), arguments);
413415

414416
Nullability nullability = Nullability.of(method);
415417
return builder
@@ -419,24 +421,6 @@ private XFunSpec staticProxyMethodForProvision(ProvisionBinding binding) {
419421
.build();
420422
}
421423

422-
/**
423-
* Returns the name that should be used to reference the given binding method.
424-
*
425-
* <p>To ensure we properly handle internal visibility, we handle the reference differently
426-
* depending on whether we're generating Java or Kotlin.
427-
*
428-
* <p>When generating Java, we use the (mangled) JVM name rather than the source name because Java
429-
* sources do not have access to the source name of an internal element (even if they're in the
430-
* same build unit).
431-
*
432-
* <p>When generating Kotlin, we use the source name rather than the JVM name because Kotlin
433-
* sources do not have access to the (mangled) JVM name of an internal element, which should be
434-
* fine since the generated factory should always be in the same build unit as the binding method.
435-
*/
436-
private String referenceName(XMethodElement method) {
437-
return method.getJvmName();
438-
}
439-
440424
private XCodeBlock maybeWrapInCheckForNull(ProvisionBinding binding, XCodeBlock codeBlock) {
441425
return binding.shouldCheckForNull(compilerOptions)
442426
? XCodeBlock.of(

dagger-compiler/main/java/dagger/internal/codegen/writing/InjectionMethods.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ private static XCodeBlock copyParameterInternal(
440440
nullability,
441441
compilerOptions));
442442
return isTypeNameAccessible
443-
? XCodeBlock.of("%L", name)
444-
: XCodeBlock.ofCast(typeName, XCodeBlock.of("%L", name));
443+
? XCodeBlock.of("%N", name)
444+
: XCodeBlock.ofCast(typeName, XCodeBlock.of("%N", name));
445445
}
446446
}

dagger-compiler/main/java/dagger/internal/codegen/writing/MembersInjectorGenerator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
4343
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
4444
import static dagger.internal.codegen.xprocessing.XFunSpecs.constructorBuilder;
45+
import static dagger.internal.codegen.xprocessing.XFunSpecs.getMethodReferenceName;
4546
import static dagger.internal.codegen.xprocessing.XFunSpecs.methodBuilder;
4647
import static dagger.internal.codegen.xprocessing.XTypeElements.typeVariableNames;
4748
import static dagger.internal.codegen.xprocessing.XTypeNames.membersInjectorOf;
@@ -181,7 +182,9 @@ private XFunSpec methodInjectionMethod(XMethodElement method, String methodName)
181182
XCodeBlock instance = copyInstance(builder, parameterNameSet, enclosingType.getType());
182183
XCodeBlock arguments =
183184
copyParameters(builder, parameterNameSet, method.getParameters(), compilerOptions);
184-
return builder.addStatement("%L.%N(%L)", instance, method.getJvmName(), arguments).build();
185+
return builder
186+
.addStatement("%L.%N(%L)", instance, getMethodReferenceName(method), arguments)
187+
.build();
185188
}
186189

187190
// Example:

dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XFunSpecs.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@
4848
// TODO(bcorso): Consider moving these methods into XProcessing library.
4949
/** A utility class for {@link XFunSpec} helper methods. */
5050
public final class XFunSpecs {
51+
/**
52+
* Returns the name to use when referencing the given method based on the language of the
53+
* generated code.
54+
*
55+
* <p>When referencing a method from a generated Java source, the method's JVM name is used. When
56+
* referencing a method from a generated Kotlin source, the method's source name is used. This is
57+
* particularly important for cases when a Kotlin method is annotated with {@code @JvmName} or has
58+
* internal visibility since the source name won't be available to Java and the JVM name won't be
59+
* available to Kotlin.
60+
*/
61+
@SuppressWarnings("deprecation")
62+
public static String getMethodReferenceName(XMethodElement method) {
63+
return method.getJvmName();
64+
}
5165

5266
/** Returns a {@link Builder} that overrides the given method. */
5367
public static Builder overriding(

java/dagger/testing/compile/CompilerTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,17 @@ public static Source.JavaSource javaSource(String fileName, String... srcLines)
117117
/** Returns a new {@link Source} with the content transformed by the given function. */
118118
public static Source transformContent(
119119
Source source, Function<String, String> contentTransformer) {
120-
return Source.Companion.java(
121-
// Remove the extension from the file name so that the file name.
122-
source.getRelativePath()
123-
.substring(0, source.getRelativePath().lastIndexOf('.')),
124-
contentTransformer.apply(source.getContents()));
120+
if (source instanceof Source.KotlinSource) {
121+
return Source.Companion.kotlin(
122+
source.getRelativePath(),
123+
contentTransformer.apply(source.getContents()));
124+
} else if (source instanceof Source.JavaSource) {
125+
return Source.Companion.java(
126+
((Source.JavaSource) source).getQName(),
127+
contentTransformer.apply(source.getContents()));
128+
} else {
129+
throw new AssertionError("Unexpected source type.");
130+
}
125131
}
126132

127133
/** Returns a {@link Compiler} instance with the given sources. */

javatests/dagger/functional/kotlinsrc/membersinject/subpackage/a/AGrandchild.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ class AGrandchild : BChild() {
2828
this.aGrandchildMethod = aGrandchildMethod
2929
}
3030

31+
// Note: In the Java source version of this code we use protected; however, in the Kotlin source
32+
// version we use public since Kotlin protected wouldn't allow access to the generated code.
3133
@Inject
32-
protected override fun aParentMethod(aParentMethod: APublicObject) {
34+
override fun aParentMethod(aParentMethod: APublicObject) {
3335
super.aParentMethod(aParentMethod)
3436
}
3537

36-
protected override fun aChildMethod(aChildMethod: APublicObject) {
38+
override fun aChildMethod(aChildMethod: APublicObject) {
3739
super.aChildMethod(aChildMethod)
3840
}
3941

javatests/dagger/functional/kotlinsrc/membersinject/subpackage/a/AParent.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ open class AParent {
2222
@Inject internal lateinit var aParentField: AInternalObject
2323
private var aParentMethod: APublicObject? = null
2424

25+
// Note: In the Java source version of this code we use protected; however, in the Kotlin source
26+
// version we use public since Kotlin protected wouldn't allow access to the generated code.
2527
@Inject
26-
protected open fun aParentMethod(aParentMethod: APublicObject) {
28+
open fun aParentMethod(aParentMethod: APublicObject) {
2729
this.aParentMethod = aParentMethod
2830
}
2931

javatests/dagger/functional/kotlinsrc/membersinject/subpackage/b/BChild.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ open class BChild : AParent() {
2424
@Inject internal lateinit var aChildField: BInternalObject
2525
private var aChildMethod: APublicObject? = null
2626

27+
// Note: In the Java source version of this code we use protected; however, in the Kotlin source
28+
// version we use public since Kotlin protected wouldn't allow access to the generated code.
2729
@Inject
28-
protected open fun aChildMethod(aChildMethod: APublicObject) {
30+
open fun aChildMethod(aChildMethod: APublicObject) {
2931
this.aChildMethod = aChildMethod
3032
}
3133

32-
protected override fun aParentMethod(aParentMethod: APublicObject) {
34+
override fun aParentMethod(aParentMethod: APublicObject) {
3335
super.aParentMethod(aParentMethod)
3436
}
3537

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2026 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 dagger.functional.membersinject;
18+
19+
import java.util.List;
20+
import javax.inject.Inject;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.JUnit4;
24+
25+
// TODO: b/477601223 - Remove this test once this bug is fixed, as it should already be covered by
26+
// MembersInjectionTest#membersInjectorSuperTypeWithInaccessibleTypeArgument().
27+
@SuppressWarnings("unused") // This test is just used to check that the code compiles.
28+
@RunWith(JUnit4.class)
29+
public class MembersInjectTypeParameterWithVarianceTest {
30+
public static class Foo<T> {
31+
@Inject T t;
32+
@Inject List<T> listT;
33+
@Inject List<? extends T> listExtendsT;
34+
@Inject List<? extends T>[] arrayListExtendsT;
35+
36+
@Inject
37+
void method(
38+
T t,
39+
List<T> listT,
40+
List<? extends T> listExtendsT,
41+
List<? extends T>[] arrayListExtendsT) {}
42+
}
43+
44+
@Test
45+
public void testBuild() {
46+
// If this compiles, then this test is WAI.
47+
}
48+
}

javatests/dagger/internal/codegen/ComponentProcessorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,8 @@ public void unprocessedMembersInjectorNotes() {
14791479
subject.hasWarningCount(0);
14801480

14811481
String generatedFileTemplate =
1482-
"dagger/internal/codegen/ComponentProcessorTestClasses_%s_MembersInjector.java";
1482+
"dagger/internal/codegen/ComponentProcessorTestClasses_%s_MembersInjector"
1483+
+ (compilerMode.isKotlinCodegenEnabled() ? ".kt" : ".java");
14831484
String noteTemplate =
14841485
"Generating a MembersInjector for "
14851486
+ "dagger.internal.codegen.ComponentProcessorTestClasses.%s.";

0 commit comments

Comments
 (0)