org.scijava.util
Class ClassUtils

java.lang.Object
  extended by org.scijava.util.ClassUtils

public final class ClassUtils
extends Object

Useful methods for working with Class objects and primitive types.

Author:
Curtis Rueden

Method Summary
static void cacheAnnotatedObjects(Class<?> scannedClass, Query query)
          This method scans the provided class, its superclasses and interfaces for all supported Annotation : AnnotatedObject pairs.
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
<T> T
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
<T> T
convert(Object value, Class<T> type)
          Deprecated. use ConversionUtils.convert(Object, Class)
static
<A extends Annotation>
List<Field>
getAnnotatedFields(Class<?> c, Class<A> annotationClass)
          Gets the given class's Fields marked with the annotation of the specified class.
static
<A extends Annotation>
void
getAnnotatedFields(Class<?> c, Class<A> annotationClass, List<Field> fields)
          Gets the given class's Fields marked with the annotation of the specified class.
static
<A extends Annotation>
List<Method>
getAnnotatedMethods(Class<?> c, Class<A> annotationClass)
          Gets the given class's Methods marked with the annotation of the specified class.
static
<A extends Annotation>
void
getAnnotatedMethods(Class<?> c, Class<A> annotationClass, List<Method> methods)
          Gets the given class's Methods 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
<T> Class<T>
getNonprimitiveType(Class<T> type)
          Deprecated. use ConversionUtils.getNonprimitiveType(Class)
static
<T> T
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

loadClass

public 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.

See Also:
loadClass(String, ClassLoader)

loadClass

public 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.

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:

Parameters:
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.

getArrayClass

public static Class<?> getArrayClass(Class<?> elementClass)
Gets the array class corresponding to the given element type.

For example, getArrayClass(double.class) returns double[].class.


hasClass

public static boolean hasClass(String className)
Checks whether a class with the given name exists.


hasClass

public static boolean hasClass(String className,
                               ClassLoader classLoader)
Checks whether a class with the given name exists.


getLocation

public static URL getLocation(String className)
Gets the base location of the given class.

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").

Parameters:
className - The name of the class whose location is desired.
See Also:
to convert the result to a {@link File}.

getLocation

public static URL getLocation(String className,
                              ClassLoader classLoader)
Gets the base location of the given class.

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").

Parameters:
className - The name of the class whose location is desired.
classLoader - The class loader to use when loading the class.
See Also:
to convert the result to a {@link File}.

getLocation

public static URL getLocation(Class<?> c)
Gets the base location of the given class.

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").

Parameters:
c - The class whose location is desired.
See Also:
to convert the result to a {@link File}.

getAnnotatedMethods

public static <A extends Annotation> List<Method> getAnnotatedMethods(Class<?> c,
                                                                      Class<A> annotationClass)
Gets the given class's Methods 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.

Parameters:
c - The class to scan for annotated methods.
annotationClass - The type of annotation for which to scan.
Returns:
A new list containing all methods with the requested annotation.

getAnnotatedMethods

public static <A extends Annotation> void getAnnotatedMethods(Class<?> c,
                                                              Class<A> annotationClass,
                                                              List<Method> methods)
Gets the given class's Methods 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.

Parameters:
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.

getAnnotatedFields

public static <A extends Annotation> List<Field> getAnnotatedFields(Class<?> c,
                                                                    Class<A> annotationClass)
Gets the given class's Fields 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.

Parameters:
c - The class to scan for annotated fields.
annotationClass - The type of annotation for which to scan.
Returns:
A new list containing all fields with the requested annotation.

getAnnotatedFields

public static <A extends Annotation> void getAnnotatedFields(Class<?> c,
                                                             Class<A> annotationClass,
                                                             List<Field> fields)
Gets the given class's Fields 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.

Parameters:
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.

cacheAnnotatedObjects

public static void cacheAnnotatedObjects(Class<?> scannedClass,
                                         Query query)
This method scans the provided class, its superclasses and interfaces for all supported Annotation : AnnotatedObject pairs. These are then cached to remove the need for future queries.

By combining multiple Annotation : AnnotatedObject pairs in one query, we can limit the number of times a class's superclass and interface hierarchy are traversed.

Parameters:
scannedClass - Class to scan
query - Pairs of Annotation and AnnotatedObjects to discover.

getField

public static Field getField(String className,
                             String fieldName)
Gets the specified field of the given class, or null if it does not exist.


getField

public static Field getField(Class<?> c,
                             String fieldName)
Gets the specified field of the given class, or null if it does not exist.


getValue

public 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.


setValue

public static void setValue(Field field,
                            Object instance,
                            Object value)
Sets the given field's value of the specified object instance.

Throws:
IllegalArgumentException - if the value cannot be set.

isBoolean

public static boolean isBoolean(Class<?> type)

isByte

public static boolean isByte(Class<?> type)

isCharacter

public static boolean isCharacter(Class<?> type)

isDouble

public static boolean isDouble(Class<?> type)

isFloat

public static boolean isFloat(Class<?> type)

isInteger

public static boolean isInteger(Class<?> type)

isLong

public static boolean isLong(Class<?> type)

isShort

public static boolean isShort(Class<?> type)

isNumber

public static boolean isNumber(Class<?> type)

isText

public static boolean isText(Class<?> type)

compare

public static int compare(Class<?> c1,
                          Class<?> c2)
Compares two 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 ClassLoaders. 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.

See Also:
Priority.compare(org.scijava.Prioritized, org.scijava.Prioritized)

convert

@Deprecated
public static <T> T convert(Object value,
                                       Class<T> type)
Deprecated. use ConversionUtils.convert(Object, Class)


canConvert

@Deprecated
public static boolean canConvert(Class<?> c,
                                            Class<?> type)
Deprecated. use ConversionUtils.canConvert(Class, Class)


canConvert

@Deprecated
public static boolean canConvert(Object value,
                                            Class<?> type)
Deprecated. use ConversionUtils.canConvert(Object, Class)


cast

@Deprecated
public static <T> T cast(Object obj,
                                    Class<T> type)
Deprecated. use ConversionUtils.cast(Object, Class)


canCast

@Deprecated
public static boolean canCast(Class<?> c,
                                         Class<?> type)
Deprecated. use ConversionUtils.canCast(Class, Class)


canCast

@Deprecated
public static boolean canCast(Object obj,
                                         Class<?> type)
Deprecated. use ConversionUtils.canCast(Object, Class)


getNonprimitiveType

@Deprecated
public static <T> Class<T> getNonprimitiveType(Class<T> type)
Deprecated. use ConversionUtils.getNonprimitiveType(Class)


getNullValue

@Deprecated
public static <T> T getNullValue(Class<T> type)
Deprecated. use ConversionUtils.getNullValue(Class)


getTypes

@Deprecated
public static List<Class<?>> getTypes(Field field,
                                                 Class<?> type)
Deprecated. use GenericUtils.getFieldClasses(Field, Class)


getGenericType

@Deprecated
public static Type getGenericType(Field field,
                                             Class<?> type)
Deprecated. use GenericUtils.getFieldType(Field, Class)



Copyright © 2009–2015 SciJava. All rights reserved.