All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.micronaut.inject.qualifiers.AnnotationMetadataQualifier Maven / Gradle / Ivy
/*
* Copyright 2017-2020 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.qualifiers;
import io.micronaut.context.Qualifier;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.AnnotationUtil;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.ObjectUtils;
import io.micronaut.inject.BeanDefinition;
import io.micronaut.inject.BeanType;
import io.micronaut.inject.DelegatingBeanDefinition;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* A {@link io.micronaut.context.Qualifier} that uses {@link AnnotationMetadata}.
*
* @param The type
* @author Graeme Rocher
* @since 1.0
*/
@Internal
final class AnnotationMetadataQualifier extends FilteringQualifier {
@NonNull
final String annotationName;
@NonNull
final String annotationSimpleName;
@Nullable
final AnnotationValue qualifierAnn;
private AnnotationMetadataQualifier(@NonNull String annotationName,
@NonNull String annotationSimpleName,
@Nullable AnnotationValue value) {
this.annotationName = annotationName;
this.annotationSimpleName = annotationSimpleName;
this.qualifierAnn = value;
}
static AnnotationMetadataQualifier fromType(@NonNull AnnotationMetadata annotationMetadata,
@NonNull Class extends Annotation> annotationType) {
return new AnnotationMetadataQualifier<>(
annotationType.getName(),
annotationType.getSimpleName(),
resolveBindingAnnotationValue(annotationMetadata, annotationType.getName())
);
}
static AnnotationMetadataQualifier fromTypeName(@NonNull AnnotationMetadata annotationMetadata,
@NonNull String annotationTypeName) {
return new AnnotationMetadataQualifier<>(
annotationTypeName,
NameUtils.getSimpleName(annotationTypeName),
resolveBindingAnnotationValue(annotationMetadata, annotationTypeName)
);
}
static AnnotationMetadataQualifier fromValue(@NonNull AnnotationMetadata annotationMetadata,
@NonNull AnnotationValue annotationValue) {
return new AnnotationMetadataQualifier<>(
annotationValue.getAnnotationName(),
NameUtils.getSimpleName(annotationValue.getAnnotationName()),
resolveBindingAnnotationValue(annotationMetadata, annotationValue.getAnnotationName(), annotationValue.getValues())
);
}
@Override
public boolean doesQualify(Class beanType, BeanType candidate) {
if (!QualifierUtils.matchType(beanType, candidate)) {
return false;
}
if (QualifierUtils.matchAny(beanType, candidate)) {
return true;
}
if (candidate instanceof BeanDefinition bdCandidate) {
Qualifier candidateDeclaredQualifier = bdCandidate.getDeclaredQualifier();
if (candidateDeclaredQualifier != null && candidateDeclaredQualifier.contains(this)) {
return true;
}
if (candidate instanceof DelegatingBeanDefinition) {
if (matchByAnnotationMetadata(candidate)) {
return true;
}
}
} else if (matchByAnnotationMetadata(candidate)) {
return true;
}
return QualifierUtils.matchByCandidateName(candidate, beanType, annotationSimpleName);
}
private > boolean matchByAnnotationMetadata(BT candidate) {
if (qualifierAnn == null) {
return candidate.getAnnotationMetadata().hasAnnotation(annotationName);
}
return qualifierAnn.equals(resolveBindingAnnotationValue(candidate.getAnnotationMetadata()));
}
@Nullable
private AnnotationValue resolveBindingAnnotationValue(AnnotationMetadata annotationMetadata) {
return resolveBindingAnnotationValue(annotationMetadata, annotationName, annotationMetadata.getValues(annotationName));
}
@Nullable
private static AnnotationValue resolveBindingAnnotationValue(AnnotationMetadata annotationMetadata,
String annotationName) {
return resolveBindingAnnotationValue(annotationMetadata, annotationName, annotationMetadata.getValues(annotationName));
}
@Nullable
private static AnnotationValue resolveBindingAnnotationValue(AnnotationMetadata annotationMetadata,
String annotationName,
Map values) {
Map bindingValues = resolveBindingValues(annotationMetadata, values);
if (CollectionUtils.isNotEmpty(bindingValues)) {
return new AnnotationValue<>(annotationName, bindingValues);
}
return null;
}
@Nullable
private static Map resolveBindingValues(AnnotationMetadata annotationMetadata,
Map values) {
Set nonBinding = resolveNonBindingMembers(annotationMetadata);
if (values.isEmpty() || nonBinding.isEmpty()) {
return values;
}
Map map = new HashMap<>();
for (Map.Entry entry : values.entrySet()) {
if (!nonBinding.contains(entry.getKey().toString()) && map.put(entry.getKey(), entry.getValue()) != null) {
throw new IllegalStateException("Duplicate key: " + entry.getKey());
}
}
return map;
}
@NonNull
private static Set resolveNonBindingMembers(AnnotationMetadata annotationMetadata) {
String[] nonBindingArray = AnnotationUtil.resolveNonBindingMembers(annotationMetadata);
return ArrayUtils.isNotEmpty(nonBindingArray) ? new LinkedHashSet<>(Arrays.asList(nonBindingArray)) : Collections.emptySet();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null) {
return false;
}
return QualifierUtils.annotationQualifiersEquals(this, o);
}
@Override
public int hashCode() {
return ObjectUtils.hash(annotationName, qualifierAnn);
}
@Override
public String toString() {
if (this.qualifierAnn != null) {
return "@" + annotationSimpleName + "(" + qualifierAnn.getValues().entrySet().stream().map(entry -> entry.getKey() + "=" + valueToString(entry)).collect(Collectors.joining(", ")) + ")";
}
return "@" + annotationSimpleName;
}
private Object valueToString(Map.Entry entry) {
final Object v = entry.getValue();
if (v instanceof Object[] objects) {
return Arrays.toString(objects);
}
return v;
}
}