15
15
*/
16
16
package com .google .auto .common ;
17
17
18
+ import static com .google .common .collect .Multimaps .transformValues ;
18
19
import static com .google .common .truth .Truth .assertAbout ;
19
20
import static com .google .common .truth .Truth .assertThat ;
20
21
import static com .google .testing .compile .JavaSourceSubjectFactory .javaSource ;
21
22
import static com .google .testing .compile .JavaSourcesSubjectFactory .javaSources ;
22
- import static java .lang .annotation .ElementType .TYPE ;
23
23
import static javax .tools .StandardLocation .SOURCE_OUTPUT ;
24
24
25
25
import com .google .common .collect .ImmutableList ;
26
26
import com .google .common .collect .ImmutableSet ;
27
+ import com .google .common .collect .ImmutableSetMultimap ;
27
28
import com .google .common .collect .SetMultimap ;
29
+ import com .google .common .truth .Correspondence ;
28
30
import com .google .testing .compile .JavaFileObjects ;
29
31
import java .io .IOException ;
30
32
import java .io .PrintWriter ;
31
33
import java .lang .annotation .Annotation ;
32
34
import java .lang .annotation .Retention ;
33
35
import java .lang .annotation .RetentionPolicy ;
34
- import java .lang .annotation .Target ;
35
36
import java .util .Set ;
36
37
import javax .annotation .processing .Filer ;
37
38
import javax .lang .model .SourceVersion ;
@@ -51,9 +52,11 @@ public class BasicAnnotationProcessorTest {
51
52
/**
52
53
* Rejects elements unless the class generated by {@link GeneratesCode}'s processor is present.
53
54
*/
54
- public class RequiresGeneratedCodeProcessor extends BasicAnnotationProcessor {
55
+ private static final class RequiresGeneratedCodeProcessor extends BasicAnnotationProcessor {
55
56
56
- private int rejectedRounds ;
57
+ int rejectedRounds ;
58
+ final ImmutableList .Builder <ImmutableSetMultimap <Class <? extends Annotation >, Element >>
59
+ processArguments = ImmutableList .builder ();
57
60
58
61
@ Override
59
62
public SourceVersion getSupportedSourceVersion () {
@@ -67,6 +70,7 @@ protected Iterable<? extends ProcessingStep> initSteps() {
67
70
@ Override
68
71
public Set <Element > process (
69
72
SetMultimap <Class <? extends Annotation >, Element > elementsByAnnotation ) {
73
+ processArguments .add (ImmutableSetMultimap .copyOf (elementsByAnnotation ));
70
74
TypeElement requiredClass =
71
75
processingEnv .getElementUtils ().getTypeElement ("test.SomeGeneratedClass" );
72
76
if (requiredClass == null ) {
@@ -81,8 +85,24 @@ public Set<Element> process(
81
85
public Set <? extends Class <? extends Annotation >> annotations () {
82
86
return ImmutableSet .of (RequiresGeneratedCode .class );
83
87
}
88
+ },
89
+ new ProcessingStep () {
90
+ @ Override
91
+ public Set <? extends Class <? extends Annotation >> annotations () {
92
+ return ImmutableSet .of (AnAnnotation .class );
93
+ }
94
+
95
+ @ Override
96
+ public Set <? extends Element > process (
97
+ SetMultimap <Class <? extends Annotation >, Element > elementsByAnnotation ) {
98
+ return ImmutableSet .of ();
99
+ }
84
100
});
85
101
}
102
+
103
+ ImmutableList <ImmutableSetMultimap <Class <? extends Annotation >, Element >> processArguments () {
104
+ return processArguments .build ();
105
+ }
86
106
}
87
107
88
108
@ Retention (RetentionPolicy .SOURCE )
@@ -115,7 +135,6 @@ public Set<? extends Class<? extends Annotation>> annotations() {
115
135
}
116
136
}
117
137
118
- @ Target (TYPE )
119
138
public @interface AnAnnotation {}
120
139
121
140
/** When annotating a type {@code Foo}, generates a class called {@code FooXYZ}. */
@@ -250,11 +269,16 @@ public void properlyDefersProcessing_nestedTypeValidBeforeOuterType() {
250
269
251
270
@ Test
252
271
public void properlyDefersProcessing_rejectsElement () {
253
- JavaFileObject classAFileObject = JavaFileObjects .forSourceLines ("test.ClassA" ,
254
- "package test;" ,
255
- "" ,
256
- "@" + RequiresGeneratedCode .class .getCanonicalName (),
257
- "public class ClassA {}" );
272
+ JavaFileObject classAFileObject =
273
+ JavaFileObjects .forSourceLines (
274
+ "test.ClassA" ,
275
+ "package test;" ,
276
+ "" ,
277
+ "@" + RequiresGeneratedCode .class .getCanonicalName (),
278
+ "public class ClassA {" ,
279
+ " @" + AnAnnotation .class .getCanonicalName (),
280
+ " public void method() {}" ,
281
+ "}" );
258
282
JavaFileObject classBFileObject = JavaFileObjects .forSourceLines ("test.ClassB" ,
259
283
"package test;" ,
260
284
"" ,
@@ -270,6 +294,31 @@ public void properlyDefersProcessing_rejectsElement() {
270
294
.generatesFileNamed (
271
295
SOURCE_OUTPUT , "test" , "GeneratedByRequiresGeneratedCodeProcessor.java" );
272
296
assertThat (requiresGeneratedCodeProcessor .rejectedRounds ).isEqualTo (1 );
297
+
298
+ // Re b/118372780: Assert that the right deferred elements are passed back, and not any enclosed
299
+ // elements annotated with annotations from a different step.
300
+ assertThat (requiresGeneratedCodeProcessor .processArguments ())
301
+ .comparingElementsUsing (setMultimapValuesByString ())
302
+ .containsExactly (
303
+ ImmutableSetMultimap .of (RequiresGeneratedCode .class , "test.ClassA" ),
304
+ ImmutableSetMultimap .of (RequiresGeneratedCode .class , "test.ClassA" ))
305
+ .inOrder ();
306
+ }
307
+
308
+ private static <K , V >
309
+ Correspondence <SetMultimap <K , V >, SetMultimap <K , String >> setMultimapValuesByString () {
310
+ return new Correspondence <SetMultimap <K , V >, SetMultimap <K , String >>() {
311
+ @ Override
312
+ public boolean compare (SetMultimap <K , V > actual , SetMultimap <K , String > expected ) {
313
+ return ImmutableSetMultimap .copyOf (transformValues (actual , Object ::toString ))
314
+ .equals (expected );
315
+ }
316
+
317
+ @ Override
318
+ public String toString () {
319
+ return "is equivalent comparing multimap values by `toString()` to" ;
320
+ }
321
+ };
273
322
}
274
323
275
324
@ Test public void reportsMissingType () {
0 commit comments