Class TypeRegistry


  • public class TypeRegistry
    extends java.lang.Object
    Used to register the actual types (also replacements by extensions). Optional type resolvers (ITypeResolver) allow deferred type resolution on other models, e.g., variability models. Type retrieval calls using a class are intended to be used by the implementation (and may prevent creating dynamic types) while the others based on string are intended for resolution of names used in the VIL/VTL.
    Author:
    Holger Eichelberger
    • Field Detail

      • NULL

        public static final java.lang.Object NULL
      • INVISIBLE_BY_DEFAULT

        public static final java.lang.Class<?>[] INVISIBLE_BY_DEFAULT
        Those classes for which the methods shall be invisible by default, in particular java.lang.Object. (public also for testing)
      • INVISIBLE_INHERITED

        private static final java.util.Map<java.lang.String,​java.util.List<java.lang.reflect.Method>> INVISIBLE_INHERITED
        Caches the operations vs. inheriting invisible methods.
      • types

        private java.util.Map<java.lang.String,​TypeDescriptor<?>> types
        Stores the registered types.
      • instantiators

        private java.util.Map<java.lang.String,​TypeDescriptor<? extends IVilType>> instantiators
        Stores the registered instantiators.
    • Constructor Detail

      • TypeRegistry

        private TypeRegistry()
        Creates the default type registry.
      • TypeRegistry

        public TypeRegistry​(TypeRegistry parentRegistry)
        Creates a (local) type registry, which delegates unknown types to the parentRegistry.
        Parameters:
        parentRegistry - the parent registry, if null DEFAULT is used
    • Method Detail

      • getParentRegistry

        public TypeRegistry getParentRegistry()
        Returns the parent registry.
        Returns:
        the parent registry (may be null in case of the root registry)
      • addTypeResolver

        public void addTypeResolver​(ITypeResolver resolver)
        Adds a type resolver as the current resolver of highest precedence.
        Parameters:
        resolver - the resolver to be added (may be null, but is ignored then)
      • registerCompoundType

        public boolean registerCompoundType​(CompoundTypeDescriptor type)
        Registers a compound type, but only if a type of this name has not been registered so far.
        Parameters:
        type - the type to register
        Returns:
        true if registered/successful, false else
      • hasTypeResolver

        public boolean hasTypeResolver​(java.lang.Class<? extends ITypeResolver> cls)
        Returns whether this registry has a type resolver of the given type.
        Parameters:
        cls - the type to match, may be null to match all
        Returns:
        true if there is a resolver, false else
      • removeTypeResolver

        public void removeTypeResolver​(ITypeResolver resolver)
        Renoves a type resolver.
        Parameters:
        resolver - the resolver to be added (may be null, but is ignored then)
      • addInvisibleInherited

        private static boolean addInvisibleInherited​(java.lang.reflect.Method method)
        Adds the given method to the cache of invisible inherited methods. Already existing registrations will not be changed.
        Parameters:
        method - the method to be added.
        Returns:
        true if method was registered before, false else
      • registerType

        public <T extends IVilTypeTypeDescriptor<T> registerType​(java.lang.Class<T> type)
                                                            throws VilException
        Registers a type. Please note that overriding a default artifact requires the same simple class name as the one to be overridden and that the new artifact type subclasses the existing one.
        Type Parameters:
        T - the actual type
        Parameters:
        type - the Java class of the VIL type to be registered
        Returns:
        the registered descriptor
        Throws:
        VilException - in case that type cannot be registered for some reason
      • createTypeDescriptor

        private static <T extends IVilTypeReflectionTypeDescriptor<T> createTypeDescriptor​(java.lang.Class<T> type)
                                                                                      throws VilException
        Creates a type descriptor considering IActualValueProvider.
        Type Parameters:
        T - the VIL type
        Parameters:
        type - the type the descriptor shall be created for
        Returns:
        the descriptor
        Throws:
        VilException - in case that creating the descriptor fails
      • testInstantiatorType

        private static void testInstantiatorType​(TypeDescriptor<? extends IVilType> type,
                                                 Instantiator inst)
                                          throws VilException
        Some basic semantic tests for instantiators.
        Parameters:
        type - the type to be tested
        inst - the instatiator annotation
        Throws:
        VilException - in case of failing tests
      • register

        private <T extends IVilType> void register​(TypeDescriptor<T> desc,
                                                   java.lang.Class<T> metaProvider)
        Registers an individual type via its name and emits an information message on the logger.
        Type Parameters:
        T - the actual VIL type
        Parameters:
        desc - the type descriptor being registered
        metaProvider - the class holding the @Meta annotation for registering equivalent classes (may be null)
        See Also:
        registerEquivalentClasses(Class, TypeDescriptor)
      • register

        protected void register​(java.lang.String key,
                                TypeDescriptor<?> desc)
        Registers an individual type and emits an information message on the logger.
        Parameters:
        key - the type name to register for
        desc - the type descriptor being registered
      • registerEquivalentClasses

        private <T extends IVilType> void registerEquivalentClasses​(java.lang.Class<T> type,
                                                                    TypeDescriptor<T> desc)
        Registers equivalent classes from ClassMeta.equiv().
        Type Parameters:
        T - the actual VIL type
        Parameters:
        type - the type being registered
        desc - the already created descriptor for type
      • cacheInheritedAnnotations

        private static void cacheInheritedAnnotations​(java.lang.Class<?> cls)
        Caches the inherited annotations and avoids repeated class analysis. [performance]
        Parameters:
        cls - the class to be cached
      • hasInheritedInvisibleAnnotation

        static boolean hasInheritedInvisibleAnnotation​(java.lang.String signature,
                                                       java.lang.Class<?> cls)
        Returns whether a certain method given by its signature (see OperationDescriptor.getSignature() has an inherited invisible annotation. Helper method for subsequent class analysis.
        Parameters:
        signature - the signature of the method to be queried for
        cls - the declaring class of the method
        Returns:
        true if it has an inherited Invisible annotation, false else
      • register

        public <T extends IVilTypeTypeDescriptor<T> register​(java.lang.Class<T> type)
        Internal default registration function with error handling.
        Type Parameters:
        T - the actual implementing type
        Parameters:
        type - the type class to be registered
        Returns:
        the corresponding type descriptor (may be null)
      • allTypes

        public java.lang.Iterable<TypeDescriptor<?>> allTypes()
        Provides access to all type descriptors.
        Returns:
        all type descriptors (without a specific sequence)
      • allInstantiators

        public java.lang.Iterable<TypeDescriptor<? extends IVilType>> allInstantiators()
        Provides access to all instantiators.
        Returns:
        all instantiators (without a specific sequence)
      • typesByNamePrefix

        public java.lang.Iterable<TypeDescriptor<?>> typesByNamePrefix​(java.lang.String prefix)
        Provides access to all type descriptors with names starting with the given prefix.
        Parameters:
        prefix - the prefix to look for
        Returns:
        the type descriptors matching the prefix (without a specific sequence)
      • getTypeOrFallback

        public TypeDescriptor<?> getTypeOrFallback​(IDatatype type)
        Returns the registered type descriptor for the given type or tries an explicitly registered fallback, e.g., for unknown types in models without advices.
        Parameters:
        type - the type to look for
        Returns:
        the type or null if none was registered
        See Also:
        getType(IDatatype)
      • allowFallbacks

        protected boolean allowFallbacks()
        Whether fallback datatypes are allowed to be stored on this registry.
        Returns:
        true for allowed, false else
      • registerFallbackType

        public void registerFallbackType​(IDatatype type,
                                         TypeDescriptor<?> descriptor)
        Registers a fallback type.
        Parameters:
        type - the IVML datatype to register the fallback descriptor for
        descriptor - the fallback descriptor
      • getType

        public TypeDescriptor<?> getType​(IDatatype type)
        Returns the registered type descriptor for the given type.
        Parameters:
        type - the type to look for
        Returns:
        the type or null if none was registered
      • resolveGenerics

        public TypeDescriptor<?>[] resolveGenerics​(IDatatype type)
        Resolves IVML type generics.
        Parameters:
        type - the type to resolve the generics fro
        Returns:
        the generics, null if there are no generics or the generics are not resolvable
        See Also:
        getType(IDatatype)
      • getType

        public TypeDescriptor<?> getType​(java.lang.String name)
        Returns the registered type descriptor for the given name also in parentRegistry.
        Parameters:
        name - the name to look for
        Returns:
        the type descriptor or null if no one is registered for name
      • getType

        public TypeDescriptor<?> getType​(java.lang.String name,
                                         boolean addIfMissing)
        Returns the registered type descriptor for the given name also in parentRegistry.
        Parameters:
        name - the name to look for
        addIfMissing - allow to dynamically add the type if it is missing through a type resolver (this shall be prevented for internal types!)
        Returns:
        the type descriptor or null if no one is registered for name
      • findType

        private TypeDescriptor<?> findType​(java.lang.String name)
        Searches for a type called name, first in this registry, then in parent registries if given.
        Parameters:
        name - the name of the type
        Returns:
        the found type, null if not found
      • hasType

        public boolean hasType​(java.lang.String name)
        Returns whether this type registry knows a type of the given name.
        Parameters:
        name - the name to search for
        Returns:
        true if the type with name is known, false else
      • resolve

        private TypeDescriptor<?> resolve​(java.lang.String name,
                                          boolean addIfMissing)
        Resolves the given type name via the resolvers.
        Parameters:
        name - the name to look for
        addIfMissing - allow to dynamically add the type if it is missing through a type resolver (this shall be prevented for internal types!)
        Returns:
        the resolved type, null if none
      • getMapType

        public static final TypeDescriptor<? extends Map<?,​?>> getMapType​(TypeDescriptor<?>... parameter)
                                                                         throws VilException
        Returns the VIL type for a map with the given type parameters.
        Parameters:
        parameter - the type parameter
        Returns:
        the type
        Throws:
        VilException - in case that the derivation of the type fails
      • getIteratorType

        public static final TypeDescriptor<? extends PseudoIterator<?>> getIteratorType​(TypeDescriptor<?>... parameter)
                                                                                 throws VilException
        Returns the VIL type for a map with the given type parameters.
        Parameters:
        parameter - the type parameter
        Returns:
        the type
        Throws:
        VilException - in case that the derivation of the type fails
      • getCollectionType

        public static final TypeDescriptor<? extends Collection<?>> getCollectionType​(TypeDescriptor<?>... parameter)
                                                                               throws VilException
        Returns the VIL type for a collection with the given type parameters.
        Parameters:
        parameter - the type parameter
        Returns:
        the type
        Throws:
        VilException - in case that the derivation of the type fails
      • getSetType

        public static final TypeDescriptor<? extends Set<?>> getSetType​(TypeDescriptor<?>... parameter)
                                                                 throws VilException
        Returns the VIL type for a set with the given type parameters.
        Parameters:
        parameter - the type parameter
        Returns:
        the type
        Throws:
        VilException - in case that the derivation of the type fails
      • getSequenceType

        public static final TypeDescriptor<? extends Sequence<?>> getSequenceType​(TypeDescriptor<?>... parameter)
                                                                           throws VilException
        Returns the VIL type for a sequence with the given type parameters.
        Parameters:
        parameter - the type parameter
        Returns:
        the type
        Throws:
        VilException - in case that the derivation of the type fails
      • getInstantiator

        public TypeDescriptor<? extends IVilType> getInstantiator​(java.lang.String name)
        Returns the registered instantiator type descriptor for the given name also in parentRegistry.
        Parameters:
        name - the name to look for
        Returns:
        the type descriptor or null if no one is registered for name
      • resolvableOperationType

        public static final TypeDescriptor<?> resolvableOperationType()
        Returns the type descriptor for the built-in operation references (a kind of function pointer to evaluate functions generically).
        Returns:
        the type descriptor
      • stringType

        public static final TypeDescriptor<?> stringType()
        Returns the type descriptor for the built-in type String.
        Returns:
        the type descriptor
      • booleanType

        public static final TypeDescriptor<?> booleanType()
        Returns the type descriptor for the built-in type 'Boolean'.
        Returns:
        the type descriptor
      • versionType

        public static final TypeDescriptor<?> versionType()
        Returns the type descriptor for the built-in type 'Version'.
        Returns:
        the type descriptor
      • anyType

        public static final TypeDescriptor<?> anyType()
        Returns the type descriptor for the built-in type 'any'.
        Returns:
        the type descriptor
      • voidType

        public static final TypeDescriptor<?> voidType()
        Returns the type descriptor for the built-in type 'void'.
        Returns:
        the type descriptor
      • typeType

        public static final TypeDescriptor<?> typeType()
        Returns the type descriptor for the built-in type 'Type'.
        Returns:
        the type descriptor
      • integerType

        public static final TypeDescriptor<?> integerType()
        Returns the type descriptor for the built-in type 'Integer'.
        Returns:
        the type descriptor
      • realType

        public static final TypeDescriptor<?> realType()
        Returns the type descriptor for the built-in type 'Real'.
        Returns:
        the type descriptor
      • getType

        public TypeDescriptor<?> getType​(java.lang.Class<?> cls)
        Converts a class into a type descriptor.
        Parameters:
        cls - the classes to be converted
        Returns:
        the corresponding type descriptor or null
      • isInternalType

        public static boolean isInternalType​(java.lang.Class<?> cls)
        Returns whether cls is considered as an internal type including implementation types such as the collections.
        Parameters:
        cls - the class to be considered
        Returns:
        true
      • findType

        public TypeDescriptor<?> findType​(java.lang.Class<?> cls)
        Finds the type descriptor for a given class.
        Parameters:
        cls - the classes to be converted
        Returns:
        the corresponding type descriptor or null
      • findType

        public TypeDescriptor<?> findType​(java.lang.Class<?> cls,
                                          boolean addIfMissing)
        Finds the type descriptor for a given class.
        Parameters:
        cls - the classes to be converted
        addIfMissing - allow to dynamically add the type if it is missing through a type resolver (this shall be prevented for internal types!)
        Returns:
        the corresponding type descriptor or null
      • convert

        public TypeDescriptor<?>[] convert​(java.lang.Class<?> param)
        Converts a class into a set of type descriptors.
        Parameters:
        param - the class to be converted
        Returns:
        the resulting type descriptors, null if params is null or empty. If a type descriptor cannot be found, TypeDescriptor#VOID is used instead.
      • convert

        public TypeDescriptor<?>[] convert​(java.lang.Class<?>... params)
        Converts a set of classes into type descriptors.
        Parameters:
        params - the classes to be converted
        Returns:
        the resulting type descriptors, null if params is null or empty. If a type descriptor cannot be found, TypeDescriptor#VOID is used instead.
      • forRuntime

        public boolean forRuntime()
        Returns whether this registry provides types for runtime variability instantiation. This is the case if this or at least one of the parent type registries indicates runtime type support.
        Returns:
        true if this registry or one of its parent registries provides runtime types, false else
      • equals

        public static final boolean equals​(IMetaType t1,
                                           IMetaType t2)
        Returns whether two types are equal including base type considerations.
        Parameters:
        t1 - the first type to be compared
        t2 - the second type to be compared
        Returns:
        true if t1 and t2 are equal, false else
      • toName

        public static java.lang.String toName​(IMetaType type)
        Returns the VIL name of type.
        Parameters:
        type - the type to return the name for
        Returns:
        the VIL name (if possible), the name else
      • addTypeMapping

        public static void addTypeMapping​(java.lang.String name,
                                          TypeDescriptor<? extends IVilType> type)
        Allows to register internal mapped types, e.g., TypeDescriptor.
        Parameters:
        name - the name of the type
        type - the actual type
      • getMappedType

        protected TypeDescriptor<?> getMappedType​(java.lang.Class<?> cls,
                                                  java.lang.Class<?>[] generics,
                                                  IntHolder pos)
        Returns mapped types provided by this registry and the descriptors created by this registry. This can be used to map Java collections into and from VIL.
        Parameters:
        cls - the class to be turned into a type
        generics - the generics to be applied to the type (may be null)
        pos - the modifiable position within generics, change only to indicate state of processing of generics
        Returns:
        the type descriptor or null if none is provided
      • obtainTypeDescriptor

        public TypeDescriptor<?> obtainTypeDescriptor​(java.lang.Object obj)
        Obtains the type descriptor of an object.
        Parameters:
        obj - the object the descriptor shall be returned for
        Returns:
        the descriptor
      • addTypeAlias

        public <T> boolean addTypeAlias​(java.lang.String name,
                                        TypeDescriptor<T> type)
                                 throws VilException
        Adds a type alias to this specific type registry.
        Type Parameters:
        T - the specific VilType or Artifact
        Parameters:
        name - the name of the type alias
        type - the type to be aliased
        Returns:
        true if successful, false else
        Throws:
        java.lang.IllegalArgumentException - in case that name is null or empty, type is null or this function shall be applied on DEFAULT
        VilException - if creating the type fails