Skip to content

Commit

Permalink
Fix bug in AnnotationExpression.
Browse files Browse the repository at this point in the history
When generating creator for annotation, the case when the return type is an array was ignored previously.

fixes #4262

RELNOTES=n/a
PiperOrigin-RevId: 618003551
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Mar 22, 2024
1 parent e6c2ac8 commit c213e36
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,15 @@ private static Set<XTypeElement> nestedAnnotationElements(
XTypeElement annotationElement, Set<XTypeElement> annotationElements) {
if (annotationElements.add(annotationElement)) {
for (XMethodElement method : annotationElement.getDeclaredMethods()) {
XTypeElement returnType = method.getReturnType().getTypeElement();
XType returnType = method.getReturnType();
XTypeElement maybeAnnotationType =
isArray(returnType)
? asArray(returnType).getComponentType().getTypeElement()
: returnType.getTypeElement();
// Return type may be null if it doesn't return a type or type is not known
if (returnType != null && returnType.isAnnotationClass()) {
if (maybeAnnotationType != null && maybeAnnotationType.isAnnotationClass()) {
// Ignore the return value since this method is just an accumulator method.
nestedAnnotationElements(returnType, annotationElements);
nestedAnnotationElements(maybeAnnotationType, annotationElements);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@ public final class ComplexMapKeysInDifferentOrderTest {
@interface ComplexMapKey {
int i();
int j();

NestKey[] nestKeys() default {};
}

@Retention(RUNTIME)
@MapKey(unwrapValue = false)
@interface NestKey {
int i() default 0;

String j() default "";
}

@Module
interface TestModule {
@Provides
@IntoMap
@ComplexMapKey(i = 1, j = 2)
@ComplexMapKey(i = 1, j = 2, nestKeys = @NestKey)
static int inOrder() {
return 3;
}
Expand All @@ -66,12 +76,21 @@ interface TestComponent {
public void test() {
Map<ComplexMapKey, Integer> map =
DaggerComplexMapKeysInDifferentOrderTest_TestComponent.create().map();
assertThat(map.get(mapKey(1, 2))).isEqualTo(3);
assertThat(map.get(mapKey(5, 4))).isEqualTo(6);
assertThat(map)
.containsEntry(
mapKey(
1,
2,
new NestKey[] {
new AutoAnnotation_ComplexMapKeysInDifferentOrderTest_ComplexMapKeyCreator_createNestKey(
0, "")
}),
3);
assertThat(map).containsEntry(mapKey(5, 4, new NestKey[] {}), 6);
}

@AutoAnnotation
static ComplexMapKey mapKey(int i, int j) {
return new AutoAnnotation_ComplexMapKeysInDifferentOrderTest_mapKey(i, j);
static ComplexMapKey mapKey(int i, int j, NestKey[] nestKeys) {
return new AutoAnnotation_ComplexMapKeysInDifferentOrderTest_mapKey(i, j, nestKeys);
}
}
63 changes: 63 additions & 0 deletions javatests/dagger/internal/codegen/MapKeyProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,67 @@ public void nestedMapKeyCreatorFile() {
.and()
.generatesSources(generatedKeyCreator);
}

@Test
public void nestedComplexMapKey_buildSuccessfully() {
JavaFileObject outerKey =
JavaFileObjects.forSourceLines(
"test.OuterKey",
"package test;",
"import dagger.MapKey;",
"import java.lang.annotation.Retention;",
"import static java.lang.annotation.RetentionPolicy.RUNTIME;",
"",
"@MapKey(unwrapValue = false)",
"public @interface OuterKey {",
" String value() default \"hello\";",
" NestedKey[] nestedKeys() default {};",
"}");
JavaFileObject nestedKey =
JavaFileObjects.forSourceLines(
"test.NestedKey",
"package test;",
"import dagger.MapKey;",
"import java.lang.annotation.Retention;",
"import static java.lang.annotation.RetentionPolicy.RUNTIME;",
"",
"@MapKey(unwrapValue = false)",
"public @interface NestedKey {",
" String value() default \"hello\";",
" String otherValue() default \"world\";",
"}");
JavaFileObject foo =
JavaFileObjects.forSourceLines(
"test.FooModule",
"package test;",
"",
"import dagger.multibindings.IntoMap;",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"public final class FooModule {",
" @IntoMap",
" @OuterKey(nestedKeys = @NestedKey)",
" @Provides",
" String provideString() { return \"hello\";}",
"}");
JavaFileObject component =
JavaFileObjects.forSourceLines(
"test.MyComponent",
"package test;",
"",
"import dagger.Component;",
"import java.util.Map;",
"",
"@Component(modules = FooModule.class)",
"public interface MyComponent {",
" Map<OuterKey, String> getFoo();",
"}");
assertAbout(javaSources())
.that(ImmutableList.of(outerKey, nestedKey, foo, component))
.withCompilerOptions(compilerMode.javacopts())
.processedWith(new ComponentProcessor(), new AutoAnnotationProcessor())
.compilesWithoutError();
}
}

0 comments on commit c213e36

Please sign in to comment.