Class Type

  • All Implemented Interfaces:
    Comparable<Type>, Writable

    public class Type
    extends ModelElement
    implements Comparable<Type>
    Represents (a reference to) the type of a bean property, parameter etc. Types are managed per generated source file. Each type corresponds to a TypeMirror, i.e. there are different instances for e.g. Set<String> and Set<Integer>.

    Allows for a unified handling of declared and primitive types and usage within templates. Instances are obtained through TypeFactory.

    Author:
    Gunnar Morling, Filip Hrisafov
    • Method Detail

      • getTypeMirror

        public TypeMirror getTypeMirror()
      • getPackageName

        public String getPackageName()
      • getName

        public String getName()
      • createReferenceName

        public String createReferenceName()
        Returns a String that could be used in generated code to reference to this Type.

        The first time a name is referred-to it will be marked as to be imported. For instance LocalDateTime can be one of java.time.LocalDateTime and org.joda.LocalDateTime)

        If the java.time variant is referred to first, the java.time.LocalDateTime will be imported and the org.joda variant will be referred to with its FQN.

        If the type is nested and its top level type is to be imported then the name including its top level type will be returned.

        Returns:
        Just the name if this Type will be imported, the name up to the top level Type (if the top level type is important, otherwise the fully-qualified name.
      • getTypeParameters

        public List<Type> getTypeParameters()
      • getComponentType

        public Type getComponentType()
      • isPrimitive

        public boolean isPrimitive()
      • isInterface

        public boolean isInterface()
      • isEnumType

        public boolean isEnumType()
      • isVoid

        public boolean isVoid()
      • isAbstract

        public boolean isAbstract()
      • isString

        public boolean isString()
      • getEnumConstants

        public List<String> getEnumConstants()
        Returns:
        this type's enum constants in case it is an enum, an empty list otherwise.
      • getImplementationType

        public Type getImplementationType()
        Returns the implementation type to be instantiated in case this type is an interface iterable, collection or map type. The type will have the correct type arguments, so if this type e.g. represents Set<String>, the implementation type is HashSet<String>.
        Returns:
        The implementation type to be instantiated in case this type is an interface iterable, collection or map type, null otherwise.
      • isIterableType

        public boolean isIterableType()
        Whether this type is a sub-type of Iterable or an array type.
        Returns:
        true if this type is a sub-type of Iterable or an array type, false otherwise.
      • isIterableOrStreamType

        public boolean isIterableOrStreamType()
        Whether this type is a sub-type ofIterable, Stream or an array type
        Returns:
        true if this type is a sub-type ofIterable, Stream or an array type, false otherwise
      • isCollectionType

        public boolean isCollectionType()
      • isMapType

        public boolean isMapType()
      • isCollectionOrMapType

        public boolean isCollectionOrMapType()
      • isArrayType

        public boolean isArrayType()
      • isTypeVar

        public boolean isTypeVar()
      • isJavaLangType

        public boolean isJavaLangType()
      • isRecord

        public boolean isRecord()
      • isStreamType

        public boolean isStreamType()
        Whether this type is a sub-type of Stream.
        Returns:
        true it this type is a sub-type of Stream, false otherwise
      • hasSuperBound

        public boolean hasSuperBound()
        A wild card type can have two types of bounds (mutual exclusive): extends and super.
        Returns:
        true if the bound has a wild card super bound (e.g. ? super Number)
      • hasExtendsBound

        public boolean hasExtendsBound()
        A wild card type can have two types of bounds (mutual exclusive): extends and super.
        Returns:
        true if the bound has a wild card super bound (e.g. ? extends Number)
      • hasLowerBound

        public boolean hasLowerBound()
        A type variable type can have two types of bounds (mutual exclusive): lower and upper. Note that its use is only permitted on a definition (not on the place where its used). For instance: <T super Number> T map( T in)
        Returns:
        true if the bound has a type variable lower bound (e.g. T super Number)
      • hasUpperBound

        public boolean hasUpperBound()
        A type variable type can have two types of bounds (mutual exclusive): lower and upper. Note that its use is only permitted on a definition (not on the place where its used). For instance: ><T extends Number> T map( T in)
        Returns:
        true if the bound has a type variable upper bound (e.g. T extends Number)
      • getFullyQualifiedName

        public String getFullyQualifiedName()
      • getImportName

        public String getImportName()
        Returns:
        The name of this type as to be used within import statements.
      • getImportTypes

        public Set<Type> getImportTypes()
        Description copied from class: ModelElement
        Returns a set containing those Types referenced by this model element for which an import statement needs to be declared.
        Specified by:
        getImportTypes in class ModelElement
        Returns:
        A set with type referenced by this model element. Must not be null.
      • isTopLevelTypeToBeImported

        protected boolean isTopLevelTypeToBeImported()
      • isToBeImported

        public boolean isToBeImported()
        Whether this type is to be imported by means of an import statement in the currently generated source file (it can be referenced in the generated source using its simple name) or not (referenced using the FQN).
        Returns:
        true if the type is imported, false otherwise.
      • erasure

        public Type erasure()
      • withoutBounds

        public Type withoutBounds()
      • isAssignableTo

        public boolean isAssignableTo​(Type other)
        Whether this type is assignable to the given other type, considering the "extends / upper bounds" as well.
        Parameters:
        other - The other type.
        Returns:
        true if and only if this type is assignable to the given other type.
      • isRawAssignableTo

        public boolean isRawAssignableTo​(Type other)
        Whether this type is raw assignable to the given other type. We can't make a verdict on typevars, they need to be resolved first.
        Parameters:
        other - The other type.
        Returns:
        true if and only if this type is assignable to the given other type.
      • asRawType

        public Type asRawType()
        removes any bounds from this type.
        Returns:
        the raw type
      • getPropertyReadAccessors

        public Map<String,​ReadAccessor> getPropertyReadAccessors()
        getPropertyReadAccessors
        Returns:
        an unmodifiable map of all read accessors (including 'is' for booleans), indexed by property name
      • getPropertyPresenceCheckers

        public Map<String,​PresenceCheckAccessor> getPropertyPresenceCheckers()
        getPropertyPresenceCheckers
        Returns:
        an unmodifiable map of all presence checkers, indexed by property name
      • getPropertyWriteAccessors

        public Map<String,​Accessor> getPropertyWriteAccessors​(org.mapstruct.ap.internal.gem.CollectionMappingStrategyGem cmStrategy)
        getPropertyWriteAccessors returns a map of the write accessors according to the CollectionMappingStrategy. These accessors include:
        • setters, the obvious candidate :-), getSetters()
        • readAccessors, for collections that do not have a setter, e.g. for JAXB generated collection attributes getPropertyReadAccessors()
        • adders, typically for from table generated entities, getAdders()
        Parameters:
        cmStrategy - collection mapping strategy
        Returns:
        an unmodifiable map of all write accessors indexed by property name
      • getRecordComponents

        public List<Element> getRecordComponents()
      • distanceTo

        public int distanceTo​(Type assignableOther)
        Returns the length of the shortest path in the type hierarchy between this type and the specified other type. Returns -1 if this type is not assignable to the other type. Returns 0 if this type is equal to the other type. Returns 1, if the other type is a direct super type of this type, and so on.
        Parameters:
        assignableOther - the other type
        Returns:
        the length of the shortest path in the type hierarchy between this type and the specified other type
      • canAccess

        public boolean canAccess​(Type type,
                                 ExecutableElement method)
        Parameters:
        type - the type declaring the method
        method - the method to check
        Returns:
        Whether this type can access the given method declared on the given type.
      • getNull

        public String getNull()
        Returns:
        A valid Java expression most suitable for representing null - useful for dealing with primitives from FTL.
      • getSensibleDefault

        public String getSensibleDefault()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • describe

        public String describe()
        Returns:
        a string representation of the type for use in messages
      • getIdentification

        public String getIdentification()
        Returns:
        an identification that can be used as part in a forged method name.
      • getTypeBound

        public Type getTypeBound()
        Establishes the type bound:
        1. <? extends Number>, returns Number
        2. <? super Number>, returns Number
        3. <?>, returns Object
        4. <T extends Number>, returns Number
        Returns:
        the bound for this parameter
      • hasAccessibleConstructor

        public boolean hasAccessibleConstructor()
      • getDirectSuperTypes

        public List<Type> getDirectSuperTypes()
        Returns the direct supertypes of a type. The interface types, if any, will appear last in the list.
        Returns:
        the direct supertypes, or an empty list if none
      • determineTypeArguments

        public List<Type> determineTypeArguments​(Class<?> superclass)
        Searches for the given superclass and collects all type arguments for the given class
        Parameters:
        superclass - the superclass or interface the generic type arguments are searched for
        Returns:
        a list of type arguments or null, if superclass was not found
      • isNative

        public boolean isNative()
        All primitive types and their corresponding boxed types are considered native.
        Returns:
        true when native.
      • isLiteral

        public boolean isLiteral()
      • resolveParameterToType

        public Type.ResolvedPair resolveParameterToType​(Type declared,
                                                        Type parameterized)
        Steps through the declaredType in order to find a match for this typeVar Type. It aligns with the provided parameterized type where this typeVar type is used. For example: this: T declaredType: JAXBElement<String> parameterizedType: JAXBElement<T> result: String this: T, T[] or ? extends T, declaredType: E.g. Callable<? extends T> parameterizedType: Callable<BigDecimal> return: BigDecimal
        Parameters:
        declared - the type
        parameterized - the parameterized type
        Returns:
        - the same type when this is not a type var in the broadest sense (T, T[], or ? extends T) - the matching parameter in the parameterized type when this is a type var when found - null in all other cases
      • isWildCardBoundByTypeVar

        public boolean isWildCardBoundByTypeVar()
      • isArrayTypeVar

        public boolean isArrayTypeVar()
      • getBoxedEquivalent

        public Type getBoxedEquivalent()
        Gets the boxed equivalent type if the type is primitive, int will return Integer
        Returns:
        boxed equivalent
      • isEnumSet

        public boolean isEnumSet()