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

com.daiyc.extension.processor.AnnotationUtils Maven / Gradle / Ivy

package com.daiyc.extension.processor;

import com.daiyc.extension.core.annotations.Adaptive;
import com.daiyc.extension.core.enums.DegradationStrategy;
import com.daiyc.extension.processor.meta.AdaptiveMeta;

import javax.lang.model.element.*;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author daiyc
 * @since 2024/7/30
 */
abstract class AnnotationUtils {

    public static Map getAnnotationValues(Element param, Class annClass) {
        AnnotationMirror annotationMirror = param.getAnnotationMirrors()
                .stream()
                .filter(ann -> {
                    TypeElement annElement = (TypeElement) ann.getAnnotationType().asElement();
                    return annClass.getCanonicalName().equals(annElement.getQualifiedName().toString());
                })
                .findFirst()
                .orElse(null);

        return getAnnotationValues(annotationMirror);
    }

    public static Map getAnnotationValues(AnnotationMirror annotationMirror) {
        Map defaultValues = new HashMap<>();
        if (annotationMirror != null) {
            defaultValues = annotationMirror.getAnnotationType().asElement().getEnclosedElements()
                    .stream()
                    .filter(el -> el.getKind() == ElementKind.METHOD)
                    .map(el -> (ExecutableElement) el)
                    .filter(el -> el.getDefaultValue() != null)
                    .collect(Collectors.toMap(el -> el.getSimpleName().toString(), ExecutableElement::getDefaultValue));
        }

        Map annValues = new HashMap<>();
        if (annotationMirror != null) {
            annValues = io.vavr.collection.HashMap.ofAll(annotationMirror.getElementValues())
                    .mapKeys(k -> k.getSimpleName().toString())
                    .mapValues(v -> (AnnotationValue) v)
                    .toJavaMap();
        }

        defaultValues.putAll(annValues);

        return defaultValues;
    }

    public static DeclaredType getEnumType(Map annotationValueMap, String key) {
        return Optional.ofNullable(annotationValueMap.get(key))
                .map(v -> (DeclaredType) v.getValue())
                .filter(c -> !"com.daiyc.extension.core.enums.None".equals(c.toString()))
                .orElse(null);
    }

    public static AdaptiveMeta readAdaptive(Element param) {
        Map annotationValues = getAnnotationValues(param, Adaptive.class);
        TypeMirror converterType = (TypeMirror) annotationValues.get("converter").getValue();
        String path = annotationValues.get("value").getValue().toString();
        String degradationStrategyName = annotationValues.get("degradationStrategy").getValue().toString();
        DegradationStrategy degradationStrategy = DegradationStrategy.valueOf(degradationStrategyName);

        AdaptiveMeta adaptiveMeta = new AdaptiveMeta()
                .setConverter(converterType)
                .setValue(path)
                .setDegradationStrategy(degradationStrategy);

        List toEnums = (List) annotationValues.get("toEnum").getValue();
        List toEnumMetas = toEnums.stream()
                .map(AnnotationUtils::readToEnum)
                .collect(Collectors.toList());

        List byTypes = (List) annotationValues.get("byType").getValue();
        List byTypeMetas = byTypes.stream()
                .map(AnnotationUtils::readByType)
                .collect(Collectors.toList());

        List byPatterns = (List) annotationValues.get("byPattern").getValue();
        List byPatternMetas = byPatterns.stream()
                .map(AnnotationUtils::readByPattern)
                .collect(Collectors.toList());

        return adaptiveMeta
                .setToEnums(toEnumMetas)
                .setByTypes(byTypeMetas)
                .setByPatterns(byPatternMetas);
    }

    protected static AdaptiveMeta.ToEnumMeta readToEnum(AnnotationMirror toEnumAnn) {
        Map annotationValues = AnnotationUtils.getAnnotationValues(toEnumAnn);
        DeclaredType enumType = getEnumType(annotationValues, "enumType");
        String byMethod = annotationValues.get("byMethod").getValue().toString();
        String byField = annotationValues.get("byField").getValue().toString();
        boolean byOrdinal = (boolean) annotationValues.get("byOrdinal").getValue();

        return new AdaptiveMeta.ToEnumMeta()
                .setEnumType(enumType)
                .setByMethod(byMethod)
                .setByField(byField)
                .setByOrdinal(byOrdinal);
    }

    protected static AdaptiveMeta.ByTypeMeta readByType(AnnotationMirror byTypeAnn) {
        Map annotationValues = AnnotationUtils.getAnnotationValues(byTypeAnn);
        List types = ((List) annotationValues.get("type").getValue())
                .stream()
                .map(i -> (DeclaredType) i.getValue())
                .collect(Collectors.toList());

        String name = annotationValues.get("name").getValue().toString();

        return new AdaptiveMeta.ByTypeMeta()
                .setTypes(types)
                .setName(name);
    }

    protected static AdaptiveMeta.ByPatternMeta readByPattern(AnnotationMirror byPatternAnn) {
        Map annotationValues = AnnotationUtils.getAnnotationValues(byPatternAnn);
        List patterns = ((List) annotationValues.get("pattern").getValue())
                .stream()
                .map(i -> i.getValue().toString())
                .collect(Collectors.toList());
        String name = annotationValues.get("name").getValue().toString();

        return new AdaptiveMeta.ByPatternMeta()
                .setPatterns(patterns)
                .setName(name);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy