Class Discovery


  • @Beta
    @PublicAPI
    public final class Discovery
    extends Object
    Utility methods for parameter discovery.

    Please note that this class is marked with Beta annotation, so it can be subject of incompatible changes or removal in later releases.

    • Method Detail

      • listInstances

        @Beta
        public static <T> List<InstanceWrapper<T>> listInstances​(Class<T> type)
        Collect instances of a specific type.
        Type Parameters:
        T - Type to collect
        Parameters:
        type - Type to collect
        Returns:
        One or more instances of the specified type. Interfaces are retrieved as services; classes are instantiated with their nullary constructors; enums are enumerated
      • selectInstance

        public static <T> com.google.common.base.Optional<InstanceWrapper<T>> selectInstance​(Class<T> type,
                                                                                             String shortName)
        Look up an instance by its short name.
        Type Parameters:
        T - Type to collect
        Parameters:
        type - Type to collect (by listInstances(java.lang.Class)
        shortName - Short name of the instance to select
        Returns:
        The first matching instance if found
      • isParametrized

        @Beta
        public static boolean isParametrized​(Object o)
        Check if an object is parametrized.
        Parameters:
        o - Object in question
        Returns:
        true if o is parametrized
      • getAnnotationForClass

        @Beta
        public static <T extends Annotation> T getAnnotationForClass​(Class<T> annotationClass,
                                                                     Class<?> clazz)
        Retrieve annotation for a class. Note that enum constants are not handled by this method.
        Type Parameters:
        T - Annotation class
        Parameters:
        annotationClass - Annotation class
        clazz - Target class
        Returns:
        Annotation retrieved
        Throws:
        IllegalArgumentException - when no Description annotation found
      • getAnnotationIfPresent

        @Beta
        public static <T extends Annotation> com.google.common.base.Optional<T> getAnnotationIfPresent​(Class<T> a,
                                                                                                       Object o)
        Retrieve an annotation on an object if present.
        Type Parameters:
        T - Annotation class
        Parameters:
        a - Annotation class
        o - Object to examine
        Returns:
        The description annotation if exist
      • getAnnotation

        @Beta
        public static <T extends Annotation> T getAnnotation​(Class<T> a,
                                                             Object o)
        Retrieve an annotation on an object.
        Type Parameters:
        T - Annotation class
        Parameters:
        a - Annotation class
        o - Object to examine
        Returns:
        The annotation if exists or null; If o is an instance of an enum then enum constant annotation is retrieved
        Throws:
        IllegalArgumentException - when no annotation found
      • getDescriptionAnnotation

        @Beta
        public static Description getDescriptionAnnotation​(Object o)
        Retrieve the description annotation on an object.
        Parameters:
        o - Object to examine
        Returns:
        The description annotation if exists or null; If o is an instance of an enum then enum constant annotation is retrieved
        Throws:
        IllegalArgumentException - when no Description annotation found
      • listCreators

        @Beta
        public static <T> List<CreatorWrapper<T>> listCreators​(Object o,
                                                               Class<T> targetType)
        CreatorWrapper acts as a type-dependent service loader.

        Note that the order of the returned list is defined by

        Type Parameters:
        T - type to be created
        Parameters:
        targetType - type to be created
        o - Instance on which the creators should be called
        Returns:
        All distinct creator parameters
      • initializeBuilderIfExists

        @Beta
        public static <T> com.google.common.base.Optional<ParameterBuilder<T>> initializeBuilderIfExists​(T o)
        Create initial Builder if possible.

        Builder association is identified by annotation BuilderClass

        Type Parameters:
        T - Parameter object type
        Parameters:
        o - An object
        Returns:
        An associated builder instance if the parameter object has associated builder.