diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c45f3aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# Created by .ignore support plugin (hsz.mobi) +.gitignore +.idea/workspace.xml +out/ +src/main/resources/ +target/ diff --git a/src/main/java/chapter1/annotation/VersionProcessor.java b/src/main/java/chapter1/annotation/VersionProcessor.java new file mode 100644 index 0000000..8239a08 --- /dev/null +++ b/src/main/java/chapter1/annotation/VersionProcessor.java @@ -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 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 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; + } +}