2525import static org .seasar .doma .internal .apt .AnnotationTypes .EXTERNAL_DOMAIN ;
2626import static org .seasar .doma .internal .apt .AnnotationTypes .SCOPE ;
2727
28- import java .util .HashSet ;
29- import java .util .Map ;
28+ import java .util .List ;
29+ import java .util .Objects ;
3030import java .util .Set ;
3131import java .util .function .Function ;
32+ import java .util .stream .Collectors ;
3233import javax .annotation .processing .AbstractProcessor ;
3334import javax .annotation .processing .ProcessingEnvironment ;
3435import javax .annotation .processing .RoundEnvironment ;
8182})
8283public class DomaProcessor extends AbstractProcessor {
8384
84- private static final Map < String , Function < RoundContext , ElementProcessor >> functionMap =
85- Map .of (
86- EXTERNAL_DOMAIN , ExternalDomainProcessor ::new ,
87- DATA_TYPE , DataTypeProcessor ::new ,
88- DOMAIN , DomainProcessor ::new ,
89- DOMAIN_CONVERTERS , DomainConvertersProcessor ::new ,
90- EMBEDDABLE , EmbeddableProcessor ::new ,
91- ENTITY , EntityProcessor ::new ,
92- AGGREGATE_STRATEGY , AggregateStrategyProcessor ::new ,
93- DAO , DaoProcessor ::new ,
94- SCOPE , ScopeProcessor ::new );
85+ private static final List < Operation > operations =
86+ List .of (
87+ new Operation ( EXTERNAL_DOMAIN , ExternalDomainProcessor ::new ) ,
88+ new Operation ( DATA_TYPE , DataTypeProcessor ::new ) ,
89+ new Operation ( DOMAIN , DomainProcessor ::new ) ,
90+ new Operation ( DOMAIN_CONVERTERS , DomainConvertersProcessor ::new ) ,
91+ new Operation ( EMBEDDABLE , EmbeddableProcessor ::new ) ,
92+ new Operation ( ENTITY , EntityProcessor ::new ) ,
93+ new Operation ( AGGREGATE_STRATEGY , AggregateStrategyProcessor ::new ) ,
94+ new Operation ( DAO , DaoProcessor ::new ) ,
95+ new Operation ( SCOPE , ScopeProcessor ::new ) );
9596
9697 private ProcessingContext processingContext ;
9798
9899 @ Override
99100 public synchronized void init (ProcessingEnvironment processingEnv ) {
100101 super .init (processingEnv );
101- processingContext = new ProcessingContext (processingEnv );
102- processingContext .init ();
102+ processingContext = ProcessingContext .of (processingEnv );
103103 }
104104
105105 @ Override
@@ -115,37 +115,32 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
115115
116116 var roundContext = processingContext .createRoundContext (roundEnv );
117117
118- var externalDomainAnnotation = new HashSet <TypeElement >(1 );
119- var otherAnnotations = new HashSet <TypeElement >(functionMap .size () - 1 );
118+ var annotationMap =
119+ annotations .stream ()
120+ .collect (
121+ Collectors .toUnmodifiableMap (
122+ it -> it .getQualifiedName ().toString (), Function .identity ()));
120123
121- for (var annotation : annotations ) {
122- if (annotation .getQualifiedName ().contentEquals (EXTERNAL_DOMAIN )) {
123- externalDomainAnnotation .add (annotation );
124- } else {
125- otherAnnotations .add (annotation );
124+ for (var operation : operations ) {
125+ var annotation = annotationMap .get (operation .name );
126+ if (annotation == null ) {
127+ continue ;
126128 }
129+ var elements = roundContext .getElementsAnnotatedWith (annotation );
130+ if (elements .isEmpty ()) {
131+ continue ;
132+ }
133+ var processor = operation .function .apply (roundContext );
134+ processor .process (elements );
127135 }
128136
129- // process ExternalDomain annotation first
130- processWithRoundContext (externalDomainAnnotation , roundContext );
131-
132- // process other annotations
133- processWithRoundContext (otherAnnotations , roundContext );
134-
135137 return true ;
136138 }
137139
138- private void processWithRoundContext (Set <TypeElement > annotations , RoundContext roundContext ) {
139- for (var annotation : annotations ) {
140- String annotationName = annotation .getQualifiedName ().toString ();
141- var function = functionMap .get (annotationName );
142- if (function != null ) {
143- var elements = roundContext .getElementsAnnotatedWith (annotation );
144- if (!elements .isEmpty ()) {
145- var processor = function .apply (roundContext );
146- processor .process (elements );
147- }
148- }
140+ private record Operation (String name , Function <RoundContext , ElementProcessor > function ) {
141+ Operation {
142+ Objects .requireNonNull (name );
143+ Objects .requireNonNull (function );
149144 }
150145 }
151146}
0 commit comments