Class Type
- java.lang.Object
-
- org.mapstruct.ap.internal.writer.FreeMarkerWritable
-
- org.mapstruct.ap.internal.model.common.ModelElement
-
- org.mapstruct.ap.internal.model.common.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 aTypeMirror
, i.e. there are different instances for e.g.Set<String>
andSet<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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Type.ResolvedPair
Reflects any Resolved Pair, examples are T, String ?-
Nested classes/interfaces inherited from interface org.mapstruct.ap.internal.writer.Writable
Writable.Context
-
-
Constructor Summary
Constructors Constructor Description Type(TypeUtils typeUtils, ElementUtils elementUtils, TypeFactory typeFactory, AccessorNamingUtils accessorNaming, TypeMirror typeMirror, TypeElement typeElement, List<Type> typeParameters, ImplementationType implementationType, Type componentType, String packageName, String name, String qualifiedName, boolean isInterface, boolean isEnumType, boolean isIterableType, boolean isCollectionType, boolean isMapType, boolean isStreamType, Map<String,String> toBeImportedTypes, Map<String,String> notToBeImportedTypes, Boolean isToBeImported, boolean isLiteral, boolean loggingVerbose)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Type
asRawType()
removes any bounds from this type.boolean
canAccess(Type type, ExecutableElement method)
int
compareTo(Type o)
String
createReferenceName()
Returns a String that could be used in generated code to reference to thisType
.String
describe()
List<Type>
determineTypeArguments(Class<?> superclass)
Searches for the given superclass and collects all type arguments for the given classint
distanceTo(Type assignableOther)
Returns the length of the shortest path in the type hierarchy between this type and the specified other type.boolean
equals(Object obj)
Type
erasure()
Type
getBoxedEquivalent()
Gets the boxed equivalent type if the type is primitive, int will return IntegerType
getComponentType()
List<Type>
getDirectSuperTypes()
Returns the direct supertypes of a type.List<String>
getEnumConstants()
String
getFullyQualifiedName()
String
getIdentification()
ImplementationType
getImplementation()
Type
getImplementationType()
Returns the implementation type to be instantiated in case this type is an interface iterable, collection or map type.String
getImportName()
Set<Type>
getImportTypes()
Returns a set containing thoseType
s referenced by this model element for which an import statement needs to be declared.String
getName()
String
getNull()
String
getPackageName()
List<? extends TypeMirror>
getPermittedSubclasses()
return the list of permitted TypeMirrors for the java 17+ sealed classPresenceCheckAccessor
getPresenceChecker(String propertyName)
Map<String,PresenceCheckAccessor>
getPropertyPresenceCheckers()
getPropertyPresenceCheckersMap<String,ReadAccessor>
getPropertyReadAccessors()
getPropertyReadAccessorsMap<String,Accessor>
getPropertyWriteAccessors(org.mapstruct.ap.internal.gem.CollectionMappingStrategyGem cmStrategy)
getPropertyWriteAccessors returns a map of the write accessors according to the CollectionMappingStrategy.ReadAccessor
getReadAccessor(String propertyName, boolean allowedMapToBean)
List<Element>
getRecordComponents()
String
getSensibleDefault()
Type
getTypeBound()
Establishes the type bound:<? extends Number>
, returns Number<? super Number>
, returns Number<?>
, returns Object<T extends Number>, returns Number
List<Type>
getTypeBounds()
TypeElement
getTypeElement()
TypeMirror
getTypeMirror()
List<Type>
getTypeParameters()
boolean
hasAccessibleConstructor()
boolean
hasExtendsBound()
A wild card type can have two types of bounds (mutual exclusive): extends and super.int
hashCode()
boolean
hasLowerBound()
A type variable type can have two types of bounds (mutual exclusive): lower and upper.boolean
hasSuperBound()
A wild card type can have two types of bounds (mutual exclusive): extends and super.boolean
hasUpperBound()
A type variable type can have two types of bounds (mutual exclusive): lower and upper.boolean
isAbstract()
boolean
isArrayType()
boolean
isArrayTypeVar()
boolean
isAssignableTo(Type other)
Whether this type is assignable to the given other type, considering the "extends / upper bounds" as well.boolean
isCollectionOrMapType()
boolean
isCollectionType()
boolean
isEnumSet()
boolean
isEnumType()
boolean
isInterface()
boolean
isIntersection()
boolean
isIterableOrStreamType()
boolean
isIterableType()
Whether this type is a sub-type ofIterable
or an array type.boolean
isJavaLangType()
boolean
isLiteral()
boolean
isMapType()
boolean
isNative()
All primitive types and their corresponding boxed types are considered native.boolean
isPrimitive()
boolean
isRawAssignableTo(Type other)
Whether this type is raw assignable to the given other type.boolean
isRecord()
boolean
isSealed()
return true if this type is a java 17+ sealed classboolean
isStreamType()
Whether this type is a sub-type ofStream
.boolean
isString()
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).protected boolean
isTopLevelTypeToBeImported()
boolean
isTypeVar()
boolean
isVoid()
boolean
isWildCardBoundByTypeVar()
Type
resolveGenericTypeParameters(Type declared, Type parameterized)
Resolves generic types using the declared and parameterized types as input.
For example:Type.ResolvedPair
resolveParameterToType(Type declared, Type parameterized)
Steps through the declaredType in order to find a match for this typeVar Type.String
toString()
Type
withoutBounds()
-
Methods inherited from class org.mapstruct.ap.internal.writer.FreeMarkerWritable
getTemplateName, getTemplateNameForClass, write
-
-
-
-
Constructor Detail
-
Type
public Type(TypeUtils typeUtils, ElementUtils elementUtils, TypeFactory typeFactory, AccessorNamingUtils accessorNaming, TypeMirror typeMirror, TypeElement typeElement, List<Type> typeParameters, ImplementationType implementationType, Type componentType, String packageName, String name, String qualifiedName, boolean isInterface, boolean isEnumType, boolean isIterableType, boolean isCollectionType, boolean isMapType, boolean isStreamType, Map<String,String> toBeImportedTypes, Map<String,String> notToBeImportedTypes, Boolean isToBeImported, boolean isLiteral, boolean loggingVerbose)
-
-
Method Detail
-
getTypeMirror
public TypeMirror getTypeMirror()
-
getTypeElement
public TypeElement getTypeElement()
-
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 thisType
.
The first time a name is referred-to it will be marked as to be imported. For instance
LocalDateTime
can be one ofjava.time.LocalDateTime
andorg.joda.LocalDateTime
)If the
java.time
variant is referred to first, thejava.time.LocalDateTime
will be imported and theorg.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.
-
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. representsSet<String>
, the implementation type isHashSet<String>
.- Returns:
- The implementation type to be instantiated in case this type is an interface iterable, collection or map
type,
null
otherwise.
-
getImplementation
public ImplementationType getImplementation()
-
isIterableType
public boolean isIterableType()
Whether this type is a sub-type ofIterable
or an array type.- Returns:
true
if this type is a sub-type ofIterable
or an array type,false
otherwise.
-
isIterableOrStreamType
public boolean isIterableOrStreamType()
-
isCollectionType
public boolean isCollectionType()
-
isMapType
public boolean isMapType()
-
isCollectionOrMapType
public boolean isCollectionOrMapType()
-
isArrayType
public boolean isArrayType()
-
isTypeVar
public boolean isTypeVar()
-
isIntersection
public boolean isIntersection()
-
isJavaLangType
public boolean isJavaLangType()
-
isRecord
public boolean isRecord()
-
isStreamType
public boolean isStreamType()
Whether this type is a sub-type ofStream
.- Returns:
true
it this type is a sub-type ofStream
,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 thoseType
s referenced by this model element for which an import statement needs to be declared.- Specified by:
getImportTypes
in classModelElement
- 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
-
getReadAccessor
public ReadAccessor getReadAccessor(String propertyName, boolean allowedMapToBean)
-
getPresenceChecker
public PresenceCheckAccessor getPresenceChecker(String propertyName)
-
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
- setters, the obvious candidate :-),
-
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. Returns0
if this type is equal to the other type. Returns1
, 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 methodmethod
- 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()
-
compareTo
public int compareTo(Type o)
- Specified by:
compareTo
in interfaceComparable<Type>
-
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:<? extends Number>
, returns Number<? super Number>
, returns Number<?>
, returns Object<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 typeparameterized
- 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
-
resolveGenericTypeParameters
public Type resolveGenericTypeParameters(Type declared, Type parameterized)
Resolves generic types using the declared and parameterized types as input.
For example:this: T declaredType: JAXBElement<T> parameterizedType: JAXBElement<Integer> result: Integer this: List<T> declaredType: JAXBElement<T> parameterizedType: JAXBElement<Integer> result: List<Integer> this: List<? extends T> declaredType: JAXBElement<? extends T> parameterizedType: JAXBElement<BigDecimal> result: List<BigDecimal> this: List<Optional<T>> declaredType: JAXBElement<T> parameterizedType: JAXBElement<BigDecimal> result: List<Optional<BigDecimal>>
For example:this: Map<K, V> declaredType: JAXBElement<K> parameterizedType: JAXBElement<BigDecimal> result: Map<BigDecimal, V>
For example when reversing Key/Value for a Map:this: Map<KEY, VALUE> declaredType: HashMap<VALUE, KEY> parameterizedType: HashMap<BigDecimal, String> result: Map<String, BigDecimal>
this: T declaredType: JAXBElement<Y> parameterizedType: JAXBElement<Integer> result: null this: List<T> declaredType: JAXBElement<Y> parameterizedType: JAXBElement<Integer> result: List<T>
- Parameters:
declared
- the typeparameterized
- the parameterized type- Returns:
- - the result of
resolveParameterToType(Type, Type)
when this type itself is a type var.
- the type but then with the matching type parameters replaced.
- the same type when this type does not contain matching type parameters.
-
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()
-
isSealed
public boolean isSealed()
return true if this type is a java 17+ sealed class
-
getPermittedSubclasses
public List<? extends TypeMirror> getPermittedSubclasses()
return the list of permitted TypeMirrors for the java 17+ sealed class
-
-