Class TypeDescriptor<T>
- java.lang.Object
-
- net.ssehub.easy.instantiation.core.model.vilTypes.TypeDescriptor<T>
-
- Type Parameters:
T- the specific VilType or Artifact
- All Implemented Interfaces:
IMetaType
- Direct Known Subclasses:
AbstractIvmlTypeDescriptor,AliasTypeDescriptor,CompoundTypeDescriptor,FakeTypeDescriptor,ReflectionTypeDescriptor
public abstract class TypeDescriptor<T> extends java.lang.Object implements IMetaType
Represents an actual VIL type, its meta information, fields and operations.- Author:
- Holger Eichelberger
-
-
Field Summary
Fields Modifier and Type Field Description private OperationDescriptor[]conversionsstatic TypeDescriptor<?>[]EMPTYprivate FieldDescriptor[]fieldsprivate java.lang.Stringnameprivate OperationDescriptor[]operationsprivate TypeDescriptor<?>[]parameterprivate java.lang.StringqualifiedName
-
Constructor Summary
Constructors Modifier Constructor Description protectedTypeDescriptor(TypeDescriptor<?>... parameter)Creates a new type descriptor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddOperation(OperationDescriptor operation)Adds an operation.abstract OperationDescriptoraddPlaceholderOperation(java.lang.String name, int parameterCount, boolean acceptsNamedParameters)Adds a placeholder operation, i.e., in case that the original operation cannot be resolved but the script shall remain executable.Set<?>allInstances()Provides access to all instances of this type.protected java.lang.StringappendParameter(java.lang.String name, int exclude)Appends the type parameters toname.abstract booleancanBeInstantiated()Returns whethercreate(Object...)will return an instance.abstract Tcreate(java.lang.Object... params)Creates an instance of the type according to the given parameters.static TypeDescriptor<?>[]createArray(int length)Creates an empty array of type descriptors.booleanenableDynamicDispatch()Returns whether this type enables dynamic dispatch of operations.OperationDescriptorfindCollectionIteratorOperation(java.lang.String name)Returns a collection iterator operation with givenname.IMetaOperationfindConversion(IMetaType sourceType, IMetaType targetType)Returns the most specific conversion operation from this type to target type (if there is any).OperationDescriptorfindConversion(TypeDescriptor<?> sourceType, TypeDescriptor<?> targetType)Returns the most specific conversion operation from this type to target type (if there is any).static OperationDescriptorfindConversionOnBoth(TypeDescriptor<?> sourceType, TypeDescriptor<?> targetType)Returns the most specific conversion operation from this type to target type considering both types as declarators of the conversion operation (if there is any).TypeDescriptor<?>flatten()Returns a flattened collection type if this type is a collection.TypeDescriptor<?>flattenParam()Returns deepest nested non-collection parameter type of this type.private TypeDescriptor<?>flattenParam(TypeDescriptor<?> type)Returns deepest nested non-collection parameter type.java.util.List<IMetaOperation>getCandidates(java.lang.String name, int unnamedArgsCount)Returns operation candidates matching the given name and number of unnamed (required) arguments.static java.util.List<IMetaOperation>getCandidates(IMetaType type, java.lang.String name, int argCount)Returns the potential operation candidates for a givennameand parameter numbers on a specifictype.OperationDescriptorgetConversion(int index)Returns the specified conversion.java.lang.Iterable<OperationDescriptor>getConversions()Returns the available conversions.intgetConversionsCount()Returns the number of conversions provided by this type descriptor.OperationDescriptorgetConversionToSequence()Returns the first registered conversion operation from this type to a sequence.java.lang.ObjectgetDefaultValue()Returns the default value for this type, usually a neutral value or a constant value to compare against.FieldDescriptorgetField(int index)Returns the specified field.FieldDescriptorgetField(java.lang.String name)Returns the field descriptor with the specified name.intgetFieldCount()Returns the number of fields provided by this type.java.lang.Iterable<FieldDescriptor>getFields()Returns the available fields.TypeDescriptor<?>[]getGenericParameter()Returns the type parameters as array.intgetGenericParameterCount()Returns the number of generic type parameters.TypeDescriptor<?>getGenericParameterType(int index)Returns the specified generic parameter type.java.lang.StringgetName()Returns the (simple) name of this type.OperationDescriptorgetOperation(int index)Returns the specified operation.java.lang.Iterable<OperationDescriptor>getOperations()Returns the available operations.intgetOperationsCount()Returns the number of operations provided by this type.java.lang.StringgetQualifiedName()Returns the qualified name of this type.abstract java.lang.Class<T>getTypeClass()Returns the class of the described VIL type.java.lang.StringgetVilName()Returns the name of this type in VIL notation.abstract booleanisAssignableFrom(TypeDescriptor<?> desc)Returns whether this descriptor is the same or a super class ofdesc.abstract booleanisCollection()Returns whether this descriptor represents a VIL collection, i.e., set, sequence or collection.protected booleanisFieldsNull()Returns whether so far no fields have been defined at all.protected booleanisInitialized()Returns whether this descriptor was initialized.abstract booleanisInstance(java.lang.Object object)Returns whether the givenobjectis an instance of this descriptor.abstract booleanisInstantiator()Returns whether this type is an instantiator.abstract booleanisIterator()Returns whether this descriptor represents an iterator.abstract booleanisMap()Returns whether this descriptor represents a VIL map.protected booleanisNameSet()Returns whether the name of this type descriptor (single or qualified) has yet been set.booleanisSame(TypeDescriptor<?> typeDescriptor)Returns whether a givenobjectis the same.abstract booleanisSameType(java.lang.Object object)Returns whether the givenobjectof of the type of this descriptor.abstract booleanisSequence()Returns whether this descriptor represents a VIL sequence.abstract booleanisSet()Returns whether this descriptor represents a VIL set.protected voidsetConversions(java.util.Collection<OperationDescriptor> conversions)Defines the conversions for this type.protected voidsetFields(java.util.Collection<FieldDescriptor> fields)Defines the fields for this type.protected voidsetFields(FieldDescriptor[] fields)Changes the fields.protected voidsetName(java.lang.String name)Defines the name of this descriptor explicitly.protected voidsetOperations(java.util.Collection<OperationDescriptor> operations)Defines the operations for this type.java.lang.StringtoString()Returns a textual representation of this descriptor (the Java signature).-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.ssehub.easy.instantiation.core.model.vilTypes.IMetaType
checkConversion, getBaseType, getSuperType, getTypeRegistry, isActualTypeOf, isAssignableFrom, isBasicType, isInternal, isPlaceholder
-
-
-
-
Field Detail
-
EMPTY
public static final TypeDescriptor<?>[] EMPTY
-
name
private java.lang.String name
-
qualifiedName
private java.lang.String qualifiedName
-
operations
private OperationDescriptor[] operations
-
fields
private FieldDescriptor[] fields
-
conversions
private OperationDescriptor[] conversions
-
parameter
private TypeDescriptor<?>[] parameter
-
-
Constructor Detail
-
TypeDescriptor
protected TypeDescriptor(TypeDescriptor<?>... parameter) throws VilException
Creates a new type descriptor. Overridden constructors shall callsetOperations(java.util.Collection),setFields(java.util.Collection)orsetConversions(java.util.Collection).- Parameters:
parameter- type parameter (may be null)- Throws:
VilException- if initialization fails for some reason
-
-
Method Detail
-
isInitialized
protected boolean isInitialized()
Returns whether this descriptor was initialized.- Returns:
trueif it was initialized,falseelse
-
setName
protected void setName(java.lang.String name)
Defines the name of this descriptor explicitly.- Parameters:
name- the name of this descriptor (may be qualified by :: and is split internally)
-
isNameSet
protected boolean isNameSet()
Returns whether the name of this type descriptor (single or qualified) has yet been set.- Returns:
- true if the name has been set,
falseelse
-
setOperations
protected void setOperations(java.util.Collection<OperationDescriptor> operations)
Defines the operations for this type.- Parameters:
operations- the operations (may be null)
-
setFields
protected void setFields(java.util.Collection<FieldDescriptor> fields)
Defines the fields for this type.- Parameters:
fields- the fields (may be null)
-
setFields
protected void setFields(FieldDescriptor[] fields)
Changes the fields.- Parameters:
fields- the new fields
-
isFieldsNull
protected boolean isFieldsNull()
Returns whether so far no fields have been defined at all.- Returns:
truefor no fields,falseelse
-
addOperation
protected void addOperation(OperationDescriptor operation)
Adds an operation.- Parameters:
operation- the operation to be added
-
setConversions
protected void setConversions(java.util.Collection<OperationDescriptor> conversions)
Defines the conversions for this type.- Parameters:
conversions- the conversions (may be null)
-
getName
public java.lang.String getName()
Description copied from interface:IMetaTypeReturns the (simple) name of this type.
-
getQualifiedName
public java.lang.String getQualifiedName()
Description copied from interface:IMetaTypeReturns the qualified name of this type.- Specified by:
getQualifiedNamein interfaceIMetaType- Returns:
- the qualified name
-
enableDynamicDispatch
public boolean enableDynamicDispatch()
Description copied from interface:IMetaTypeReturns whether this type enables dynamic dispatch of operations.- Specified by:
enableDynamicDispatchin interfaceIMetaType- Returns:
trueif it enables dynamic dispatch,falseelse
-
getTypeClass
public abstract java.lang.Class<T> getTypeClass()
Returns the class of the described VIL type. Please use this method sparingly as the required functionality is provided by this class.- Returns:
- the class
-
canBeInstantiated
public abstract boolean canBeInstantiated()
Returns whethercreate(Object...)will return an instance.- Returns:
trueif this type can be instantiated from VIL/VTL,falseelse
-
create
public abstract T create(java.lang.Object... params) throws VilException
Creates an instance of the type according to the given parameters.- Parameters:
params- the parameters- Returns:
- the created instance
- Throws:
VilException- in case that the creation does not work
-
getFields
public java.lang.Iterable<FieldDescriptor> getFields()
Returns the available fields.- Returns:
- the available fields
-
getOperations
public java.lang.Iterable<OperationDescriptor> getOperations()
Returns the available operations.- Returns:
- the available operations
-
getConversions
public java.lang.Iterable<OperationDescriptor> getConversions()
Returns the available conversions.- Returns:
- the available conversion
-
getOperationsCount
public int getOperationsCount()
Description copied from interface:IMetaTypeReturns the number of operations provided by this type.- Specified by:
getOperationsCountin interfaceIMetaType- Returns:
- the number of operations
-
getOperation
public OperationDescriptor getOperation(int index)
Description copied from interface:IMetaTypeReturns the specified operation.- Specified by:
getOperationin interfaceIMetaType- Parameters:
index- the 0-based index of the operation to be returned- Returns:
- the specified operation
-
getField
public FieldDescriptor getField(int index)
Description copied from interface:IMetaTypeReturns the specified field.
-
getField
public FieldDescriptor getField(java.lang.String name)
Returns the field descriptor with the specified name.- Parameters:
name- the name of the field- Returns:
- the field descriptor (may be null if not found)
-
getFieldCount
public int getFieldCount()
Description copied from interface:IMetaTypeReturns the number of fields provided by this type.- Specified by:
getFieldCountin interfaceIMetaType- Returns:
- the number of fields
-
getConversionsCount
public int getConversionsCount()
Returns the number of conversions provided by this type descriptor.- Returns:
- the number of conversions, i.e., from this type to other types
-
getConversion
public OperationDescriptor getConversion(int index)
Returns the specified conversion.- Parameters:
index- the index of the conversion to be returned- Returns:
- the specified conversion
- Throws:
java.lang.IndexOutOfBoundsException- ifindex < 0 || index>=getConversionsCount()
-
isAssignableFrom
public abstract boolean isAssignableFrom(TypeDescriptor<?> desc)
Returns whether this descriptor is the same or a super class ofdesc.- Parameters:
desc- the descriptor to be tested- Returns:
trueif both descriptors are assignment compatible,falseelse
-
findConversion
public IMetaOperation findConversion(IMetaType sourceType, IMetaType targetType)
Returns the most specific conversion operation from this type to target type (if there is any).- Specified by:
findConversionin interfaceIMetaType- Parameters:
sourceType- the source type to convert fromtargetType- the target type to convert to- Returns:
- the most specific conversion operation as defined in the underlying implementation class, null if no matching can be found
-
findConversionOnBoth
public static final OperationDescriptor findConversionOnBoth(TypeDescriptor<?> sourceType, TypeDescriptor<?> targetType)
Returns the most specific conversion operation from this type to target type considering both types as declarators of the conversion operation (if there is any).- Parameters:
sourceType- the source type to convert fromtargetType- the target type to convert to- Returns:
- the most specific conversion operation as defined in the underlying implementation class, null if no matching can be found
-
findConversion
public OperationDescriptor findConversion(TypeDescriptor<?> sourceType, TypeDescriptor<?> targetType)
Returns the most specific conversion operation from this type to target type (if there is any).- Parameters:
sourceType- the source type to convert fromtargetType- the target type to convert to- Returns:
- the most specific conversion operation as defined in the underlying implementation class, null if no matching can be found
-
getConversionToSequence
public OperationDescriptor getConversionToSequence()
Returns the first registered conversion operation from this type to a sequence.- Returns:
- the conversion operation or null if none was found
-
toString
public java.lang.String toString()
Returns a textual representation of this descriptor (the Java signature).- Overrides:
toStringin classjava.lang.Object- Returns:
- the textual representation
-
getGenericParameter
public TypeDescriptor<?>[] getGenericParameter()
Returns the type parameters as array.- Returns:
- the type parameters as array, may be null
-
getGenericParameterCount
public int getGenericParameterCount()
Description copied from interface:IMetaTypeReturns the number of generic type parameters.- Specified by:
getGenericParameterCountin interfaceIMetaType- Returns:
- the number of generic type parameters
-
getGenericParameterType
public TypeDescriptor<?> getGenericParameterType(int index)
Description copied from interface:IMetaTypeReturns the specified generic parameter type.- Specified by:
getGenericParameterTypein interfaceIMetaType- Parameters:
index- the index of the parameter type to return- Returns:
- the specified parameter type
-
createArray
public static TypeDescriptor<?>[] createArray(int length)
Creates an empty array of type descriptors.- Parameters:
length- the length of the array to be created- Returns:
- the array instance
-
appendParameter
protected java.lang.String appendParameter(java.lang.String name, int exclude)Appends the type parameters toname.- Parameters:
name- the VIL name of this typeexclude- the parameters from the end to exclude,0for none- Returns:
- the VIL name including parameter types if applicable
-
getVilName
public java.lang.String getVilName()
Returns the name of this type in VIL notation.- Returns:
- the name of this type
-
isMap
public abstract boolean isMap()
Returns whether this descriptor represents a VIL map.- Returns:
trueif this is a VIL map,falseelse
-
isIterator
public abstract boolean isIterator()
Returns whether this descriptor represents an iterator.- Returns:
trueif this is an iterator,falseelse
-
isCollection
public abstract boolean isCollection()
Returns whether this descriptor represents a VIL collection, i.e., set, sequence or collection.- Returns:
trueif this is a VIL collection,falseelse
-
isSet
public abstract boolean isSet()
Returns whether this descriptor represents a VIL set.- Returns:
trueif this is a VIL set,falseelse
-
isSequence
public abstract boolean isSequence()
Returns whether this descriptor represents a VIL sequence.- Returns:
trueif this is a VIL sequence,falseelse
-
isInstance
public abstract boolean isInstance(java.lang.Object object)
Returns whether the givenobjectis an instance of this descriptor.- Parameters:
object- the object to be checked- Returns:
trueif it is an instance,falseelse
-
isSameType
public abstract boolean isSameType(java.lang.Object object)
Returns whether the givenobjectof of the type of this descriptor.- Parameters:
object- the object to be checked- Returns:
trueifobjecthas the same type,falseelse
-
addPlaceholderOperation
public abstract OperationDescriptor addPlaceholderOperation(java.lang.String name, int parameterCount, boolean acceptsNamedParameters)
Adds a placeholder operation, i.e., in case that the original operation cannot be resolved but the script shall remain executable.- Specified by:
addPlaceholderOperationin interfaceIMetaType- Parameters:
name- the name of the operationparameterCount- the number of parameters of the operationacceptsNamedParameters- whether the operation accepts named parameters- Returns:
- the added operation, null if this type is not a
placeholder
-
isInstantiator
public abstract boolean isInstantiator()
Returns whether this type is an instantiator.- Returns:
trueif it is an instantiator,falseelse
-
isSame
public boolean isSame(TypeDescriptor<?> typeDescriptor)
Returns whether a givenobjectis the same.- Parameters:
typeDescriptor- the object to be checked- Returns:
trueif the object is the same,falseelse
-
flatten
public TypeDescriptor<?> flatten() throws VilException
Returns a flattened collection type if this type is a collection. If not, return this.- Returns:
- the flattened collection type or this
- Throws:
VilException- in case that the derivation of the type fails
-
flattenParam
public TypeDescriptor<?> flattenParam()
Returns deepest nested non-collection parameter type of this type.- Returns:
- the nested non-collection type
-
flattenParam
private TypeDescriptor<?> flattenParam(TypeDescriptor<?> type)
Returns deepest nested non-collection parameter type.- Parameters:
type- the type to return the parameter type for- Returns:
- the nested non-collection type
-
findCollectionIteratorOperation
public OperationDescriptor findCollectionIteratorOperation(java.lang.String name)
Returns a collection iterator operation with givenname.- Parameters:
name- the name of the operation to search for- Returns:
- the collection iterator operation or null
-
allInstances
public Set<?> allInstances()
Provides access to all instances of this type.- Returns:
- access to all instances (may be null if there are none)
-
getDefaultValue
public java.lang.Object getDefaultValue()
Returns the default value for this type, usually a neutral value or a constant value to compare against.- Returns:
- the default value, null for none - may lead to an undefined operation execution
-
getCandidates
public java.util.List<IMetaOperation> getCandidates(java.lang.String name, int unnamedArgsCount)
Description copied from interface:IMetaTypeReturns operation candidates matching the given name and number of unnamed (required) arguments.- Specified by:
getCandidatesin interfaceIMetaType- Parameters:
name- the name of the operationunnamedArgsCount- the number of unnamed arguments- Returns:
- the operation candidates
-
getCandidates
public static java.util.List<IMetaOperation> getCandidates(IMetaType type, java.lang.String name, int argCount)
Returns the potential operation candidates for a givennameand parameter numbers on a specifictype. This is the most basic implementation done by a linear search on the available operations. An optimized version may/shall be realized viagetCandidates(String, int).- Parameters:
type- the type to be searched forname- the name of the operation call to be resolvedargCount- the number of required arguments- Returns:
trueifdescis a candidate,falseelse
-
-