Class ConfigAnnotationUtil
- java.lang.Object
-
- org.apache.sling.testing.mock.osgi.config.ConfigAnnotationUtil
-
public final class ConfigAnnotationUtil extends Object
Common functions for resolving OSGi config test parameters.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ConfigAnnotationUtil.ConfigTypePredicate
Predicate for custom filtering of fully identified candidate annotations, accepting anOptional
ofConfigType
representing the parent annotation when present, and theClass
of the effective config type.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Predicate<Annotation>
configTypeAnnotationFilter(ConfigAnnotationUtil.ConfigTypePredicate configTypePredicate)
Utility function that returns a predicate for use as a filter expression forfindConfigTypeAnnotations(AnnotatedElement)
andfindConfigTypeAnnotations(Collection)
that reduces the input stream of annotations based on provided set of allowed config types.static Optional<Class<?>>
determineSupportedConfigType(@NotNull Class<?> type)
Return the appropriate config type for the given test parameter type, if it supported for binding.static Stream<Annotation>
findConfigTypeAnnotations(@NotNull AnnotatedElement element)
Find candidate OSGi config annotations on the givenAnnotatedElement
, returning a stream of only those matching one of the desired config types.static Stream<Annotation>
findConfigTypeAnnotations(@NotNull AnnotatedElement element, @Nullable ConfigAnnotationUtil.ConfigTypePredicate configTypePredicate)
Find candidate OSGi config annotations on the givenAnnotatedElement
, returning a stream of only those matching one of the desired config types.static Stream<Annotation>
findConfigTypeAnnotations(@NotNull Collection<Annotation> annotations)
Find candidate OSGi config annotations in the given collection, returning a stream of only those matching one of the desired config types.static Stream<Annotation>
findConfigTypeAnnotations(@NotNull Collection<Annotation> annotations, @Nullable ConfigAnnotationUtil.ConfigTypePredicate configTypePredicate)
Find candidate OSGi config annotations in the given collection, returning a stream of only those matching one of the desired config types.static Stream<SetConfig>
findUpdateConfigAnnotations(@NotNull AnnotatedElement element)
FindSetConfig
annotations on the givenAnnotatedElement
.static Stream<SetConfig>
findUpdateConfigAnnotations(@NotNull Collection<Annotation> annotations)
FindSetConfig
annotations in the given collection.static boolean
isValidConfigType(@NotNull Class<?> configType)
Utility function for filtering out component property types that can't be mapped to configurations.static <T> T[]
resolveParameterToArray(@NotNull ConfigCollection configCollection, @NotNull Class<T> configType)
Returns an array of configs matching the specified parameter config type.static <T> Optional<Map<String,Object>>
resolveParameterToConfigMap(@NotNull ConfigCollection configCollection, @NotNull Class<T> parameterConfigType, @NotNull Class<?>[] signatureParameterTypes, int parameterIndex)
Returns the first config map from theConfigCollection
, if present, after skipping the same number of values as there are matching signature parameter types with an index lower than the current parameterIndex.static <T> Optional<TypedConfig<T>>
resolveParameterToTypedConfig(@NotNull ConfigCollection configCollection, @NotNull Class<T> parameterConfigType, @NotNull Class<?>[] signatureParameterTypes, int parameterIndex)
Returns the firstTypedConfig
from theConfigCollection
, if present, after skipping the same number of values as there are matching signature parameter types with an index lower than the current parameterIndex.static <T> Optional<T>
resolveParameterToValue(@NotNull ConfigCollection configCollection, @NotNull Class<T> parameterConfigType, @NotNull Class<?>[] signatureParameterTypes, int parameterIndex)
Returns the first config value from theConfigCollection
, if present, after skipping the same number of values as there are matching signature parameter types with an index lower than the current parameterIndex.
-
-
-
Method Detail
-
findConfigTypeAnnotations
public static Stream<Annotation> findConfigTypeAnnotations(@NotNull @NotNull AnnotatedElement element)
Find candidate OSGi config annotations on the givenAnnotatedElement
, returning a stream of only those matching one of the desired config types. An annotation matches a config type only if the annotation's own type is the same as the config type, or if the annotation is aConfigType
and itsConfigType.type()
is the same as the config type. If theAnnotatedElement
has aConfigTypes
annotation, its nestedConfigType
annotations will be considered as well.- Parameters:
element
- the annotated element- Returns:
- a stream of annotations
-
findConfigTypeAnnotations
public static Stream<Annotation> findConfigTypeAnnotations(@NotNull @NotNull AnnotatedElement element, @Nullable @Nullable ConfigAnnotationUtil.ConfigTypePredicate configTypePredicate)
Find candidate OSGi config annotations on the givenAnnotatedElement
, returning a stream of only those matching one of the desired config types. An annotation matches a config type only if the annotation's own type is the same as the config type, or if the annotation is aConfigType
and itsConfigType.type()
is the same as the config type. If theAnnotatedElement
has aConfigTypes
annotation, its nestedConfigType
annotations will be considered as well.- Parameters:
element
- the annotated elementconfigTypePredicate
- an optional subsequent predicate for the applicable configType- Returns:
- a stream of annotations
-
findConfigTypeAnnotations
public static Stream<Annotation> findConfigTypeAnnotations(@NotNull @NotNull Collection<Annotation> annotations)
Find candidate OSGi config annotations in the given collection, returning a stream of only those matching one of the desired config types. An annotation matches a config type only if the annotation's own type is the same as the config type, or if the annotation is aConfigType
and itsConfigType.type()
is the same as the config type. If the collection has aConfigTypes
annotation, its nestedConfigType
annotations will be considered as well.- Parameters:
annotations
- a collection of annotations- Returns:
- a stream of annotations
-
findConfigTypeAnnotations
public static Stream<Annotation> findConfigTypeAnnotations(@NotNull @NotNull Collection<Annotation> annotations, @Nullable @Nullable ConfigAnnotationUtil.ConfigTypePredicate configTypePredicate)
Find candidate OSGi config annotations in the given collection, returning a stream of only those matching one of the desired config types. An annotation matches a config type only if the annotation's own type is the same as the config type, or if the annotation is aConfigType
and itsConfigType.type()
is the same as the config type. If the collection has aConfigTypes
annotation, its nestedConfigType
annotations will be considered as well.- Parameters:
annotations
- a collection of annotationsconfigTypePredicate
- an optional subsequent predicate for the applicable configType- Returns:
- a stream of annotations
-
findUpdateConfigAnnotations
public static Stream<SetConfig> findUpdateConfigAnnotations(@NotNull @NotNull AnnotatedElement element)
FindSetConfig
annotations on the givenAnnotatedElement
. If theAnnotatedElement
has anSetConfigs
annotation, its nestedSetConfig
annotations will be included as well.- Parameters:
element
- the annotated element- Returns:
- a stream of annotations
-
findUpdateConfigAnnotations
public static Stream<SetConfig> findUpdateConfigAnnotations(@NotNull @NotNull Collection<Annotation> annotations)
FindSetConfig
annotations in the given collection. If the collection has anSetConfigs
annotation, its nestedSetConfig
annotations will be included as well.- Parameters:
annotations
- a collection of annotations- Returns:
- a stream of annotations
-
isValidConfigType
public static boolean isValidConfigType(@NotNull @NotNull Class<?> configType)
Utility function for filtering out component property types that can't be mapped to configurations.- Parameters:
configType
- Config type- Returns:
- true if the provided class is a valid config type
-
configTypeAnnotationFilter
public static Predicate<Annotation> configTypeAnnotationFilter(@Nullable ConfigAnnotationUtil.ConfigTypePredicate configTypePredicate)
Utility function that returns a predicate for use as a filter expression forfindConfigTypeAnnotations(AnnotatedElement)
andfindConfigTypeAnnotations(Collection)
that reduces the input stream of annotations based on provided set of allowed config types.- Parameters:
configTypePredicate
- an optional subsequent predicate for the applicable configType- Returns:
- an annotation stream predicate
-
determineSupportedConfigType
public static Optional<Class<?>> determineSupportedConfigType(@NotNull @NotNull Class<?> type)
Return the appropriate config type for the given test parameter type, if it supported for binding. If the provided type is an array, the array's component type will be returned if it is a supported type.- Parameters:
type
- the candidate parameter type- Returns:
- an optional containing a supported config type, or empty if not supported.
-
resolveParameterToArray
public static <T> T[] resolveParameterToArray(@NotNull @NotNull ConfigCollection configCollection, @NotNull @NotNull Class<T> configType)
Returns an array of configs matching the specified parameter config type.- Type Parameters:
T
- the desired config type- Parameters:
configCollection
- the config collectionconfigType
- the parameter config type class- Returns:
- an array of matching configs
-
resolveParameterToTypedConfig
public static <T> Optional<TypedConfig<T>> resolveParameterToTypedConfig(@NotNull @NotNull ConfigCollection configCollection, @NotNull @NotNull Class<T> parameterConfigType, @NotNull @NotNull Class<?>[] signatureParameterTypes, int parameterIndex)
Returns the firstTypedConfig
from theConfigCollection
, if present, after skipping the same number of values as there are matching signature parameter types with an index lower than the current parameterIndex. For example, if we are injecting values into a test method signature that accepts twoMyConfig
arguments, this method will be called twice for the same configCollection, parameterConfigType, and signatureParameterTypes array. The first call will specify a parameterIndex of 0, which will return the first value from the collection, and the second call will specify a parameterIndex of 1 which will return the second value from the collection. This method does not consider array orConfigCollection
parameter types when skipping values.- Type Parameters:
T
- the parameter config type- Parameters:
configCollection
- the config collectionparameterConfigType
- the parameter config type class (must be an interface or an annotation type)signatureParameterTypes
- the types of the signature's parametersparameterIndex
- the 0-based index of the parameter in the executable's signature- Returns:
- a single parameter value if available, or empty
-
resolveParameterToValue
public static <T> Optional<T> resolveParameterToValue(@NotNull @NotNull ConfigCollection configCollection, @NotNull @NotNull Class<T> parameterConfigType, @NotNull @NotNull Class<?>[] signatureParameterTypes, int parameterIndex)
Returns the first config value from theConfigCollection
, if present, after skipping the same number of values as there are matching signature parameter types with an index lower than the current parameterIndex. For example, if we are injecting values into a test method signature that accepts twoMyConfig
arguments, this method will be called twice for the same configCollection, parameterConfigType, and signatureParameterTypes array. The first call will specify a parameterIndex of 0, which will return the first value from the collection, and the second call will specify a parameterIndex of 1 which will return the second value from the collection. This method does not consider array orConfigCollection
parameter types when skipping values.- Type Parameters:
T
- the parameter config type- Parameters:
configCollection
- the config collectionparameterConfigType
- the parameter config type class (must be an interface or an annotation type)signatureParameterTypes
- the types of the signature's parametersparameterIndex
- the 0-based index of the parameter in the executable's signature- Returns:
- a single parameter value if available, or empty
-
resolveParameterToConfigMap
public static <T> Optional<Map<String,Object>> resolveParameterToConfigMap(@NotNull @NotNull ConfigCollection configCollection, @NotNull @NotNull Class<T> parameterConfigType, @NotNull @NotNull Class<?>[] signatureParameterTypes, int parameterIndex)
Returns the first config map from theConfigCollection
, if present, after skipping the same number of values as there are matching signature parameter types with an index lower than the current parameterIndex. For example, if we are injecting values into a test method signature that accepts twoMyConfig
arguments, this method will be called twice for the same configCollection, parameterConfigType, and signatureParameterTypes array. The first call will specify a parameterIndex of 0, which will return the first value from the collection, and the second call will specify a parameterIndex of 1 which will return the second value from the collection. This method does not consider array orConfigCollection
parameter types when skipping values.- Type Parameters:
T
- the parameter config type- Parameters:
configCollection
- the config collectionparameterConfigType
- the parameter config type class (must be an interface or an annotation type)signatureParameterTypes
- the types of the signature's parametersparameterIndex
- the 0-based index of the parameter in the executable's signature- Returns:
- a single parameter value if available, or empty
-
-