Created
February 21, 2016 18:13
-
-
Save ip-imamoto/d70af439b008caf58276 to your computer and use it in GitHub Desktop.
ToDoProcessorクラス(コンパイル時の処理を記述)
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
package todo; | |
import javax.annotation.processing.*; | |
import javax.lang.model.SourceVersion; | |
import javax.lang.model.element.Element; | |
import javax.lang.model.element.TypeElement; | |
import javax.tools.Diagnostic.Kind; | |
import java.time.LocalDate; | |
import java.time.format.DateTimeFormatter; | |
import java.util.Set; | |
@SupportedSourceVersion(SourceVersion.RELEASE_8) | |
@SupportedAnnotationTypes("todo.ToDo") | |
public class ToDoProcessor extends AbstractProcessor { | |
@Override | |
public boolean process(Set<? extends TypeElement> typeElements, RoundEnvironment roundEnv) { | |
Messager messager = super.processingEnv.getMessager(); | |
for (TypeElement typeElement : typeElements) { | |
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(typeElement); | |
for (Element element : elements) { | |
ToDo todo = element.getAnnotation(ToDo.class); | |
messager.printMessage(Kind.WARNING, "@ToDo at " + element + " : " + todo.message()); | |
LocalDate expirationDate = LocalDate.parse(todo.expiration(), DateTimeFormatter.ofPattern("yyyyMMdd")); | |
if (LocalDate.now().isAfter(expirationDate)) { | |
messager.printMessage(Kind.ERROR, "ToDoの対応期限が切れています。対応期限" + expirationDate, element); | |
} | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment