br.com.jarch.apt.processor.OrderByProcessor Maven / Gradle / Ivy
package br.com.jarch.apt.processor;
import br.com.jarch.annotation.JArchOrderBy;
import br.com.jarch.annotation.JArchOrderByField;
import br.com.jarch.util.ProcessorUtils;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Completion;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static javax.annotation.processing.Completions.of;
@SupportedAnnotationTypes("br.com.jarch.annotation.JArchOrderBy")
public class OrderByProcessor extends AbstractProcessor {
private final List listaAtributos = new ArrayList<>();
@Override
public boolean process(final Set extends TypeElement> annotations, final RoundEnvironment roundEnv) {
ProcessorUtils.processingEnvironment = processingEnv;
ProcessorUtils.messageMandatoryWarning("Processando: " + getClass().getSimpleName());
try {
for (Element element : roundEnv.getElementsAnnotatedWith(JArchOrderBy.class)) {
ProcessorUtils.messageAnalyzing(getClass(), element);
final JArchOrderBy jArchOrderBy = element.getAnnotation(JArchOrderBy.class);
if (jArchOrderBy.ignoreProcessor()) {
continue;
}
final TypeElement classeElement = (TypeElement) element;
listaAtributos.clear();
for (JArchOrderByField jArchOrderByField : jArchOrderBy.fields()) {
if (jArchOrderByField.ignoreProcessor()) {
continue;
}
boolean achou = false;
for (Element ee : classeElement.getEnclosedElements()) {
if (ee.getKind() != ElementKind.FIELD
|| !ee.getSimpleName().contentEquals(jArchOrderByField.value())) {
continue;
}
listaAtributos.add(of(jArchOrderByField.value()));
achou = true;
break;
}
TypeElement superClass = processingEnv.getElementUtils().getTypeElement(((TypeElement) element).getSuperclass().toString());
for (Element ee : superClass.getEnclosedElements()) {
if (ee.getKind() != ElementKind.FIELD
|| !ee.getSimpleName().contentEquals(jArchOrderByField.value())) {
continue;
}
listaAtributos.add(of(jArchOrderByField.value()));
achou = true;
break;
}
if (!achou) {
if (!ProcessorUtils.existsField(classeElement, jArchOrderByField.value())) {
ProcessorUtils.messageError("JARCH ERROR: Atributo " + jArchOrderByField + " não localizado na anotação @JArchOrderByField da classe " + classeElement.getQualifiedName().toString(), element);
}
}
}
}
} catch (Exception ex) {
ProcessorUtils.messageError("JARCH ERROR: Erro no APT " + getClass().getSimpleName() + " Mensagem: " + ex.getMessage());
}
return true;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}
@Override
public Iterable extends Completion> getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) {
return listaAtributos;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy