br.com.jarch.apt.processor.PeriodProcessor Maven / Gradle / Ivy
The newest version!
package br.com.jarch.apt.processor;
import br.com.jarch.core.annotation.JArchValidPeriod;
import br.com.jarch.apt.util.ProcessorUtils;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@SupportedAnnotationTypes({"br.com.jarch.core.annotation.JArchValidPeriod", "br.com.jarch.core.annotation.JArchValidPeriod.List"})
public class PeriodProcessor extends AbstractProcessor {
@Override
public boolean process(final Set extends TypeElement> annotations, final RoundEnvironment roundEnv) {
try {
ProcessorUtils.processingEnvironment = processingEnv;
ProcessorUtils.messageNote("Analyzing: " + getClass().getSimpleName());
roundEnv
.getElementsAnnotatedWithAny(Set.of(JArchValidPeriod.class, JArchValidPeriod.List.class))
.forEach(element -> {
ProcessorUtils.messageAnalyzing(getClass(), element);
validPeriod(element);
});
} catch (Exception ex) {
ProcessorUtils.messageError("JARCH ERROR: Erro no APT " + getClass().getSimpleName() + " Mensagem: " + ex.getMessage());
}
return false;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}
private void validPeriod(Element element) {
for (JArchValidPeriod anotacao : element.getAnnotationsByType(JArchValidPeriod.class)) {
final TypeElement classeElement = (TypeElement) element;
existsFieldPeriod(processingEnv, element, classeElement, anotacao.fieldBegin());
existsFieldPeriod(processingEnv, element, classeElement, anotacao.fieldEnd());
validaTypesEquals(processingEnv, element, classeElement, anotacao.fieldBegin(), anotacao.fieldEnd());
}
}
private static void existsFieldPeriod(ProcessingEnvironment processingEnv, Element elementAtual, TypeElement classeElement, String field) {
List> classTypes = new ArrayList<>();
classTypes.add(Date.class);
classTypes.add(LocalDate.class);
classTypes.add(YearMonth.class);
if (!ProcessorUtils.existsFieldType(classeElement, field, classTypes)) {
ProcessorUtils.messageError("JARCH ERROR: Atributo " + field + " dos tipos [" + classTypes.stream().map(c -> c.getName()).collect(Collectors.joining(", ")) + "] não localizado na anotação @JArchValidPeriod", elementAtual);
}
}
private static void validaTypesEquals(ProcessingEnvironment processingEnv, Element elementAtual, TypeElement classeElement, String field1, String field2) {
try {
String classAttribute1 = ProcessorUtils.classTypeField(classeElement, field1);
String classAttribute2 = ProcessorUtils.classTypeField(classeElement, field2);
if (!classAttribute1.equals(classAttribute2)) {
throw new Exception("Tipo do atributo " + field1 + " (" + classAttribute1 + ")" + " diferente do tipo atributo " + field2 + " (" + classAttribute2 + ")" + " na anotação @JArchValidPeriod");
}
} catch (Exception ex) {
ProcessorUtils.messageError("JARCH ERROR: " + ex.getMessage(), classeElement);
}
}
@Override
public Iterable extends Completion> getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) {
return super.getCompletions(element, annotation, member, userText);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy