|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.scijava.util.ClassUtils
public final class ClassUtils
Useful methods for working with Class
objects and primitive types.
Method Summary | ||
---|---|---|
static boolean |
canCast(Class<?> c,
Class<?> type)
Deprecated. use ConversionUtils.canCast(Class, Class) |
|
static boolean |
canCast(Object obj,
Class<?> type)
Deprecated. use ConversionUtils.canCast(Object, Class) |
|
static boolean |
canConvert(Class<?> c,
Class<?> type)
Deprecated. use ConversionUtils.canConvert(Class, Class) |
|
static boolean |
canConvert(Object value,
Class<?> type)
Deprecated. use ConversionUtils.canConvert(Object, Class) |
|
static
|
cast(Object obj,
Class<T> type)
Deprecated. use ConversionUtils.cast(Object, Class) |
|
static int |
compare(Class<?> c1,
Class<?> c2)
Compares two Class objects using their fully qualified names. |
|
static
|
convert(Object value,
Class<T> type)
Deprecated. use ConversionUtils.convert(Object, Class) |
|
static
|
getAnnotatedFields(Class<?> c,
Class<A> annotationClass)
Gets the given class's Field s marked with the annotation of the
specified class. |
|
static
|
getAnnotatedFields(Class<?> c,
Class<A> annotationClass,
List<Field> fields)
Gets the given class's Field s marked with the annotation of the
specified class. |
|
static
|
getAnnotatedMethods(Class<?> c,
Class<A> annotationClass)
Gets the given class's Method s marked with the annotation of the
specified class. |
|
static
|
getAnnotatedMethods(Class<?> c,
Class<A> annotationClass,
List<Method> methods)
Gets the given class's Method s marked with the annotation of the
specified class. |
|
static Class<?> |
getArrayClass(Class<?> elementClass)
Gets the array class corresponding to the given element type. |
|
static Field |
getField(Class<?> c,
String fieldName)
Gets the specified field of the given class, or null if it does not exist. |
|
static Field |
getField(String className,
String fieldName)
Gets the specified field of the given class, or null if it does not exist. |
|
static Type |
getGenericType(Field field,
Class<?> type)
Deprecated. use GenericUtils.getFieldType(Field, Class) |
|
static URL |
getLocation(Class<?> c)
Gets the base location of the given class. |
|
static URL |
getLocation(String className)
Gets the base location of the given class. |
|
static URL |
getLocation(String className,
ClassLoader classLoader)
Gets the base location of the given class. |
|
static
|
getNonprimitiveType(Class<T> type)
Deprecated. use ConversionUtils.getNonprimitiveType(Class) |
|
static
|
getNullValue(Class<T> type)
Deprecated. use ConversionUtils.getNullValue(Class) |
|
static List<Class<?>> |
getTypes(Field field,
Class<?> type)
Deprecated. use GenericUtils.getFieldClasses(Field, Class) |
|
static Object |
getValue(Field field,
Object instance)
Gets the given field's value of the specified object instance, or null if the value cannot be obtained. |
|
static boolean |
hasClass(String className)
Checks whether a class with the given name exists. |
|
static boolean |
hasClass(String className,
ClassLoader classLoader)
Checks whether a class with the given name exists. |
|
static boolean |
isBoolean(Class<?> type)
|
|
static boolean |
isByte(Class<?> type)
|
|
static boolean |
isCharacter(Class<?> type)
|
|
static boolean |
isDouble(Class<?> type)
|
|
static boolean |
isFloat(Class<?> type)
|
|
static boolean |
isInteger(Class<?> type)
|
|
static boolean |
isLong(Class<?> type)
|
|
static boolean |
isNumber(Class<?> type)
|
|
static boolean |
isShort(Class<?> type)
|
|
static boolean |
isText(Class<?> type)
|
|
static Class<?> |
loadClass(String className)
Loads the class with the given name, using the current thread's context class loader, or null if it cannot be loaded. |
|
static Class<?> |
loadClass(String name,
ClassLoader classLoader)
Loads the class with the given name, using the specified ClassLoader , or null if it cannot be loaded. |
|
static void |
setValue(Field field,
Object instance,
Object value)
Sets the given field's value of the specified object instance. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static Class<?> loadClass(String className)
loadClass(String, ClassLoader)
public static Class<?> loadClass(String name, ClassLoader classLoader)
ClassLoader
, or null if it cannot be loaded.
This method is capable of parsing several different class name syntaxes. In particular, array classes (including primitives) represented using either square brackets or internal Java array name syntax are supported. Examples:
boolean
is loaded as boolean.class
Z
is loaded as boolean.class
double[]
is loaded as double[].class
string[]
is loaded as java.lang.String.class
[F
is loaded as float[].class
name
- The name of the class to load.classLoader
- The class loader with which to load the class; if null,
the current thread's context class loader will be used.public static Class<?> getArrayClass(Class<?> elementClass)
For example, getArrayClass(double.class)
returns
double[].class
.
public static boolean hasClass(String className)
public static boolean hasClass(String className, ClassLoader classLoader)
public static URL getLocation(String className)
If the class is directly on the file system (e.g., "/path/to/my/package/MyClass.class") then it will return the base directory (e.g., "/path/to").
If the class is within a JAR file (e.g., "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the path to the JAR (e.g., "/path/to/my-jar.jar").
className
- The name of the class whose location is desired.to convert the result to a {@link File}.
public static URL getLocation(String className, ClassLoader classLoader)
If the class is directly on the file system (e.g., "/path/to/my/package/MyClass.class") then it will return the base directory (e.g., "/path/to").
If the class is within a JAR file (e.g., "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the path to the JAR (e.g., "/path/to/my-jar.jar").
className
- The name of the class whose location is desired.classLoader
- The class loader to use when loading the class.to convert the result to a {@link File}.
public static URL getLocation(Class<?> c)
If the class is directly on the file system (e.g., "/path/to/my/package/MyClass.class") then it will return the base directory (e.g., "file:/path/to").
If the class is within a JAR file (e.g., "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the path to the JAR (e.g., "file:/path/to/my-jar.jar").
c
- The class whose location is desired.to convert the result to a {@link File}.
public static <A extends Annotation> List<Method> getAnnotatedMethods(Class<?> c, Class<A> annotationClass)
Method
s marked with the annotation of the
specified class.
Unlike Class.getMethods()
, the result will include any non-public
methods, including methods defined in supertypes of the given class.
c
- The class to scan for annotated methods.annotationClass
- The type of annotation for which to scan.
public static <A extends Annotation> void getAnnotatedMethods(Class<?> c, Class<A> annotationClass, List<Method> methods)
Method
s marked with the annotation of the
specified class.
Unlike Class.getMethods()
, the result will include any non-public
methods, including methods defined in supertypes of the given class.
c
- The class to scan for annotated methods.annotationClass
- The type of annotation for which to scan.methods
- The list to which matching methods will be added.public static <A extends Annotation> List<Field> getAnnotatedFields(Class<?> c, Class<A> annotationClass)
Field
s marked with the annotation of the
specified class.
Unlike Class.getFields()
, the result will include any non-public
fields, including fields defined in supertypes of the given class.
c
- The class to scan for annotated fields.annotationClass
- The type of annotation for which to scan.
public static <A extends Annotation> void getAnnotatedFields(Class<?> c, Class<A> annotationClass, List<Field> fields)
Field
s marked with the annotation of the
specified class.
Unlike Class.getFields()
, the result will include any non-public
fields, including fields defined in supertypes of the given class.
c
- The class to scan for annotated fields.annotationClass
- The type of annotation for which to scan.fields
- The list to which matching fields will be added.public static Field getField(String className, String fieldName)
public static Field getField(Class<?> c, String fieldName)
public static Object getValue(Field field, Object instance)
public static void setValue(Field field, Object instance, Object value)
IllegalArgumentException
- if the value cannot be set.public static boolean isBoolean(Class<?> type)
public static boolean isByte(Class<?> type)
public static boolean isCharacter(Class<?> type)
public static boolean isDouble(Class<?> type)
public static boolean isFloat(Class<?> type)
public static boolean isInteger(Class<?> type)
public static boolean isLong(Class<?> type)
public static boolean isShort(Class<?> type)
public static boolean isNumber(Class<?> type)
public static boolean isText(Class<?> type)
public static int compare(Class<?> c1, Class<?> c2)
Class
objects using their fully qualified names.
Note: this method provides a natural ordering that may be inconsistent with
equals. Specifically, two unequal classes may return 0 when compared in
this fashion if they represent the same class loaded using two different
ClassLoader
s. Hence, if this method is used as a basis for
implementing Comparable.compareTo(T)
or
Comparator.compare(T, T)
, that implementation may want to
impose logic beyond that of this method, for breaking ties, if a total
ordering consistent with equals is always required.
Priority.compare(org.scijava.Prioritized,
org.scijava.Prioritized)
@Deprecated public static <T> T convert(Object value, Class<T> type)
ConversionUtils.convert(Object, Class)
@Deprecated public static boolean canConvert(Class<?> c, Class<?> type)
ConversionUtils.canConvert(Class, Class)
@Deprecated public static boolean canConvert(Object value, Class<?> type)
ConversionUtils.canConvert(Object, Class)
@Deprecated public static <T> T cast(Object obj, Class<T> type)
ConversionUtils.cast(Object, Class)
@Deprecated public static boolean canCast(Class<?> c, Class<?> type)
ConversionUtils.canCast(Class, Class)
@Deprecated public static boolean canCast(Object obj, Class<?> type)
ConversionUtils.canCast(Object, Class)
@Deprecated public static <T> Class<T> getNonprimitiveType(Class<T> type)
ConversionUtils.getNonprimitiveType(Class)
@Deprecated public static <T> T getNullValue(Class<T> type)
ConversionUtils.getNullValue(Class)
@Deprecated public static List<Class<?>> getTypes(Field field, Class<?> type)
GenericUtils.getFieldClasses(Field, Class)
@Deprecated public static Type getGenericType(Field field, Class<?> type)
GenericUtils.getFieldType(Field, Class)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |