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
@@ -115,37 +116,32 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
115116
116117 var roundContext = processingContext .createRoundContext (roundEnv );
117118
118- var externalDomainAnnotation = new HashSet <TypeElement >(1 );
119- var otherAnnotations = new HashSet <TypeElement >(functionMap .size () - 1 );
119+ var annotationMap =
120+ annotations .stream ()
121+ .collect (
122+ Collectors .toUnmodifiableMap (
123+ it -> it .getQualifiedName ().toString (), Function .identity ()));
120124
121- for (var annotation : annotations ) {
122- if (annotation .getQualifiedName ().contentEquals (EXTERNAL_DOMAIN )) {
123- externalDomainAnnotation .add (annotation );
124- } else {
125- otherAnnotations .add (annotation );
125+ for (var operation : operations ) {
126+ var annotation = annotationMap .get (operation .name );
127+ if (annotation == null ) {
128+ continue ;
126129 }
130+ var elements = roundContext .getElementsAnnotatedWith (annotation );
131+ if (elements .isEmpty ()) {
132+ continue ;
133+ }
134+ var processor = operation .function .apply (roundContext );
135+ processor .process (elements );
127136 }
128137
129- // process ExternalDomain annotation first
130- processWithRoundContext (externalDomainAnnotation , roundContext );
131-
132- // process other annotations
133- processWithRoundContext (otherAnnotations , roundContext );
134-
135138 return true ;
136139 }
137140
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- }
141+ private record Operation (String name , Function <RoundContext , ElementProcessor > function ) {
142+ Operation {
143+ Objects .requireNonNull (name );
144+ Objects .requireNonNull (function );
149145 }
150146 }
151147}
0 commit comments