-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented annotation processor base class.
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/groovy/com/xinra/nucleus/apt/NucleusProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.xinra.nucleus.apt; | ||
|
||
import com.squareup.javapoet.AnnotationSpec; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import javax.annotation.Generated; | ||
import javax.annotation.processing.AbstractProcessor; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.Element; | ||
import javax.tools.Diagnostic; | ||
|
||
/** | ||
* Base class for annotation Processors | ||
* | ||
* @author Erik Hofer | ||
*/ | ||
public abstract class NucleusProcessor extends AbstractProcessor { | ||
|
||
/** | ||
* Emits an error message (will be displayed in the IDE). | ||
* | ||
* @param element the element to which the error belongs | ||
* @param message the message (will be formatted with {@link String#format(String, Object...)}) | ||
* @param args arguments for the formatting | ||
*/ | ||
public void error(Element element, String message, Object... args) { | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, | ||
String.format(message, args), element); | ||
} | ||
|
||
/** | ||
* Returns {@link SourceVersion#latestSupported()}. | ||
*/ | ||
@Override | ||
public SourceVersion getSupportedSourceVersion() { | ||
return SourceVersion.latestSupported(); | ||
} | ||
|
||
/** | ||
* Returns a spec of @{@link Generated} with {@code value} set to the fully | ||
* qualified name of the annotation processor and {@code date} set to the | ||
* current date and time. | ||
* | ||
* @param comments the value of {@link Generated#comments()} (omitted if {@code null}) | ||
*/ | ||
public AnnotationSpec getGeneratedAnnotation(String comments) { | ||
AnnotationSpec.Builder builder = AnnotationSpec.builder(Generated.class) | ||
.addMember("value", "$S", this.getClass().getName()) | ||
.addMember("date", "$S", ZonedDateTime.now().format(DateTimeFormatter.ISO_INSTANT)); | ||
if(comments != null) { | ||
builder.addMember("comments", "$S", comments); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Returns a spec of @{@link Generated} with {@code value} set to the fully | ||
* qualified name of the annotation processor, {@code date} set to the | ||
* current date and time and without {@code comments}. | ||
*/ | ||
public AnnotationSpec getGeneratedAnnotation() { | ||
return getGeneratedAnnotation(null); | ||
} | ||
|
||
} |