public final class TypeUtilities extends Object
Modifier and Type | Method and 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.
|
public static boolean isMethodInvocationConvertible(Class<?> sourceType, Class<?> targetType)
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.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)public static boolean isConvertibleWithoutLoss(Class<?> sourceType, Class<?> targetType)
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.sourceType
- the source typetargetType
- the target typepublic static boolean isSubtype(Class<?> subType, Class<?> superType)
subType
- the supposed subtypesuperType
- the supposed supertype of the subtypepublic static Class<?> getPrimitiveTypeByName(String name)
Integer.TYPE
.name
- the name of the primitive typepublic static Class<?> getPrimitiveType(Class<?> wrapperType)
Integer.class
will return Integer.TYPE
. If passed a
class that is not a wrapper for primitive type, returns null.wrapperType
- the class object representing a wrapper for a
primitive type.public static Class<?> getWrapperType(Class<?> primitiveType)
int.class
will return Integer.class
. If passed a class
that is not a primitive type, returns null.primitiveType
- the class object representing a primitive typepublic static boolean isWrapperType(Class<?> type)
type
- the examined type
Copyright © 2015, 2016, Oracle and/or its affiliates. All rights reserved.
DRAFT 9-internal+0-2016-02-27-124400.buildd.src