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

org.babyfish.jimmer.apt.immutable.meta.ValidationMessages Maven / Gradle / Ivy

There is a newer version: 0.8.177
Show newest version
package org.babyfish.jimmer.apt.immutable.meta;

import com.squareup.javapoet.ClassName;
import org.babyfish.jimmer.apt.MetaException;

import javax.lang.model.element.*;
import javax.validation.Constraint;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.*;

public class ValidationMessages {

    public static final String CONSTRAINT_FULL_NAME = Constraint.class.getName();

    private ValidationMessages() {}

    public static Map parseMessageMap(Element element) {
        Map map = new LinkedHashMap<>();
        for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
            if (hasConstraint((TypeElement) annotationMirror.getAnnotationType().asElement())) {
                TypeElement typeElement = (TypeElement) annotationMirror.getAnnotationType().asElement();
                ClassName className = ClassName.get(
                        ((PackageElement)typeElement.getEnclosingElement()).getQualifiedName().toString(),
                        typeElement.getSimpleName().toString()
                );
                if (map.containsKey(className)) {
                    throw new MetaException(
                            element,
                            "duplicate annotations \"" + className + "\""
                    );
                }
                String message = "";
                for (Map.Entry e :
                        annotationMirror.getElementValues().entrySet()) {
                    if (e.getKey().getSimpleName().toString().equals("message")) {
                        Object msg = e.getValue().getValue();
                        if (msg instanceof String) {
                            message = (String) msg;
                        }
                        break;
                    }
                }
                map.put(className, message);
            }
        }
        return Collections.unmodifiableMap(map);
    }

    private static boolean hasConstraint(TypeElement element) {
        for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
            TypeElement annoElement = (TypeElement) mirror.getAnnotationType().asElement();
            if (annoElement.getQualifiedName().toString().equals(CONSTRAINT_FULL_NAME)) {
                Retention retention = element.getAnnotation(Retention.class);
                if (retention == null || retention.value() != RetentionPolicy.RUNTIME) {
                    throw new MetaException(
                            element,
                            "the annotation @" +
                                    element.getQualifiedName().toString() +
                                    " is decorated by @" +
                                    Constraint.class.getName() +
                                    " but its retention is not runtime"
                    );
                }
                for (Map.Entry e :
                    mirror.getElementValues().entrySet()) {
                    if (e.getKey().getSimpleName().toString().equals("validatedBy")) {
                        return !((Collection)e.getValue().getValue()).isEmpty();
                    }
                }
                break;
            }
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy