11package io .toolisticon .spiap .processor ;
22
33import io .toolisticon .aptk .tools .AbstractAnnotationProcessor ;
4- import io .toolisticon .aptk .tools .AnnotationUtils ;
54import io .toolisticon .aptk .tools .ElementUtils ;
65import io .toolisticon .aptk .tools .FilerUtils ;
76import io .toolisticon .aptk .tools .MessagerUtils ;
8- import io .toolisticon .aptk .tools .ProcessingEnvironmentUtils ;
7+ import io .toolisticon .aptk .tools .TypeMirrorWrapper ;
98import io .toolisticon .aptk .tools .TypeUtils ;
109import io .toolisticon .aptk .tools .generators .SimpleResourceWriter ;
10+ import io .toolisticon .aptk .tools .wrapper .ElementWrapper ;
11+ import io .toolisticon .aptk .tools .wrapper .TypeElementWrapper ;
1112import io .toolisticon .spiap .api .OutOfService ;
1213import io .toolisticon .spiap .api .Service ;
1314import io .toolisticon .spiap .api .Services ;
1415
1516import javax .annotation .processing .RoundEnvironment ;
1617import javax .lang .model .element .Element ;
1718import javax .lang .model .element .TypeElement ;
18- import javax .lang .model .type .TypeMirror ;
1919import javax .tools .StandardLocation ;
2020import java .io .IOException ;
2121import java .io .StringWriter ;
2828/**
2929 * Annotation Processor for {@link Service} and {@link Services}.
3030 * Creates the service descriptor file in "/META-INF/services/fqnOfSpi".
31- * Additionally it creates a property file containing all additional metadata used by the ServiceLocator (like order, id and description)
31+ * Additionally, it creates a property file containing all additional metadata used by the ServiceLocator (like order, id and description)
3232 * to get rid of the api library as run time dependency.
3333 */
3434public class ServiceProcessor extends AbstractAnnotationProcessor {
3535
3636 private final static Set <String > SUPPORTED_ANNOTATIONS = createSupportedAnnotationSet (Services .class , Service .class );
3737
38- private final Map <String , SimpleResourceWriter > spiResourceFilePool = new HashMap <String , SimpleResourceWriter >();
39-
4038 private final ServiceImplMap serviceImplHashMap = new ServiceImplMap ();
4139
4240 /**
@@ -46,18 +44,14 @@ protected static class ServiceImplMap extends HashMap<String, Set<String>> {
4644
4745 /**
4846 * Convenience method to add a value.
49- * Creates a Set if no entry for passedkey can be found.
47+ * Creates a Set if no entry for passed key can be found.
5048 *
5149 * @param key the key to look for
5250 * @param value the value to store in keys set
5351 * @return the keys set
5452 */
5553 public Set <String > put (String key , String value ) {
56- Set <String > set = this .get (key );
57- if (set == null ) {
58- set = new HashSet <String >();
59- this .put (key , set );
60- }
54+ Set <String > set = this .computeIfAbsent (key , k -> new HashSet <>());
6155 set .add (value );
6256 return set ;
6357 }
@@ -73,7 +67,7 @@ public Set<String> getSupportedAnnotationTypes() {
7367 public boolean processAnnotations (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
7468
7569 if (!roundEnv .processingOver ()) {
76- processAnnotationsInternally (annotations , roundEnv );
70+ processAnnotationsInternally (roundEnv );
7771 } else {
7872 writeConfigurationFiles ();
7973 }
@@ -82,7 +76,7 @@ public boolean processAnnotations(Set<? extends TypeElement> annotations, RoundE
8276
8377 }
8478
85- private void processAnnotationsInternally (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
79+ private void processAnnotationsInternally (RoundEnvironment roundEnv ) {
8680
8781 // process Services annotation
8882 for (Element element : roundEnv .getElementsAnnotatedWith (Services .class )) {
@@ -93,7 +87,7 @@ private void processAnnotationsInternally(Set<? extends TypeElement> annotations
9387 }
9488
9589 // read annotation
96- ServicesWrapper servicesAnnotationWrapper = ServicesWrapper .wrapAnnotationOfElement (element );
90+ ServicesWrapper servicesAnnotationWrapper = ServicesWrapper .wrap (element );
9791
9892 for (ServiceWrapper serviceWrapper : servicesAnnotationWrapper .value ()) {
9993 processAnnotation (serviceWrapper , element );
@@ -112,7 +106,7 @@ private void processAnnotationsInternally(Set<? extends TypeElement> annotations
112106 }
113107
114108 // read annotation
115- ServiceWrapper serviceAnnotationWrapper = ServiceWrapper .wrapAnnotationOfElement (element );
109+ ServiceWrapper serviceAnnotationWrapper = ServiceWrapper .wrap (element );
116110
117111 processAnnotation (serviceAnnotationWrapper , element );
118112
@@ -141,31 +135,31 @@ private void processAnnotation(ServiceWrapper serviceAnnotationWrapper, Element
141135
142136 // Now create the service locator
143137
144- TypeElement typeElement = ElementUtils . CastElement . castClass ( annotatedElement );
138+ TypeElementWrapper typeElement = TypeElementWrapper . wrap (( TypeElement ) annotatedElement );
145139
146140 if (serviceAnnotationWrapper != null ) {
147141
148- Set <String > spiInterfaces = new HashSet <String >();
142+ Set <String > spiInterfaces = new HashSet <>();
149143 String spiAttributeTypes = serviceAnnotationWrapper .valueAsFqn ();
150144 if (spiAttributeTypes != null ) {
151145 spiInterfaces .add (spiAttributeTypes );
152146 }
153147
154148 for (String fqTypeName : spiInterfaces ) {
155149
156- TypeMirror typeMirror = TypeUtils . TypeRetrieval . getTypeMirror (fqTypeName );
157- TypeElement typeMirrorTypeElement = ( TypeElement ) ProcessingEnvironmentUtils . getTypes ().asElement ( typeMirror );
150+ TypeMirrorWrapper typeMirror = TypeMirrorWrapper . wrap (fqTypeName );
151+ TypeElementWrapper typeMirrorTypeElement = typeMirror . getTypeElement ().get ( );
158152
159153 //check if type is interface
160- if (!ElementUtils . CheckKindOfElement . isInterface (typeMirrorTypeElement )) {
161- MessagerUtils .error (annotatedElement , ServiceProcessorMessages .ERROR_VALUE_ATTRIBUTE_MUST_ONLY_CONTAIN_INTERFACES , typeMirrorTypeElement .getQualifiedName (). toString () );
154+ if (!typeMirrorTypeElement . isInterface ()) {
155+ MessagerUtils .error (annotatedElement , ServiceProcessorMessages .ERROR_VALUE_ATTRIBUTE_MUST_ONLY_CONTAIN_INTERFACES , typeMirrorTypeElement .getQualifiedName ());
162156 break ;
163157 }
164158
165159 // check if annotatedElement is assignable to spi interface == implements the spi interface
166- if (!TypeUtils .TypeComparison .isAssignableTo (typeElement , typeMirror )) {
160+ if (!TypeUtils .TypeComparison .isAssignableTo (typeElement . unwrap () , typeMirror . unwrap () )) {
167161
168- MessagerUtils .error (annotatedElement , ServiceProcessorMessages .ERROR_ANNOTATED_CLASS_MUST_IMPLEMENT_CONFIGURED_INTERFACES , typeMirrorTypeElement .getQualifiedName (). toString () );
162+ MessagerUtils .error (annotatedElement , ServiceProcessorMessages .ERROR_ANNOTATED_CLASS_MUST_IMPLEMENT_CONFIGURED_INTERFACES , typeMirrorTypeElement .getQualifiedName ());
169163 break ;
170164
171165 }
@@ -174,7 +168,7 @@ private void processAnnotation(ServiceWrapper serviceAnnotationWrapper, Element
174168 writePropertiesFile (typeElement , serviceAnnotationWrapper );
175169
176170 // put service provider implementations in map by using the SPI fqn as key
177- serviceImplHashMap .put (fqTypeName , typeElement .getQualifiedName (). toString () );
171+ serviceImplHashMap .put (fqTypeName , typeElement .getQualifiedName ());
178172
179173
180174 }
@@ -186,15 +180,15 @@ private void processAnnotation(ServiceWrapper serviceAnnotationWrapper, Element
186180 * This method generates the property files of configured service annotation values.
187181 * This allows us not to get rid of spiap-api dependency at runtime.
188182 *
189- * @param annotatedTypeElement the annotated element
190- * @param serviceAnnotationWrapper The Service annotations AnotationMirror
183+ * @param annotatedTypeElement the annotated element
184+ * @param serviceAnnotationWrapper The Service annotations AnnotationMirror
191185 */
192- private void writePropertiesFile (TypeElement annotatedTypeElement , ServiceWrapper serviceAnnotationWrapper ) {
186+ private void writePropertiesFile (TypeElementWrapper annotatedTypeElement , ServiceWrapper serviceAnnotationWrapper ) {
193187
194188
195189 String spiClassName = serviceAnnotationWrapper .valueAsFqn ();
196- ;
197- String serviceClassName = annotatedTypeElement .getQualifiedName (). toString () ;
190+
191+ String serviceClassName = annotatedTypeElement .getQualifiedName ();
198192 String fileName = "META-INF/spiap/" + spiClassName + "/" + serviceClassName + ".properties" ;
199193
200194 try {
@@ -209,7 +203,7 @@ private void writePropertiesFile(TypeElement annotatedTypeElement, ServiceWrappe
209203 properties .setProperty (Constants .PROPERTY_KEY_ID , !serviceAnnotationWrapper .id ().isEmpty () ? serviceAnnotationWrapper .id () : serviceClassName );
210204 properties .setProperty (Constants .PROPERTY_KEY_DESCRIPTION , serviceAnnotationWrapper .description ());
211205 properties .setProperty (Constants .PROPERTY_KEY_PRIORITY , "" + serviceAnnotationWrapper .priority ());
212- properties .setProperty (Constants .PROPERTY_KEY_OUT_OF_SERVICE , "" + ( AnnotationUtils . getAnnotationMirror ( annotatedTypeElement , OutOfService .class ) != null ));
206+ properties .setProperty (Constants .PROPERTY_KEY_OUT_OF_SERVICE , "" + annotatedTypeElement . getAnnotation ( OutOfService .class ). isPresent ( ));
213207
214208 StringWriter writer = new StringWriter ();
215209 properties .store (writer , "Property files used by the spi service locator" );
@@ -221,8 +215,7 @@ private void writePropertiesFile(TypeElement annotatedTypeElement, ServiceWrappe
221215
222216
223217 } catch (IOException e ) {
224- MessagerUtils .error (annotatedTypeElement , "Wasn't able to write service provider properties file for service ${0} for spi ${1}" , serviceClassName , spiClassName );
225- return ;
218+ MessagerUtils .error (annotatedTypeElement .unwrap (), "Wasn't able to write service provider properties file for service ${0} for spi ${1}" , serviceClassName , spiClassName );
226219 }
227220
228221 }
@@ -244,15 +237,15 @@ private void writeConfigurationFiles() {
244237 return ;
245238 }
246239
247- Set <String > allServiceImplementations = new HashSet <String >(entry .getValue ());
240+ Set <String > allServiceImplementations = new HashSet <>(entry .getValue ());
248241 allServiceImplementations .addAll (existingServiceImplementations );
249242
250243 try {
251244
252245 // write service provider file by using a template
253246 SimpleResourceWriter writer = FilerUtils .createResource (StandardLocation .CLASS_OUTPUT , "" , serviceProviderFile );
254247
255- Map <String , Object > model = new HashMap <String , Object >();
248+ Map <String , Object > model = new HashMap <>();
256249 model .put ("fqns" , allServiceImplementations );
257250
258251 writer .writeTemplateString ("!{for fqn: fqns}${fqn}\n !{/for}" , model );
@@ -294,7 +287,7 @@ protected Set<String> readServiceFile(String serviceProviderFile) {
294287
295288 MessagerUtils .info (null , "Reading existing service file : ${0}" , serviceProviderFile );
296289
297- Set <String > resultSet = new HashSet <String >();
290+ Set <String > resultSet = new HashSet <>();
298291
299292 try {
300293
0 commit comments