Class ReflectionTypeDescriptor<T>

  • Type Parameters:
    T - the specific VilType or Artifact
    All Implemented Interfaces:
    IMetaType
    Direct Known Subclasses:
    ActualValueReflectionTypeDescriptor

    public class ReflectionTypeDescriptor<T>
    extends TypeDescriptor<T>
    Represents an actual type based on reflection analysis of the underlying class. Instances of this class are created upon registration. This class works on the default type registry (only). Thereby, the available operations are determined and cached for fast access. Considers IActualTypeProvider.
    Author:
    Holger Eichelberger
    • Constructor Detail

      • ReflectionTypeDescriptor

        protected ReflectionTypeDescriptor​(java.lang.Class<T> cls)
                                    throws VilException
        Creates a new type descriptor.
        Parameters:
        cls - the class to create the type descriptor for
        Throws:
        VilException - if analyzing the class fails for some reason
      • ReflectionTypeDescriptor

        ReflectionTypeDescriptor​(java.lang.Class<T> cls,
                                 TypeDescriptor<?>... parameter)
                          throws VilException
        Creates a new type descriptor.
        Parameters:
        cls - the class to create the type descriptor for
        parameter - type parameter (may be null)
        Throws:
        VilException - if analyzing the class fails for some reason
    • Method Detail

      • resolve

        protected ReflectionTypeDescriptor<T> resolve()
                                               throws VilException
        Resolves the details of the type. Required to register the type first in order to allow resolution of the own type in operations and fields.
        Returns:
        this (builder pattern)
        Throws:
        VilException - in case of resolution problems
      • resolveFields

        protected void resolveFields()
        Resolves the fields.
      • processInner

        protected void processInner​(java.lang.Class<?> cls)
                             throws VilException
        Processes the inner classes of cls.
        Parameters:
        cls - the class
        Throws:
        VilException - in case of failures
      • addConversions

        private void addConversions​(java.lang.Class<?> cls,
                                    java.util.List<OperationDescriptor> convs)
        Adds the the conversions in cls to convs.
        Parameters:
        cls - the class to analyze
        convs - the conversions (to be modified as a side effect)
      • addOperation

        private void addOperation​(java.util.Map<java.lang.String,​OperationDescriptor> operations,
                                  java.lang.String sig,
                                  OperationDescriptor desc)
        Adds a specific operation descriptor via its signature to the temporary operation map. Adjusts canBeInstantiated.
        Parameters:
        operations - the map of operations (signature-descriptor) to be modified as a side effect
        sig - the signature
        desc - the operation descriptor to be added
      • addMethods

        private void addMethods​(java.util.Map<java.lang.String,​OperationDescriptor> operations,
                                java.lang.Class<?> cls,
                                java.lang.Class<?> start,
                                java.lang.String instantiatorName)
        Adds all (relevant and not invisible) methods provided by cls. This method works recursively on superclasses of cls.
        Parameters:
        operations - the map of operations (signature-descriptor) to be modified as a side effect
        cls - the class to be analyzed
        start - the class for which this call was initiated
        instantiatorName - the name of the instantiator (may be null)
      • enableMethod

        protected boolean enableMethod​(java.lang.reflect.Method method)
        Returns whether a method shall be added.
        Parameters:
        method - the method to be added
        Returns:
        true if the method shall be added, false else
      • considerAsConstructor

        protected boolean considerAsConstructor​(java.lang.reflect.Method method)
        Returns whether the given method shall be considered as constructor.
        Parameters:
        method - the method to be checked
        Returns:
        true if the method shall be considered as constructor, false else
      • addMethod

        private void addMethod​(java.util.Map<java.lang.String,​OperationDescriptor> operations,
                               java.lang.reflect.Method method,
                               java.lang.String instantiatorName)
        Adds the given method to the operations map.
        Parameters:
        operations - the map of operations (signature-descriptor) to be modified as a side effect
        method - the method to be added
        instantiatorName - the name of the instantiator (may be null)
        See Also:
        OperationDescriptor.isOperationOrConstructor(Method)
      • createDescriptor

        protected ReflectionOperationDescriptor createDescriptor​(java.lang.reflect.Method method,
                                                                 java.lang.String name,
                                                                 boolean constructor)
        Creates a reflection operation descriptor for an usual method call.
        Parameters:
        method - the method to be wrapped into the descriptor
        name - the name of the operation (null use the one of method)
        constructor - whether the method is considered to be a constructor
        Returns:
        the descriptor
      • filterMethodByName

        private static boolean filterMethodByName​(java.lang.reflect.Method method,
                                                  java.lang.String instantiatorName)
        Returns whether the method shall be considered based on its name. In instantiators, only methods with the instantiator name or constructors are considered.
        Parameters:
        method - the method to be checked
        instantiatorName - the instantiator name signalling that we check for an instantiator (may be null in case of an ordinary IVilType or an artifact)
        Returns:
        true if it shall be considered, false else
      • registerAliasOperation

        private boolean registerAliasOperation​(java.util.Map<java.lang.String,​OperationDescriptor> operations,
                                               java.lang.reflect.Method method)
        Register the given method as an alias operation, i.e., getters without Constants.GETTER_PREFIX. This method considers OperationMeta.
        Parameters:
        operations - the map of operations (signature-descriptor) to be modified as a side effect
        method - the method to be considered
        Returns:
        true register also the original operation, false do not register the original operation
      • stripGetterPrefix

        public static java.lang.String stripGetterPrefix​(java.lang.String name)
        Strips the getter prefix Constants.GETTER_PREFIX from name.
        Parameters:
        name - the name to strip
        Returns:
        the name without the getter prefix, null if there was nothing to strip off
      • getRegName

        public static java.lang.String getRegName​(java.lang.Class<?> cls)
        Returns the name how a class shall be registered based on ClassMeta.
        Parameters:
        cls - the class to be considered
        Returns:
        the name
      • getAlias

        public static java.lang.String getAlias​(java.lang.Class<?> cls)
        Returns a possible alias for cls based on ClassMeta.
        Parameters:
        cls - the class to be considered
        Returns:
        the alias or null if there is none
      • getAlias

        private static java.lang.String getAlias​(ClassMeta meta)
        Returns a possible alias for ClassMeta.
        Parameters:
        meta - the annotation to query
        Returns:
        the alias or null if there is none
      • getName

        public java.lang.String getName()
        Description copied from interface: IMetaType
        Returns the (simple) name of this type.
        Specified by:
        getName in interface IMetaType
        Overrides:
        getName in class TypeDescriptor<T>
        Returns:
        the simple name
      • create

        public T create​(java.lang.Object... params)
                 throws VilException
        Creates an instance of the type according to the given parameters.
        Specified by:
        create in class TypeDescriptor<T>
        Parameters:
        params - the parameters
        Returns:
        the created instance
        Throws:
        VilException - in case that the creation does not work
      • getTypeClass

        public java.lang.Class<T> getTypeClass()
        Description copied from class: TypeDescriptor
        Returns the class of the described VIL type. Please use this method sparingly as the required functionality is provided by this class.
        Specified by:
        getTypeClass in class TypeDescriptor<T>
        Returns:
        the class
      • isNAssign

        private boolean isNAssign​(java.lang.Class<?> cls)
        Checks whether the given class cls is considered as a non-assignable class for this type descriptor.
        Parameters:
        cls - the class to be tested
        Returns:
        true if it is non-assignable, false else
        See Also:
        ClassMeta.nAssign()
      • isAssignableFrom

        public boolean isAssignableFrom​(IMetaType type)
        Description copied from interface: IMetaType
        Returns whether this type is the same or a super class of type.
        Parameters:
        type - the descriptor to be tested
        Returns:
        true if both types are assignment compatible, false else
      • isAssignableFrom

        public boolean isAssignableFrom​(TypeDescriptor<?> desc)
        Description copied from class: TypeDescriptor
        Returns whether this descriptor is the same or a super class of desc.
        Specified by:
        isAssignableFrom in class TypeDescriptor<T>
        Parameters:
        desc - the descriptor to be tested
        Returns:
        true if both descriptors are assignment compatible, false else
      • toString

        public java.lang.String toString()
        Returns a textual representation of this descriptor (the Java signature).
        Overrides:
        toString in class TypeDescriptor<T>
        Returns:
        the textual representation
      • getVilName

        public java.lang.String getVilName()
        Description copied from class: TypeDescriptor
        Returns the name of this type in VIL notation.
        Overrides:
        getVilName in class TypeDescriptor<T>
        Returns:
        the name of this type
      • isCollection

        static boolean isCollection​(java.lang.Class<?> cls)
        Returns whether the given class represents a VIL collection.
        Parameters:
        cls - the class to be checked
        Returns:
        true if it is a VIL collection, false else
      • isSet

        static boolean isSet​(java.lang.Class<?> cls)
        Returns whether the given class represents a VIL set.
        Parameters:
        cls - the class to be checked
        Returns:
        true if it is a VIL set, false else
      • isIterator

        static boolean isIterator​(java.lang.Class<?> cls)
        Returns whether the given class represents an iterator.
        Parameters:
        cls - the class to be checked
        Returns:
        true if it is an iterator, false else
      • isSequence

        static boolean isSequence​(java.lang.Class<?> cls)
        Returns whether the given class represents a VIL sequence.
        Parameters:
        cls - the class to be checked
        Returns:
        true if it is a VIL sequence, false else
      • isMap

        static boolean isMap​(java.lang.Class<?> cls)
        Returns whether the given class represents a VIL map.
        Parameters:
        cls - the class to be checked
        Returns:
        true if it is a VIL map, false else
      • isCollection

        public boolean isCollection()
        Description copied from class: TypeDescriptor
        Returns whether this descriptor represents a VIL collection, i.e., set, sequence or collection.
        Specified by:
        isCollection in class TypeDescriptor<T>
        Returns:
        true if this is a VIL collection, false else
      • isIterator

        public boolean isIterator()
        Description copied from class: TypeDescriptor
        Returns whether this descriptor represents an iterator.
        Specified by:
        isIterator in class TypeDescriptor<T>
        Returns:
        true if this is an iterator, false else
      • isMap

        public boolean isMap()
        Description copied from class: TypeDescriptor
        Returns whether this descriptor represents a VIL map.
        Specified by:
        isMap in class TypeDescriptor<T>
        Returns:
        true if this is a VIL map, false else
      • isSet

        public boolean isSet()
        Description copied from class: TypeDescriptor
        Returns whether this descriptor represents a VIL set.
        Specified by:
        isSet in class TypeDescriptor<T>
        Returns:
        true if this is a VIL set, false else
      • isSequence

        public boolean isSequence()
        Description copied from class: TypeDescriptor
        Returns whether this descriptor represents a VIL sequence.
        Specified by:
        isSequence in class TypeDescriptor<T>
        Returns:
        true if this is a VIL sequence, false else
      • isInstance

        public boolean isInstance​(java.lang.Object object)
        Description copied from class: TypeDescriptor
        Returns whether the given object is an instance of this descriptor.
        Specified by:
        isInstance in class TypeDescriptor<T>
        Parameters:
        object - the object to be checked
        Returns:
        true if it is an instance, false else
      • isSameType

        public boolean isSameType​(java.lang.Object object)
        Description copied from class: TypeDescriptor
        Returns whether the given object of of the type of this descriptor.
        Specified by:
        isSameType in class TypeDescriptor<T>
        Parameters:
        object - the object to be checked
        Returns:
        true if object has the same type, false else
      • isBasicType

        public boolean isBasicType()
        Description copied from interface: IMetaType
        Returns whether this instance represents a basic type.
        Returns:
        true if this instance represents a basic type, false else
      • getTypeRegistry

        public TypeRegistry getTypeRegistry()
        Description copied from interface: IMetaType
        Returns the type registry this type was registered by.
        Returns:
        the type registry
      • isPlaceholder

        public boolean isPlaceholder()
        Description copied from interface: IMetaType
        Returns whether this type is valid or whether it is a placeholder type in case that the original type cannot be resolved but the script shall remain executable.
        Returns:
        true if this type is a placeholder, false else
      • addPlaceholderOperation

        public OperationDescriptor addPlaceholderOperation​(java.lang.String name,
                                                           int parameterCount,
                                                           boolean acceptsNamedParameters)
        Description copied from class: TypeDescriptor
        Adds a placeholder operation, i.e., in case that the original operation cannot be resolved but the script shall remain executable.
        Specified by:
        addPlaceholderOperation in interface IMetaType
        Specified by:
        addPlaceholderOperation in class TypeDescriptor<T>
        Parameters:
        name - the name of the operation
        parameterCount - the number of parameters of the operation
        acceptsNamedParameters - whether the operation accepts named parameters
        Returns:
        the added operation, null if this type is not a placeholder
      • isActualTypeOf

        public boolean isActualTypeOf​(IMetaType type)
        Description copied from interface: IMetaType
        Returns whether this type can act as an actual type of type.
        Parameters:
        type - the type to be checked
        Returns:
        true if this type can act as an actual type of type, false else
        See Also:
        IActualTypeProvider
      • getBaseType

        public IMetaType getBaseType()
        Description copied from interface: IMetaType
        Returns the base type (in case of IVML derived types).
        Returns:
        the base type or null
      • isInternal

        public boolean isInternal()
        Description copied from interface: IMetaType
        Returns whether this type is internal and shall not be visible to the user.
        Returns:
        true if this type is internal, false else
      • isInstantiator

        public boolean isInstantiator()
        Description copied from class: TypeDescriptor
        Returns whether this type is an instantiator.
        Specified by:
        isInstantiator in class TypeDescriptor<T>
        Returns:
        true if it is an instantiator, false else
      • getSuperType

        public IMetaType getSuperType()
        Description copied from interface: IMetaType
        Returns the type this type is refined from.
        Returns:
        the super type or null if there is none
      • checkConversion

        public boolean checkConversion​(IMetaType param,
                                       IMetaOperation conversion)
        Description copied from interface: IMetaType
        Checks a conversion from this type to param via the given conversion.
        Parameters:
        param - the original parameter value
        conversion - the conversion function
        Returns:
        true if ok, false else
      • getDefaultValue

        public java.lang.Object getDefaultValue()
        Description copied from class: TypeDescriptor
        Returns the default value for this type, usually a neutral value or a constant value to compare against.
        Overrides:
        getDefaultValue in class TypeDescriptor<T>
        Returns:
        the default value, null for none - may lead to an undefined operation execution