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

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

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

import br.com.jarch.core.annotation.JArchValidExclusives;
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.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import java.util.Set;

@SupportedAnnotationTypes("br.com.jarch.core.annotation.JArchValidExclusives")
public class ExclusivesProcessor extends AbstractProcessor {

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

            for (Element element : roundEnv.getElementsAnnotatedWith(JArchValidExclusives.class)) {
                ProcessorUtils.messageAnalyzing(getClass(), element);

                final JArchValidExclusives anotacao = element.getAnnotation(JArchValidExclusives.class);

                if (anotacao.fields().length == 0) {
                    throw new Exception("O atributo fields deve ser preenchido");
                }

                if (anotacao.fields().length == 1) {
                    throw new Exception("Para validação de somente um atributo utilize @JArchValidExclusive na declaração do atributo");
                }

                if (anotacao.ignoreProcessor()) {
                    continue;
                }

                TypeElement classeElement = (TypeElement) element;
                String[] fields = anotacao.fields();

                for (String field : fields) {
                    existsField(element, classeElement, field);
                }
            }

        } 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 existsField(Element elementAtual, TypeElement classeElement, String field) {
        if (!ProcessorUtils.existsField(classeElement, field)) {
            ProcessorUtils.messageError("JARCH ERROR: Atributo " + field + " não localizado na anotação @JArchValidExclusives da classe " + classeElement.getQualifiedName().toString(), elementAtual);
        }
    }


    @Override
    public Iterable getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) {
        return super.getCompletions(element, annotation, member, userText);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy