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

io.micronaut.inject.annotation.MappingAnnotationMetadataDelegate Maven / Gradle / Ivy

There is a newer version: 4.7.5
Show newest version
/*
 * Copyright 2017-2022 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.inject.annotation;

import io.micronaut.core.annotation.AnnotationMetadataDelegate;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.StringUtils;
import io.micronaut.core.value.OptionalValues;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.function.Supplier;

/**
 * Abstract annotation metadata delegate for cases when annotation
 * values need to be mapped before being returned.
 *
 * @since 4.0.0
 * @author Sergey Gavrilov
 */
@Experimental
public abstract sealed class MappingAnnotationMetadataDelegate implements AnnotationMetadataDelegate permits EvaluatedAnnotationMetadata {
    public abstract  AnnotationValue mapAnnotationValue(AnnotationValue av);


    @Override
    public Optional stringValue(String annotation, String member) {
        return findAnnotation(annotation)
                   .flatMap(av -> av.stringValue(member));
    }

    @Override
    public Optional stringValue(Class annotation, String member) {
        return stringValue(annotation.getName(), member);
    }

    @Override
    public Optional stringValue(Class annotation) {
        return stringValue(annotation, VALUE_MEMBER);
    }

    @Override
    public Optional stringValue(String annotation) {
        return stringValue(annotation, VALUE_MEMBER);
    }

    @Override
    public String[] stringValues(String annotation, String member) {
        return findAnnotation(annotation)
                   .map(av -> av.stringValues(member))
                   .orElse(StringUtils.EMPTY_STRING_ARRAY);
    }

    @Override
    public String[] stringValues(Class annotation, String member) {
        return stringValues(annotation.getName(), member);
    }

    @Override
    public String[] stringValues(Class annotation) {
        return stringValues(annotation, VALUE_MEMBER);
    }

    @Override
    public String[] stringValues(String annotation) {
        return stringValues(annotation, VALUE_MEMBER);
    }

    @Override
    public > Optional enumValue(String annotation, Class enumType) {
        return enumValue(annotation, VALUE_MEMBER, enumType);
    }

    @Override
    public > Optional enumValue(String annotation, String member,
                                                     Class enumType) {
        return findAnnotation(annotation)
                   .flatMap(av -> av.enumValue(member, enumType));
    }

    @Override
    public > Optional enumValue(Class annotation,
                                                     Class enumType) {
        return enumValue(annotation.getName(), VALUE_MEMBER, enumType);
    }

    @Override
    public > Optional enumValue(Class annotation,
                                                     String member, Class enumType) {
        return enumValue(annotation.getName(), member, enumType);
    }

    @Override
    public > E[] enumValues(String annotation, String member, Class enumType) {
        return findAnnotation(annotation)
                   .map(av -> av.enumValues(member, enumType))
                   .orElse((E[]) Array.newInstance(enumType, 0));
    }

    @Override
    public > E[] enumValues(String annotation, Class enumType) {
        return enumValues(annotation, VALUE_MEMBER, enumType);
    }

    @Override
    public > E[] enumValues(Class annotation,
                                              Class enumType) {
        return enumValues(annotation.getName(), VALUE_MEMBER, enumType);
    }

    @Override
    public > E[] enumValues(Class annotation,
                                              String member, Class enumType) {
        return enumValues(annotation.getName(), member, enumType);
    }

    @Override
    public  Class[] classValues(String annotation, String member) {
        return (Class[]) findAnnotation(annotation)
                                .map(av -> av.classValues(member))
                                .orElse(ReflectionUtils.EMPTY_CLASS_ARRAY);
    }

    @Override
    public  Class[] classValues(String annotation) {
        return classValues(annotation, VALUE_MEMBER);
    }

    @Override
    public  Class[] classValues(Class annotation) {
        return classValues(annotation.getName(), VALUE_MEMBER);
    }

    @Override
    public  Class[] classValues(Class annotation, String member) {
        return classValues(annotation.getName(), member);
    }

    @Override
    public Optional booleanValue(String annotation, String member) {
        return findAnnotation(annotation)
                   .flatMap(av -> av.booleanValue(member));
    }

    @Override
    public Optional booleanValue(Class annotation, String member) {
        return booleanValue(annotation.getName(), member);
    }

    @Override
    public Optional booleanValue(Class annotation) {
        return booleanValue(annotation.getName(), VALUE_MEMBER);
    }

    @Override
    public Optional booleanValue(String annotation) {
        return booleanValue(annotation, VALUE_MEMBER);
    }

    @Override
    public boolean isTrue(String annotation, String member) {
        return getValue(annotation, member, Boolean.class).orElse(false);
    }

    @Override
    public boolean isTrue(Class annotation, String member) {
        return isTrue(annotation.getName(), member);
    }

    @Override
    public boolean isFalse(String annotation, String member) {
        return !isTrue(annotation, member);
    }

    @Override
    public boolean isFalse(Class annotation, String member) {
        return isFalse(annotation.getName(), member);
    }

    @Override
    public Optional classValue(String annotation, String member) {
        return findAnnotation(annotation)
                   .flatMap(av -> av.classValue(member));
    }

    @Override
    public Optional classValue(String annotation) {
        return classValue(annotation, VALUE_MEMBER);
    }

    @Override
    public Optional classValue(Class annotation) {
        return classValue(annotation.getName(), VALUE_MEMBER);
    }

    @Override
    public Optional classValue(Class annotation, String member) {
        return classValue(annotation.getName(), member);
    }

    @Override
    public OptionalInt intValue(String annotation, String member) {
        return findAnnotation(annotation)
                   .map(AnnotationValue::intValue)
                   .orElse(OptionalInt.empty());
    }

    @Override
    public OptionalInt intValue(Class annotation, String member) {
        return intValue(annotation.getName(), member);
    }

    @Override
    public OptionalInt intValue(Class annotation) {
        return intValue(annotation.getName(), VALUE_MEMBER);
    }

    @Override
    public OptionalLong longValue(String annotation, String member) {
        return findAnnotation(annotation)
                   .map(AnnotationValue::longValue)
                   .orElse(OptionalLong.empty());
    }

    @Override
    public OptionalLong longValue(Class annotation, String member) {
        return longValue(annotation.getName(), member);
    }

    @Override
    public OptionalDouble doubleValue(String annotation, String member) {
        return findAnnotation(annotation)
                   .map(av -> av.doubleValue(member))
                   .orElse(OptionalDouble.empty());
    }

    @Override
    public OptionalDouble doubleValue(Class annotation, String member) {
        return findAnnotation(annotation)
                   .map(av -> av.doubleValue(member))
                   .orElse(OptionalDouble.empty());
    }

    @Override
    public OptionalDouble doubleValue(Class annotation) {
        return doubleValue(annotation, VALUE_MEMBER);
    }

    @Override
    public  Optional getValue(String annotation, String member, Argument requiredType) {
        return findAnnotation(annotation)
                   .flatMap(av -> av.get(member, requiredType));
    }

    @Override
    public  Optional getValue(Class annotation, String member,
                                    Argument requiredType) {
        return findAnnotation(annotation)
                   .flatMap(av -> av.get(member, requiredType));
    }

    @Override
    public  Optional getValue(String annotation, Argument requiredType) {
        return getValue(annotation, VALUE_MEMBER, requiredType);
    }

    @Override
    public  Optional getValue(Class annotation,
                                    Argument requiredType) {
        return getValue(annotation, VALUE_MEMBER, requiredType);
    }

    @Override
    public  Optional getValue(Class annotation, String member,
                                    Class requiredType) {
        return getValue(annotation, member, Argument.of(requiredType));
    }

    @Override
    public  Optional getValue(Class annotation, Class requiredType) {
        return getValue(annotation, VALUE_MEMBER, requiredType);
    }

    @Override
    public  Optional getValue(String annotation, String member, Class requiredType) {
        return getValue(annotation, member, Argument.of(requiredType));
    }

    @Override
    public  Optional getValue(String annotation, Class requiredType) {
        return getValue(annotation, VALUE_MEMBER, Argument.of(requiredType));
    }

    @Override
    public Optional getValue(String annotation, String member) {
        return getValue(annotation, member, Object.class);
    }

    @Override
    public Optional getValue(Class annotation, String member) {
        return getValue(annotation, member, Object.class);
    }

    @Override
    public Optional getValue(String annotation) {
        return getValue(annotation, VALUE_MEMBER, Object.class);
    }

    @Override
    public Optional getValue(Class annotation) {
        return getValue(annotation, VALUE_MEMBER, Object.class);
    }

    @Override
    public  OptionalValues getValues(Class annotation,
                                           Class valueType) {
        return getValues(annotation.getName(), valueType);
    }

    @Override
    public  OptionalValues getValues(String annotation, Class valueType) {
        return OptionalValues.of(valueType, getValues(annotation));
    }

    @Override
    public Map getValues(String annotation) {
        return findAnnotation(annotation)
                   .map(AnnotationValue::getValues)
                   .orElse(Collections.emptyMap());
    }

    @Override
    public  AnnotationValue getDeclaredAnnotation(Class annotationClass) {
        AnnotationValue av = getAnnotationMetadata().getDeclaredAnnotation(annotationClass);
        if (av != null) {
            return mapAnnotationValue(av);
        }
        return null;
    }

    @Override
    public  AnnotationValue getAnnotation(Class annotationClass) {
        return getAnnotation(annotationClass.getName());
    }

    @Override
    public  AnnotationValue getAnnotation(String annotation) {
        AnnotationValue av = getAnnotationMetadata().getAnnotation(annotation);
        if (av != null) {
            return mapAnnotationValue(av);
        }
        return null;
    }

    @Override
    public  Optional> findAnnotation(String annotation) {
        AnnotationValue av = getAnnotationMetadata().getAnnotation(annotation);
        if (av != null) {
            //noinspection unchecked
            return Optional.of((AnnotationValue) mapAnnotationValue(av));
        }
        return Optional.empty();
    }

    @Override
    public  Optional> findAnnotation(Class annotationClass) {
        return getAnnotationMetadata().findAnnotation(annotationClass)
                   .map(this::mapAnnotationValue);
    }

    @Override
    public  Optional> findDeclaredAnnotation(Class annotationClass) {
        return findDeclaredAnnotation(annotationClass.getName());
    }

    @Override
    public  Optional> findDeclaredAnnotation(String annotation) {
        Optional> av =
            getAnnotationMetadata().findDeclaredAnnotation(annotation);
        return av.map(this::mapAnnotationValue);
    }

    @Override
    public  T[] synthesizeDeclaredAnnotationsByType(Class annotationClass) {
        return getDeclaredAnnotationValuesByType(annotationClass).stream()
                   .map(annotation -> AnnotationMetadataSupport.buildAnnotation(annotationClass,
                       annotation))
                   .toArray(value -> (T[]) Array.newInstance(annotationClass, value));
    }

    @Override
    public  T[] synthesizeAnnotationsByType(Class annotationClass) {
        return getAnnotationValuesByType(annotationClass).stream()
                   .map(annotation -> AnnotationMetadataSupport.buildAnnotation(annotationClass,
                       annotation))
                   .toArray(value -> (T[]) Array.newInstance(annotationClass, value));
    }

    @Override
    public  T synthesizeDeclared(Class annotationClass) {
        return findDeclaredAnnotation(annotationClass)
                   .map(av -> AnnotationMetadataSupport.buildAnnotation(annotationClass, av))
                   .orElse(null);
    }

    @Override
    public  T synthesize(Class annotationClass) {
        return findAnnotation(annotationClass)
                   .map(av -> AnnotationMetadataSupport.buildAnnotation(annotationClass, av))
                   .orElse(null);
    }

    @Override
    public  T synthesize(Class annotationClass, String sourceAnnotation) {
        AnnotationValue av = getAnnotation(sourceAnnotation);
        if (av != null) {
            return AnnotationMetadataSupport.buildAnnotation(annotationClass, av);
        }
        return null;
    }

    @Override
    public  T synthesizeDeclared(Class annotationClass, String sourceAnnotation) {
        AnnotationValue av = getDeclaredAnnotation(sourceAnnotation);
        if (av != null) {
            return AnnotationMetadataSupport.buildAnnotation(annotationClass, av);
        }
        return null;
    }

    @Override
    public  List> getAnnotationValuesByType(Class annotationType) {
        return getAnnotationValues(() -> getAnnotationMetadata().getAnnotationValuesByType(annotationType));
    }

    @Override
    public  List> getDeclaredAnnotationValuesByType(Class annotationType) {
        return getAnnotationValues(() -> getAnnotationMetadata().getDeclaredAnnotationValuesByType(annotationType));
    }

    @Override
    public  List> getAnnotationValuesByStereotype(String stereotype) {
        return getAnnotationValues(() -> getAnnotationMetadata().getAnnotationValuesByStereotype(stereotype));
    }

    @Override
    public  List> getDeclaredAnnotationValuesByName(String annotationType) {
        return getAnnotationValues(() -> getAnnotationMetadata().getDeclaredAnnotationValuesByName(annotationType));
    }

    @Override
    public  List> getAnnotationValuesByName(String annotationType) {
        return getAnnotationValues(() -> getAnnotationMetadata().getAnnotationValuesByName(annotationType));
    }

    private  List> getAnnotationValues(Supplier>> supplier) {
        return supplier.get().stream()
                   .map(this::mapAnnotationValue)
                   .toList();
    }
}