java.lang.Object
jdk.dynalink.linker.support.TypeUtilities
public final class TypeUtilities extends Object
Various static utility methods for working with Java types.
-
Method Summary
Modifier and Type Method Description static Class<?>
getPrimitiveType(Class<?> wrapperType)
When passed a class representing a wrapper for a primitive type, returns the class representing the corresponding primitive type.static Class<?>
getPrimitiveTypeByName(String name)
Given a name of a primitive type returns the class representing it.static Class<?>
getWrapperType(Class<?> primitiveType)
When passed a class representing a primitive type, returns the class representing the corresponding wrapper type.static boolean
isConvertibleWithoutLoss(Class<?> sourceType, Class<?> targetType)
Determines whether a type can be converted to another without losing any precision.static boolean
isMethodInvocationConvertible(Class<?> sourceType, Class<?> targetType)
Determines whether one type can be converted to another type using a method invocation conversion, as per JLS 5.3 "Method Invocation Conversion".static boolean
isSubtype(Class<?> subType, Class<?> superType)
Determines whether one type is a subtype of another type, as per JLS 4.10 "Subtyping".static boolean
isWrapperType(Class<?> type)
Returns true if the passed type is a wrapper for a primitive type.
-
Method Details
-
isMethodInvocationConvertible
Determines whether one type can be converted to another type using a method invocation conversion, as per JLS 5.3 "Method Invocation Conversion". This is basically all conversions allowed by subtyping (seeisSubtype(Class, Class)
) as well as boxing conversion (JLS 5.1.7) optionally followed by widening reference conversion, and unboxing conversion (JLS 5.1.8) optionally followed by widening primitive conversion.- Parameters:
sourceType
- the type being converted from (call site type for parameter types, method type for return types)targetType
- the parameter type being converted to (method type for parameter types, call site type for return types)- Returns:
- true if source type is method invocation convertible to target type.
-
isConvertibleWithoutLoss
Determines whether a type can be converted to another without losing any precision. As a special case, void is considered convertible only to void andObject
(either asnull
or as a custom value set inDynamicLinkerFactory.setAutoConversionStrategy(MethodTypeConversionStrategy)
). Somewhat unintuitively, we consider anything to be convertible to void even though converting to void causes the ultimate loss of data. On the other hand, conversion to void essentially means that the value is of no interest and should be discarded, thus there's no expectation of preserving any precision.- Parameters:
sourceType
- the source typetargetType
- the target type- Returns:
- true if lossless conversion is possible
-
isSubtype
Determines whether one type is a subtype of another type, as per JLS 4.10 "Subtyping". Note: this is not strict or proper subtype, therefore true is also returned for identical types; to be completely precise, it allows identity conversion (JLS 5.1.1), widening primitive conversion (JLS 5.1.2) and widening reference conversion (JLS 5.1.5).- Parameters:
subType
- the supposed subtypesuperType
- the supposed supertype of the subtype- Returns:
- true if subType can be converted by identity conversion, widening primitive conversion, or widening reference conversion to superType.
-
getPrimitiveTypeByName
Given a name of a primitive type returns the class representing it. I.e. when invoked with "int", returnsInteger.TYPE
.- Parameters:
name
- the name of the primitive type- Returns:
- the class representing the primitive type, or null if the name does not correspond to a primitive type.
-
getPrimitiveType
When passed a class representing a wrapper for a primitive type, returns the class representing the corresponding primitive type. I.e. calling it withInteger.class
will returnInteger.TYPE
. If passed a class that is not a wrapper for primitive type, returns null.- Parameters:
wrapperType
- the class object representing a wrapper for a primitive type.- Returns:
- the class object representing the primitive type, or null if the passed class is not a primitive wrapper.
-
getWrapperType
When passed a class representing a primitive type, returns the class representing the corresponding wrapper type. I.e. calling it withint.class
will returnInteger.class
. If passed a class that is not a primitive type, returns null.- Parameters:
primitiveType
- the class object representing a primitive type- Returns:
- the class object representing the wrapper type, or null if the passed class is not a primitive.
-
isWrapperType
Returns true if the passed type is a wrapper for a primitive type.- Parameters:
type
- the examined type- Returns:
- true if the passed type is a wrapper for a primitive type.
-