|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.scijava.util.ConversionUtils
public class ConversionUtils
Useful methods for converting and casting between classes and types.
Method Summary | ||
---|---|---|
static boolean |
canCast(Class<?> src,
Class<?> dest)
Checks whether objects of the given class can be cast to the specified type. |
|
static boolean |
canCast(Object src,
Class<?> dest)
Checks whether the given object can be cast to the specified type. |
|
static boolean |
canConvert(Class<?> src,
Class<?> dest)
Checks whether objects of the given class can be converted to the specified type. |
|
static boolean |
canConvert(Class<?> src,
Type dest)
Checks whether objects of the given class can be converted to the specified type. |
|
static boolean |
canConvert(Object src,
Class<?> dest)
Checks whether the given object's type can be converted to the specified type. |
|
static boolean |
canConvert(Object src,
Type dest)
Checks whether the given object's type can be converted to the specified type. |
|
static
|
cast(Object src,
Class<T> dest)
Casts the given object to the specified type, or null if the types are incompatible. |
|
static
|
convert(Object src,
Class<T> dest)
Converts the given object to an object of the specified type. |
|
static Object |
convert(Object src,
Type dest)
As convert(Object, Class) but capable of creating and populating
multi-element objects (Collection s and array types). |
|
static
|
convertToEnum(String src,
Class<T> dest)
Converts the given string value to an enumeration constant of the specified type. |
|
static Class<?> |
getClass(Type type)
Gets the raw class corresponding to the given type. |
|
static Class<?> |
getComponentClass(Type type)
Gets the component type of the given array type, or null if not an array. |
|
static
|
getNonprimitiveType(Class<T> type)
Returns the non-primitive Class closest to the given type. |
|
static
|
getNullValue(Class<T> type)
Gets the "null" value for the given type. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static Object convert(Object src, Type dest)
convert(Object, Class)
but capable of creating and populating
multi-element objects (Collection
s and array types). If a single
element type is provided, it will be converted the same as
convert(Object, Class)
. If a multi-element type is detected, then
the value parameter will be interpreted as potential collection of values.
An appropriate container will be created, and the full set of values will
be type converted and added.
NB: This method should be capable of creating any array type, but if a
Collection
interface or abstract class is provided we can only make
a best guess as to what container type to instantiate. Defaults are
provided for Set
and List
subclasses.
src
- The object to convert.dest
- Type to which the object should be converted.public static <T> T convert(Object src, Class<T> dest)
String
, which uses the
Object.toString()
method instead). In the case of primitive types,
returns an object of the corresponding wrapped type. If the destination
type does not have an appropriate constructor, returns null.
T
- Type to which the object should be converted.src
- The object to convert.dest
- Type to which the object should be converted.public static <T> T convertToEnum(String src, Class<T> dest)
src
- The value to convert.dest
- The type of the enumeration constant.
public static boolean canConvert(Class<?> src, Type dest)
convert(Object, Type)
public static boolean canConvert(Class<?> src, Class<?> dest)
convert(Object, Class)
public static boolean canConvert(Object src, Type dest)
Note that this does not necessarily entail that
convert(Object, Type)
on that specific object will succeed. For
example: canConvert("5.1", int.class)
will return true
because a String
can in general be converted to an int
, but
calling convert("5.1", int.class)
will throw a
NumberFormatException
when the conversion is actually attempted
via the Integer.Integer(String)
constructor.
convert(Object, Type)
public static boolean canConvert(Object src, Class<?> dest)
Note that this does not necessarily entail that
convert(Object, Class)
on that specific object will succeed. For
example: canConvert("5.1", int.class)
will return true
because a String
can in general be converted to an int
, but
calling convert("5.1", int.class)
will throw a
NumberFormatException
when the conversion is actually attempted
via the Integer.Integer(String)
constructor.
convert(Object, Class)
public static <T> T cast(Object src, Class<T> dest)
public static boolean canCast(Class<?> src, Class<?> dest)
cast(Object, Class)
public static boolean canCast(Object src, Class<?> dest)
cast(Object, Class)
public static <T> Class<T> getNonprimitiveType(Class<T> type)
Class
closest to the given type.
Specifically, the following type conversions are done:
public static <T> T getNullValue(Class<T> type)
public static Class<?> getClass(Type type)
If the type is a Class
it is simply casted. In the case of a
ParameterizedType
, then ParameterizedType.getRawType()
is
returned. Otherwise, returns null.
public static Class<?> getComponentClass(Type type)
Class.getComponentType()
if type
is a Class
) and generic array types (i.e.,
GenericArrayType.getGenericComponentType()
if type
is a
GenericArrayType
).
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |