All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.jarch.apt.processor.OrderByProcessor Maven / Gradle / Ivy

The newest version!
package br.com.jarch.apt.processor;

import br.com.jarch.core.annotation.JArchOrderBy;
import br.com.jarch.core.annotation.JArchOrderByField;
import br.com.jarch.apt.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.core.annotation.JArchOrderBy")
public class OrderByProcessor extends AbstractProcessor {

    private final List listaAtributos = new ArrayList<>();

    @Override
    public boolean process(final Set annotations, final RoundEnvironment roundEnv) {
        try {
            ProcessorUtils.processingEnvironment = processingEnv;
            ProcessorUtils.messageNote("Analyzing: " + getClass().getSimpleName());

            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 getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) {
        return listaAtributos;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy