io.micronaut.inject.annotation.MappingAnnotationMetadataDelegate Maven / Gradle / Ivy
/*
* 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 extends Annotation> annotation, String member) {
return stringValue(annotation.getName(), member);
}
@Override
public Optional stringValue(Class extends Annotation> 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 extends Annotation> annotation, String member) {
return stringValues(annotation.getName(), member);
}
@Override
public String[] stringValues(Class extends Annotation> 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 extends Annotation> annotation,
Class enumType) {
return enumValue(annotation.getName(), VALUE_MEMBER, enumType);
}
@Override
public > Optional enumValue(Class extends Annotation> 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 extends Annotation> annotation,
Class enumType) {
return enumValues(annotation.getName(), VALUE_MEMBER, enumType);
}
@Override
public > E[] enumValues(Class extends Annotation> 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 extends Annotation> annotation) {
return classValues(annotation.getName(), VALUE_MEMBER);
}
@Override
public Class[] classValues(Class extends Annotation> 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 extends Annotation> annotation, String member) {
return booleanValue(annotation.getName(), member);
}
@Override
public Optional booleanValue(Class extends Annotation> 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 extends Annotation> 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 extends Annotation> 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 extends Annotation> annotation) {
return classValue(annotation.getName(), VALUE_MEMBER);
}
@Override
public Optional classValue(Class extends Annotation> 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 extends Annotation> annotation, String member) {
return intValue(annotation.getName(), member);
}
@Override
public OptionalInt intValue(Class extends Annotation> 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 extends Annotation> 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 extends Annotation> annotation, String member) {
return findAnnotation(annotation)
.map(av -> av.doubleValue(member))
.orElse(OptionalDouble.empty());
}
@Override
public OptionalDouble doubleValue(Class extends Annotation> 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 extends Annotation> 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 extends Annotation> annotation,
Argument requiredType) {
return getValue(annotation, VALUE_MEMBER, requiredType);
}
@Override
public Optional getValue(Class extends Annotation> annotation, String member,
Class requiredType) {
return getValue(annotation, member, Argument.of(requiredType));
}
@Override
public Optional getValue(Class extends Annotation> 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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy