Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Feb 17, 2023
2 parents a345790 + 324f01e commit 9180ee1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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=<your processor projects group id> \
-DartifactId=<your processor projects artifact id> \
-Dversion=<your version number> \
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.toolisticon.maven.archetypes</groupId>
<artifactId>annotationprocessor-archetype</artifactId>
<version>0.7.0</version>
<version>0.8.0</version>
<packaging>maven-archetype</packaging>

<name>annotationprocessor-archetype</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,57 +47,74 @@ public Set<String> getSupportedAnnotationTypes() {
@Override
public boolean processAnnotations(Set<? extends TypeElement> 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.
*
* Example how to use the templating engine.
*
* 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
Expand All @@ -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());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/archetype-resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<!-- project dependency versions -->
<cute.version>0.12.0</cute.version>
<spiap.version>0.11.0</spiap.version>
<aptk.version>0.20.0</aptk.version>
<aptk.version>0.20.2</aptk.version>

<!-- versions of test dependencies -->
<junit.version>4.13.1</junit.version>
Expand Down

0 comments on commit 9180ee1

Please sign in to comment.