Skip to content

Commit

Permalink
An Annotation Processor to Process Version Annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
sklee committed Mar 14, 2017
1 parent 573f907 commit 71930fe
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Created by .ignore support plugin (hsz.mobi)
.gitignore
.idea/workspace.xml
out/
src/main/resources/
target/
55 changes: 55 additions & 0 deletions src/main/java/chapter1/annotation/VersionProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package chapter1.annotation;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.*;
import javax.tools.Diagnostic;
import java.util.Set;

/**
* Project: BeginningJava8LanguageFeatures
* FileName: VersionProcessor
* Date: 2017-03-14
* Time: 오전 9:21
* Author: Hadeslee
* Note:
* To change this template use File | Settings | File Templates.
*/
public class VersionProcessor extends AbstractProcessor {
public VersionProcessor() {

}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// Process all annotations
for (TypeElement currentAnnotation : annotations) {
javax.lang.model.element.Name qualifiedName = currentAnnotation.getQualifiedName();
// check if it is a Version annotation
if (qualifiedName.contentEquals("chapter1.annotation.Version")) {
// Look at all elements that have Version annotations
Set<? extends Element> annotatedElements;
annotatedElements = roundEnv.getElementsAnnotatedWith(
currentAnnotation);
for (Element element : annotatedElements) {
Version v = element.getAnnotation(Version.class);
int major = v.major();
int minor = v.minor();
if (major < 0 || minor < 0) {
// Print the error message
String errorMsg = "Version cannot" +
" be negative." +
" major=" + major +
" minor=" + minor;
Messager messager =
this.processingEnv.getMessager();
messager.printMessage(Diagnostic.Kind.ERROR,
errorMsg, element);
}
}
}
}
return true;
}
}

0 comments on commit 71930fe

Please sign in to comment.