diff --git a/README.MD b/README.MD index 88b8127..2dae8c9 100644 --- a/README.MD +++ b/README.MD @@ -21,7 +21,7 @@ You just need to create the project by using te following maven command mvn archetype:generate \ -DarchetypeGroupId=io.toolisticon.maven.archetypes \ -DarchetypeArtifactId=annotationprocessor-archetype \ - -DarchetypeVersion=0.7.0 \ + -DarchetypeVersion=0.8.0 \ -DgroupId= \ -DartifactId= \ -Dversion= \ diff --git a/pom.xml b/pom.xml index 2355050..76bda5d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ io.toolisticon.maven.archetypes annotationprocessor-archetype - 0.7.0 + 0.8.0 maven-archetype annotationprocessor-archetype diff --git a/src/main/resources/archetype-resources/__rootArtifactId__-processor/src/main/java/processor/__annotationName__Processor.java b/src/main/resources/archetype-resources/__rootArtifactId__-processor/src/main/java/processor/__annotationName__Processor.java index 12d5b09..c5b81bd 100644 --- a/src/main/resources/archetype-resources/__rootArtifactId__-processor/src/main/java/processor/__annotationName__Processor.java +++ b/src/main/resources/archetype-resources/__rootArtifactId__-processor/src/main/java/processor/__annotationName__Processor.java @@ -12,6 +12,7 @@ import io.toolisticon.aptk.tools.corematcher.AptkCoreMatchers; import io.toolisticon.aptk.tools.fluentvalidator.FluentElementValidator; import io.toolisticon.aptk.tools.generators.SimpleJavaWriter; +import io.toolisticon.aptk.tools.wrapper.TypeElementWrapper; import io.toolisticon.spiap.api.SpiService; import javax.annotation.processing.Processor; @@ -46,42 +47,59 @@ public Set getSupportedAnnotationTypes() { @Override public boolean processAnnotations(Set annotations, RoundEnvironment roundEnv) { - // process Services annotation - for (Element element : roundEnv.getElementsAnnotatedWith(${annotationName}.class)) { + if (!roundEnv.processingOver()) { + // process Services annotation + for (Element element : roundEnv.getElementsAnnotatedWith(${annotationName}.class)) { - // ---------------------------------------------------------- - // TODO: replace the following code by your business logic - // ---------------------------------------------------------- + TypeElementWrapper wrappedTypeElement = TypeElementWrapper.wrap((TypeElement) element); + ${annotationName}Wrapper annotation = ${annotationName}Wrapper.wrap(wrappedTypeElement.unwrap()); - // Some example validations : Annotation may only be applied on Classes with Noarg constructor. - FluentElementValidator.createFluentElementValidator(element) - .is(AptkCoreMatchers.IS_CLASS) - .applyValidator(AptkCoreMatchers.HAS_PUBLIC_NOARG_CONSTRUCTOR) - .validateAndIssueMessages(); + if (validateUsage(wrappedTypeElement, annotation)) { + processAnnotation(wrappedTypeElement, annotation); + } + } + } else { - // It's safe to cast to TypeElement now - TypeElement typeElement = (TypeElement) element; + // ProcessingOver round - // get annotation - ${annotationName}Wrapper annotation = ${annotationName}Wrapper.wrap(typeElement); + } + return false; - if(annotation.value().isEmpty()) { - MessagerUtils.error(typeElement, ${annotationName}ProcessorMessages.ERROR_VALUE_MUST_NOT_BE_EMPTY); - continue; - } + } - // Now get all attributes - createClass(typeElement, annotation); + void processAnnotation(TypeElementWrapper wrappedTypeElement, ${annotationName}Wrapper annotation) { - } + // ---------------------------------------------------------- + // TODO: replace the following code by your business logic + // ---------------------------------------------------------- - return false; + createClass(wrappedTypeElement, annotation); } + boolean validateUsage(TypeElementWrapper wrappedTypeElement, ${annotationName}Wrapper annotation) { + + // ---------------------------------------------------------- + // TODO: replace the following code by your business logic + // ---------------------------------------------------------- + + // Some example validations : Annotation may only be applied on Classes with Noarg constructor. + boolean result = wrappedTypeElement.validateWithFluentElementValidator() + .is(AptkCoreMatchers.IS_CLASS) + .applyValidator(AptkCoreMatchers.HAS_PUBLIC_NOARG_CONSTRUCTOR) + .validateAndIssueMessages(); + + if(annotation.value().isEmpty()) { + wrappedTypeElement.compilerMessage().asError().write(${annotationName}ProcessorMessages.ERROR_VALUE_MUST_NOT_BE_EMPTY); + result = false; + } + return result; + + } + /** * Generates a class. * @@ -89,14 +107,14 @@ public boolean processAnnotations(Set annotations, RoundE * * TODO: remove this * - * @param typeElement The TypeElement representing the annotated class + * @param wrappedTypeElement The TypeElement representing the annotated class * @param annotation The ${annotationName} annotation */ - private void createClass(TypeElement typeElement, ${annotationName}Wrapper annotation) { + private void createClass(TypeElementWrapper wrappedTypeElement, ${annotationName}Wrapper annotation) { // Now create class - String packageName = ((PackageElement) ElementUtils.AccessEnclosingElements.getFirstEnclosingElementOfKind(typeElement, ElementKind.PACKAGE)).getQualifiedName().toString(); + String packageName = wrappedTypeElement.getPackageName(); String className = annotation.value(); // Fill Model @@ -107,11 +125,11 @@ private void createClass(TypeElement typeElement, ${annotationName}Wrapper annot // create the class String filePath = packageName + "." + className; try { - SimpleJavaWriter javaWriter = FilerUtils.createSourceFile(filePath, typeElement); + SimpleJavaWriter javaWriter = FilerUtils.createSourceFile(filePath, wrappedTypeElement.unwrap()); javaWriter.writeTemplate("/${annotationName}.tpl", model); javaWriter.close(); } catch (IOException e) { - MessagerUtils.error(typeElement, ${annotationName}ProcessorMessages.ERROR_COULD_NOT_CREATE_CLASS, filePath, e.getMessage()); + wrappedTypeElement.compilerMessage().asError().write(${annotationName}ProcessorMessages.ERROR_COULD_NOT_CREATE_CLASS, filePath, e.getMessage()); } } diff --git a/src/main/resources/archetype-resources/pom.xml b/src/main/resources/archetype-resources/pom.xml index 3213c2e..ea73de4 100644 --- a/src/main/resources/archetype-resources/pom.xml +++ b/src/main/resources/archetype-resources/pom.xml @@ -75,7 +75,7 @@ 0.12.0 0.11.0 - 0.20.0 + 0.20.2 4.13.1