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

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

There is a newer version: 24.9.0
Show newest version
package br.com.jarch.apt.processor;

import br.com.jarch.annotation.AutoIncrementSize;
import br.com.jarch.annotation.JArchAutoIncrement;
import br.com.jarch.util.ProcessorUtils;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import java.util.Set;

@SupportedAnnotationTypes({"br.com.jarch.annotation.JArchAutoIncrement"})
public class AutoIncrementProcessor extends AbstractProcessor {

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

        try {
            roundEnv
                    .getElementsAnnotatedWithAny(Set.of(JArchAutoIncrement.class))
                    .stream()
                    .forEach(element -> {
                        ProcessorUtils.messageAnalyzing(getClass(), element) ;
                        validTypeField(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 validTypeField(Element element) {
        JArchAutoIncrement annotation = element.getAnnotation(JArchAutoIncrement.class);
        TypeMirror numberType = processingEnv.getElementUtils().getTypeElement(Number.class.getName()).asType();
        TypeMirror stringType = processingEnv.getElementUtils().getTypeElement(String.class.getName()).asType();
        boolean size0 = annotation.toString().contains(AutoIncrementSize.class.getName());
        try {
            if (size0 && !processingEnv.getTypeUtils().isAssignable(element.asType(), numberType)) {
                ProcessorUtils.messageError("JARCH ERROR: @JArchAutoIncrement somente permitido para field com tipo que estenda de Number ", element);
            }

            if (!size0 && !processingEnv.getTypeUtils().isAssignable(element.asType(), stringType)) {
                ProcessorUtils.messageError("JARCH ERROR: @JArchAutoIncrement somente permitido para field com tipo String quando atributo size for definido", element);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy