io.micronaut.context.BeanLocator 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.context;
import io.micronaut.core.reflect.InstantiationUtils;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.type.Argument;
import io.micronaut.core.type.GenericArgument;
import io.micronaut.inject.BeanDefinition;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
/**
* Core interface for locating and discovering {@link io.micronaut.context.annotation.Bean} instances.
*
* @author Graeme Rocher
* @since 1.0
*/
public interface BeanLocator {
/**
* Obtains a Bean for the given bean definition.
*
* @param definition The bean type
* @param The bean type parameter
* @return An instanceof said bean
* @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist
* for the given type
* @see io.micronaut.inject.qualifiers.Qualifiers
*/
@NonNull T getBean(@NonNull BeanDefinition definition);
/**
* Obtains a Bean for the given type and qualifier.
*
* @param beanType The bean type
* @param qualifier The qualifier
* @param The bean type parameter
* @return An instanceof said bean
* @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist
* for the given type
* @see io.micronaut.inject.qualifiers.Qualifiers
*/
@NonNull T getBean(@NonNull Class beanType, @Nullable Qualifier qualifier);
/**
* Obtains a Bean for the given type and qualifier.
*
* @param beanType The potentially parameterized bean type
* @param qualifier The qualifier
* @param The bean type parameter
* @return An instanceof said bean
* @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist
* for the given type
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 3.0.0
*/
default @NonNull T getBean(@NonNull Argument beanType, @Nullable Qualifier qualifier) {
return getBean(
Objects.requireNonNull(beanType, "Bean type cannot be null").getType(),
qualifier
);
}
/**
* Obtains a Bean for the given type and qualifier.
*
* @param beanType The potentially parameterized bean type
* @param The bean type parameter
* @return An instanceof said bean
* @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist
* for the given type
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 3.0.0
*/
default @NonNull T getBean(@NonNull Argument beanType) {
return getBean(
beanType,
null
);
}
/**
* Obtains a {@link BeanProvider} for the given type and qualifier.
*
* @param beanType The bean type
* @param qualifier The qualifier
* @param The bean type parameter
* @return The provider
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 4.5.0
*/
default @NonNull BeanProvider getProvider(@NonNull Class beanType, @Nullable Qualifier qualifier) {
return getProvider(Argument.of(beanType), qualifier);
}
/**
* Obtains a {@link BeanProvider} for the given type and qualifier.
*
* @param beanType The bean type
* @param The bean type parameter
* @return The provider
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 4.5.0
*/
default @NonNull BeanProvider getProvider(@NonNull Class beanType) {
return getProvider(Argument.of(beanType), null);
}
/**
* Obtains a {@link BeanProvider} for the given type and qualifier.
*
* @param beanType The potentially parameterized bean type
* @param qualifier The qualifier
* @param The bean type parameter
* @return The provider
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 4.5.0
*/
@SuppressWarnings({"unchecked", "rawtypes"})
default @NonNull BeanProvider getProvider(@NonNull Argument beanType, @Nullable Qualifier qualifier) {
Argument providerArgument = Argument.of(BeanProvider.class, beanType);
return (BeanProvider) getBean(
providerArgument,
qualifier
);
}
/**
* Obtains a {@link BeanProvider} for the given type and qualifier.
*
* @param beanType The potentially parameterized bean type
* @param The bean type parameter
* @return A provider
* for the given type
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 4.5.0
*/
default @NonNull BeanProvider getProvider(@NonNull Argument beanType) {
return getProvider(
beanType,
null
);
}
/**
* Finds a Bean for the given type and qualifier.
*
* @param beanType The bean type
* @param qualifier The qualifier
* @param The bean type parameter
* @return An instance of {@link Optional} that is either empty or containing the specified bean
* @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist
* for the given type
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 3.0.0
*/
@NonNull Optional findBean(@NonNull Argument beanType, @Nullable Qualifier qualifier);
/**
* Finds a Bean for the given type and qualifier.
*
* @param beanType The bean type
* @param The bean type parameter
* @return An instance of {@link Optional} that is either empty or containing the specified bean
* @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist
* for the given type
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 3.0.0
*/
default @NonNull Optional findBean(@NonNull Argument beanType) {
return findBean(beanType, null);
}
/**
* Finds a Bean for the given type and qualifier.
*
* @param beanType The bean type
* @param qualifier The qualifier
* @param The bean type parameter
* @return An instance of {@link Optional} that is either empty or containing the specified bean
* @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist
* for the given type
* @see io.micronaut.inject.qualifiers.Qualifiers
*/
@NonNull Optional findBean(@NonNull Class beanType, @Nullable Qualifier qualifier);
/**
* Get all beans of the given type.
*
* @param beanType The bean type
* @param The bean type parameter
* @return The found beans
*/
@NonNull Collection getBeansOfType(@NonNull Class beanType);
/**
* Get all beans of the given type.
*
* @param beanType The bean type
* @param qualifier The qualifier
* @param The bean type parameter
* @return The found beans
*/
@NonNull Collection getBeansOfType(@NonNull Class beanType, @Nullable Qualifier qualifier);
/**
* Get all beans of the given type.
*
* @param beanType The potenitally parameterized bean type
* @param The bean type parameter
* @return The found beans
* @since 3.0.0
*/
default @NonNull Collection getBeansOfType(@NonNull Argument beanType) {
Objects.requireNonNull(beanType, "Bean type cannot be null");
return getBeansOfType(beanType.getType());
}
/**
* Get all beans of the given type.
*
* @param beanType The potenitally parameterized bean type
* @param qualifier The qualifier
* @param The bean type parameter
* @return The found beans
* @since 3.0.0
*/
default @NonNull Collection getBeansOfType(@NonNull Argument beanType, @Nullable Qualifier qualifier) {
Objects.requireNonNull(beanType, "Bean type cannot be null");
return getBeansOfType(beanType.getType(), qualifier);
}
/**
* Obtain a stream of beans of the given type.
*
* @param beanType The bean type
* @param qualifier The qualifier
* @param The bean concrete type
* @return A stream of instances
* @see io.micronaut.inject.qualifiers.Qualifiers
*/
@NonNull Stream streamOfType(@NonNull Class beanType, @Nullable Qualifier qualifier);
/**
* Obtain a stream of beans of the given type.
*
* @param beanType The potentially parameterized bean type
* @param qualifier The qualifier
* @param The bean concrete type
* @return A stream of instances
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 3.0.0
*/
default @NonNull Stream streamOfType(@NonNull Argument beanType, @Nullable Qualifier qualifier) {
return streamOfType(
Objects.requireNonNull(beanType, "Bean type cannot be null").getType(),
qualifier
);
}
/**
* Obtain a stream of beans of the given type.
*
* @param beanType The potentially parameterized bean type
* @param The bean concrete type
* @return A stream of instances
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 3.0.0
*/
default @NonNull Stream streamOfType(@NonNull Argument beanType) {
return streamOfType(
Objects.requireNonNull(beanType, "Bean type cannot be null"),
null
);
}
/**
* Obtain a map of beans of the given type where the key is the qualifier.
*
* @param beanType The potentially parameterized bean type
* @param qualifier The qualifier
* @param The bean concrete type
* @return A map of instances
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 4.0.0
*/
default @NonNull Map mapOfType(@NonNull Argument beanType, @Nullable Qualifier qualifier) {
return Collections.emptyMap();
}
/**
* Obtain a map of beans of the given type where the key is the qualifier.
*
* @param beanType The potentially parameterized bean type
* @param qualifier The qualifier
* @param The bean concrete type
* @return A map of instances
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 4.0.0
*/
default @NonNull Map mapOfType(@NonNull Class beanType, @Nullable Qualifier qualifier) {
return mapOfType(Argument.of(beanType), qualifier);
}
/**
* Obtain a map of beans of the given type where the key is the qualifier.
*
* @param beanType The potentially parameterized bean type
* @param The bean concrete type
* @return A map of instances
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 4.0.0
*/
default @NonNull Map mapOfType(@NonNull Class beanType) {
return mapOfType(Argument.of(beanType), null);
}
/**
* Obtain a map of beans of the given type where the key is the qualifier.
*
* @param beanType The potentially parameterized bean type
* @param The bean concrete type
* @return A map of instances
* @see io.micronaut.inject.qualifiers.Qualifiers
* @since 4.0.0
*/
default @NonNull Map mapOfType(@NonNull Argument beanType) {
return mapOfType(beanType, null);
}
/**
* Resolves the proxy target for a given bean type. If the bean has no proxy then the original bean is returned.
*
* @param beanType The bean type
* @param qualifier The bean qualifier
* @param The generic type
* @return The proxied instance
*/
@NonNull T getProxyTargetBean(@NonNull Class beanType, @Nullable Qualifier qualifier);
/**
* Resolves the proxy target for a given bean type. If the bean has no proxy then the original bean is returned.
*
* @param beanType The bean type
* @param qualifier The bean qualifier
* @param The generic type
* @return The proxied instance
* @since 3.0.0
*/
default @NonNull T getProxyTargetBean(@NonNull Argument beanType, @Nullable Qualifier qualifier) {
return getProxyTargetBean(Objects.requireNonNull(beanType, "Bean type cannot be null").getType(), qualifier);
}
/**
* Obtain a stream of beans of the given type.
*
* @param beanType The bean type
* @param The bean concrete type
* @return A stream
*/
default @NonNull Stream streamOfType(@NonNull Class beanType) {
return streamOfType(beanType, null);
}
/**
* Obtains a Bean for the given type.
*
* @param beanType The bean type
* @param The bean type parameter
* @return An instanceof said bean
* @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist
* for the given type
* @throws io.micronaut.context.exceptions.NoSuchBeanException If the bean doesn't exist
*/
default @NonNull T getBean(@NonNull Class beanType) {
return getBean(beanType, null);
}
/**
* Finds a Bean for the given type.
*
* @param beanType The bean type
* @param The bean type parameter
* @return An instance of {@link Optional} that is either empty or containing the specified bean
* @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist
* for the given type
*/
default @NonNull Optional findBean(@NonNull Class beanType) {
return findBean(beanType, null);
}
/**
* Finds a Bean for the given type or attempts to instantiate the given instance.
*
* @param beanType The bean type
* @param The bean type parameter
* @return An instance of {@link Optional} that is either empty or containing the specified bean if it could not
* be found or instantiated
* @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist
* for the given type
*/
default @NonNull Optional findOrInstantiateBean(@NonNull Class beanType) {
Optional bean = findBean(beanType, null);
if (bean.isPresent()) {
return bean;
} else {
return InstantiationUtils.tryInstantiate(beanType);
}
}
}