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
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classReflects any Resolved Pair, examples are T, String ?Nested classes/interfaces inherited from interface org.mapstruct.ap.internal.writer.Writable
Writable.Context -
Constructor Summary
ConstructorsConstructorDescriptionType(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
Modifier and TypeMethodDescriptionremoves any bounds from this type.booleancanAccess(Type type, ExecutableElement method) intReturns a String that could be used in generated code to reference to thisType.describe()determineTypeArguments(Class<?> superclass) Searches for the given superclass and collects all type arguments for the given classintdistanceTo(Type assignableOther) Returns the length of the shortest path in the type hierarchy between this type and the specified other type.booleanerasure()Gets the boxed equivalent type if the type is primitive, int will return IntegerReturns the direct supertypes of a type.Returns the implementation type to be instantiated in case this type is an interface iterable, collection or map type.Returns a set containing thoseTypes referenced by this model element for which an import statement needs to be declared.getName()getNull()List<? extends TypeMirror> return the list of permitted TypeMirrors for the java 17+ sealed classgetPresenceChecker(String propertyName) getPropertyPresenceCheckersgetPropertyReadAccessorsgetPropertyWriteAccessors(org.mapstruct.ap.internal.gem.CollectionMappingStrategyGem cmStrategy) getPropertyWriteAccessors returns a map of the write accessors according to the CollectionMappingStrategy.getReadAccessor(String propertyName, boolean allowedMapToBean) Establishes the type bound:<? extends Number>, returns Number<? super Number>, returns Number<?>, returns Object<T extends Number>, returns NumberbooleanbooleanA wild card type can have two types of bounds (mutual exclusive): extends and super.inthashCode()booleanA type variable type can have two types of bounds (mutual exclusive): lower and upper.booleanA wild card type can have two types of bounds (mutual exclusive): extends and super.booleanA type variable type can have two types of bounds (mutual exclusive): lower and upper.booleanbooleanbooleanbooleanisAssignableTo(Type other) Whether this type is assignable to the given other type, considering the "extends / upper bounds" as well.booleanbooleanbooleanbooleanbooleanbooleanbooleanbooleanWhether this type is a sub-type ofIterableor an array type.booleanbooleanbooleanbooleanisNative()All primitive types and their corresponding boxed types are considered native.booleanbooleanbooleanisRawAssignableTo(Type other) Whether this type is raw assignable to the given other type.booleanisRecord()booleanisSealed()return true if this type is a java 17+ sealed classbooleanWhether this type is a sub-type ofStream.booleanisString()booleanWhether 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 booleanbooleanbooleanisVoid()booleanresolveGenericTypeParameters(Type declared, Type parameterized) Resolves generic types using the declared and parameterized types as input.
For example:resolveParameterToType(Type declared, Type parameterized) Steps through the declaredType in order to find a match for this typeVar Type.toString()Methods inherited from class org.mapstruct.ap.internal.writer.FreeMarkerWritable
getTemplateName, getTemplateNameForClass, write
-
Constructor Details
-
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 Details
-
getTypeMirror
-
getTypeElement
-
getPackageName
-
getName
-
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
LocalDateTimecan be one ofjava.time.LocalDateTimeandorg.joda.LocalDateTime)If the
java.timevariant is referred to first, thejava.time.LocalDateTimewill be imported and theorg.jodavariant 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.
-
getTypeParameters
-
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
- Returns:
- this type's enum constants in case it is an enum, an empty list otherwise.
-
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,
nullotherwise.
-
getImplementation
-
isIterableType
public boolean isIterableType()Whether this type is a sub-type ofIterableor an array type.- Returns:
trueif this type is a sub-type ofIterableor an array type,falseotherwise.
-
isIterableOrStreamType
public boolean isIterableOrStreamType() -
isCollectionType
public boolean isCollectionType() -
isMapType
public boolean isMapType() -
isCollectionOrMapType
public boolean isCollectionOrMapType() -
isArrayType
public boolean isArrayType() -
isOptionalType
public boolean isOptionalType() -
getOptionalBaseType
-
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:
trueit 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
-
getImportName
- Returns:
- The name of this type as to be used within import statements.
-
getImportTypes
Description copied from class:ModelElementReturns a set containing thoseTypes referenced by this model element for which an import statement needs to be declared.- Specified by:
getImportTypesin 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:
trueif the type is imported,falseotherwise.
-
erasure
-
withoutBounds
-
isAssignableTo
Whether this type is assignable to the given other type, considering the "extends / upper bounds" as well.- Parameters:
other- The other type.- Returns:
trueif and only if this type is assignable to the given other type.
-
isRawAssignableTo
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:
trueif and only if this type is assignable to the given other type.
-
asRawType
removes any bounds from this type.- Returns:
- the raw type
-
getReadAccessor
-
getPresenceChecker
-
getPropertyReadAccessors
getPropertyReadAccessors- Returns:
- an unmodifiable map of all read accessors (including 'is' for booleans), indexed by property name
-
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 :-),
-
getRecordComponents
-
distanceTo
Returns the length of the shortest path in the type hierarchy between this type and the specified other type. Returns-1if this type is not assignable to the other type. Returns0if 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
- 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
- Returns:
- A valid Java expression most suitable for representing null - useful for dealing with primitives from FTL.
-
getSensibleDefault
-
hashCode
public int hashCode() -
equals
-
compareTo
- Specified by:
compareToin interfaceComparable<Type>
-
toString
-
describe
- Returns:
- a string representation of the type for use in messages
-
getIdentification
- Returns:
- an identification that can be used as part in a forged method name.
-
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
-
getTypeBounds
-
hasAccessibleConstructor
public boolean hasAccessibleConstructor() -
getKotlinMetadata
-
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
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
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
Resolves generic types using the declared and parameterized types as input.
For example:
It also works for partial matching.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:
It also works with multiple parameters at both sides.this: Map<K, V> declaredType: JAXBElement<K> parameterizedType: JAXBElement<BigDecimal> result: Map<BigDecimal, V>
For example when reversing Key/Value for a Map:
Mismatch result examples: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
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
return the list of permitted TypeMirrors for the java 17+ sealed class
-