APIdia Launch Partner
Top Description Fields Constructors Methods
jdk.dynalink.linker.support

public final Class TypeUtilities

extends Object
Class Inheritance
Imports
java.util.Collection, .Collections, .HashMap, .IdentityHashMap, .Map, jdk.dynalink.DynamicLinkerFactory, jdk.dynalink.linker.MethodTypeConversionStrategy

Various static utility methods for working with Java types.
Since
9

Field Summary

Modifier and TypeField and Description
private static final Map<Class<?>, Class<?>>
private static final Map<String, Class<?>>
private static final Map<Class<?>, Class<?>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Map<String, Class<?>>
private static Map<Class<?>, Class<?>>
public static Class<?>

Returns:

the class object representing the primitive type, or null if the passed class is not a primitive wrapper.
getPrimitiveType
(final Class<?>
the class object representing a wrapper for a primitive type.
wrapperType
)

When passed a class representing a wrapper for a primitive type, returns the class representing the corresponding primitive type.

public static Class<?>

Returns:

the class representing the primitive type, or null if the name does not correspond to a primitive type.
getPrimitiveTypeByName
(final String
the name of the primitive type
name
)

Given a name of a primitive type returns the class representing it.

public static Class<?>

Returns:

the class object representing the wrapper type, or null if the passed class is not a primitive.
getWrapperType
(final Class<?>
the class object representing a primitive type
primitiveType
)

When passed a class representing a primitive type, returns the class representing the corresponding wrapper type.

private static <K, V> Map<V, K>
invertMap(final Map<K, V> map)

private static boolean
isBoxingAndWideningReferenceConversion(final Class<?> sourceType, final Class<?> targetType)

public static boolean

Returns:

true if lossless conversion is possible
isConvertibleWithoutLoss
(final Class<?>
the source type
sourceType
,
final Class<?>
the target type
targetType
)

Determines whether a type can be converted to another without losing any precision.

public static boolean

Returns:

true if source type is method invocation convertible to target type.
isMethodInvocationConvertible
(final Class<?>
the type being converted from (call site type for parameter types, method type for return types)
sourceType
,
final Class<?>
the parameter type being converted to (method type for parameter types, call site type for return types)
targetType
)

Determines whether one type can be converted to another type using a method invocation conversion, as per JLS 5.3 "Method Invocation Conversion".

private static boolean

Returns:

true if subType is a proper (not identical to) primitive subtype of the superType that can be represented by the supertype without no precision loss.
isProperPrimitiveLosslessSubtype
(final Class<?>
the supposed subtype
subType
,
final Class<?>
the supposed supertype
superType
)

Similar to isProperPrimitiveSubtype(Class, Class), except it disallows conversions from int and long to float, and from long to double, as those can lose precision.

private static boolean

Returns:

true if subType is a proper (not identical to) primitive subtype of the superType
isProperPrimitiveSubtype
(final Class<?>
the supposed subtype
subType
,
final Class<?>
the supposed supertype
superType
)

Returns true if a supposed primitive subtype is a proper subtype ( meaning, subtype and not identical) of the supposed primitive supertype

public static boolean

Returns:

true if subType can be converted by identity conversion, widening primitive conversion, or widening reference conversion to superType.
isSubtype
(final Class<?>
the supposed subtype
subType
,
final Class<?>
the supposed supertype of the subtype
superType
)

Determines whether one type is a subtype of another type, as per JLS 4.10 "Subtyping".

public static boolean

Returns:

true if the passed type is a wrapper for a primitive type.
isWrapperType
(final Class<?>
the examined type
type
)

Returns true if the passed type is a wrapper for a primitive type.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

PRIMITIVE_TYPESback to summary
private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES
PRIMITIVE_TYPES_BY_NAMEback to summary
private static final Map<String, Class<?>> PRIMITIVE_TYPES_BY_NAME
WRAPPER_TYPESback to summary
private static final Map<Class<?>, Class<?>> WRAPPER_TYPES

Constructor Detail

TypeUtilitiesback to summary
private TypeUtilities()

Method Detail

createClassNameMappingback to summary
private static Map<String, Class<?>> createClassNameMapping(final Collection<Class<?>> classes)
createWrapperTypesback to summary
private static Map<Class<?>, Class<?>> createWrapperTypes()
getPrimitiveTypeback to summary
public static Class<?> getPrimitiveType(final Class<?> wrapperType)

When passed a class representing a wrapper for a primitive type, returns the class representing the corresponding primitive type. I.e. calling it with Integer.class will return Integer.TYPE. If passed a class that is not a wrapper for primitive type, returns null.

Parameters
wrapperType:Class<?>

the class object representing a wrapper for a primitive type.

Returns:Class<?>

the class object representing the primitive type, or null if the passed class is not a primitive wrapper.

getPrimitiveTypeByNameback to summary
public static Class<?> getPrimitiveTypeByName(final String name)

Given a name of a primitive type returns the class representing it. I.e. when invoked with "int", returns Integer#TYPE.

Parameters
name:String

the name of the primitive type

Returns:Class<?>

the class representing the primitive type, or null if the name does not correspond to a primitive type.

getWrapperTypeback to summary
public static Class<?> getWrapperType(final Class<?> primitiveType)

When passed a class representing a primitive type, returns the class representing the corresponding wrapper type. I.e. calling it with int.class will return Integer.class. If passed a class that is not a primitive type, returns null.

Parameters
primitiveType:Class<?>

the class object representing a primitive type

Returns:Class<?>

the class object representing the wrapper type, or null if the passed class is not a primitive.

invertMapback to summary
private static <K, V> Map<V, K> invertMap(final Map<K, V> map)
isBoxingAndWideningReferenceConversionback to summary
private static boolean isBoxingAndWideningReferenceConversion(final Class<?> sourceType, final Class<?> targetType)
isConvertibleWithoutLossback to summary
public static boolean isConvertibleWithoutLoss(final Class<?> sourceType, final Class<?> targetType)

Determines whether a type can be converted to another without losing any precision. As a special case, void is considered convertible only to void and Object (either as null or as a custom value set in DynamicLinkerFactory#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:Class<?>

the source type

targetType:Class<?>

the target type

Returns:boolean

true if lossless conversion is possible

isMethodInvocationConvertibleback to summary
public static boolean isMethodInvocationConvertible(final Class<?> sourceType, final 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". This is basically all conversions allowed by subtyping (see isSubtype(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:Class<?>

the type being converted from (call site type for parameter types, method type for return types)

targetType:Class<?>

the parameter type being converted to (method type for parameter types, call site type for return types)

Returns:boolean

true if source type is method invocation convertible to target type.

isProperPrimitiveLosslessSubtypeback to summary
private static boolean isProperPrimitiveLosslessSubtype(final Class<?> subType, final Class<?> superType)

Similar to isProperPrimitiveSubtype(Class, Class), except it disallows conversions from int and long to float, and from long to double, as those can lose precision. It also disallows conversion from and to char and anything else (similar to boolean) as char is not meant to be an arithmetic type.

Parameters
subType:Class<?>

the supposed subtype

superType:Class<?>

the supposed supertype

Returns:boolean

true if subType is a proper (not identical to) primitive subtype of the superType that can be represented by the supertype without no precision loss.

isProperPrimitiveSubtypeback to summary
private static boolean isProperPrimitiveSubtype(final Class<?> subType, final Class<?> superType)

Returns true if a supposed primitive subtype is a proper subtype ( meaning, subtype and not identical) of the supposed primitive supertype

Parameters
subType:Class<?>

the supposed subtype

superType:Class<?>

the supposed supertype

Returns:boolean

true if subType is a proper (not identical to) primitive subtype of the superType

isSubtypeback to summary
public static boolean isSubtype(final Class<?> subType, final Class<?> superType)

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:Class<?>

the supposed subtype

superType:Class<?>

the supposed supertype of the subtype

Returns:boolean

true if subType can be converted by identity conversion, widening primitive conversion, or widening reference conversion to superType.

isWrapperTypeback to summary
public static boolean isWrapperType(final Class<?> type)

Returns true if the passed type is a wrapper for a primitive type.

Parameters
type:Class<?>

the examined type

Returns:boolean

true if the passed type is a wrapper for a primitive type.