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

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

The newest version!
package com.daiyc.extension.processor;

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

import javax.lang.model.element.*;
import javax.lang.model.type.DeclaredType;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author daiyc
 * @since 2024/7/30
 */
@SuppressWarnings("unchecked")
public abstract class AnnotationUtils {
    public static  T getAnnotationValues(Element param, Class annClass, Function, T> func) {
        return func.apply(getAnnotationValues(param, annClass));
    }

    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 ExtensionPointMeta readExtensionPoint(Map annotationValues) {
        return new ExtensionPointMeta()
                .setCandidates(readStrings(annotationValues.get("candidates")))
                .setStrictMode((boolean) annotationValues.get("strictMode").getValue())
                .setValue((String) annotationValues.get("value").getValue())
                .setEnumType(getEnumType(annotationValues, "enumType"))
                .setUseDefault((boolean) annotationValues.get("useDefault").getValue())
                ;
    }

    public static AdaptiveMeta readAdaptive(Element element) {
        Map annotationValues = getAnnotationValues(element, Adaptive.class);
        String path = annotationValues.get("value").getValue().toString();

        AdaptiveMeta adaptiveMeta = new AdaptiveMeta()
                .setValue(path)
                .setUseDefault((boolean) annotationValues.get("useDefault").getValue())
                .setDefaultExtension((String) annotationValues.get("defaultExtension").getValue());

        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 = readStrings(annotationValues.get("pattern"));
        String name = annotationValues.get("name").getValue().toString();

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

    protected static List readStrings(AnnotationValue annotationValue) {
        List value = (List) annotationValue.getValue();
        if (value == null) {
            return Collections.emptyList();
        }
        return value.stream()
                .map(i -> i.getValue().toString())
                .collect(Collectors.toList());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy