Class ClassUtils
- java.lang.Object
-
- org.scijava.util.ClassUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidcacheAnnotatedObjects(Class<?> scannedClass, Query query)This method scans the provided class, its superclasses and interfaces for all supportedAnnotation:AnnotatedElementpairs.static booleancanCast(Class<?> c, Class<?> type)Deprecated.static booleancanCast(Object obj, Class<?> type)Deprecated.static booleancanConvert(Class<?> c, Class<?> type)Deprecated.static booleancanConvert(Object value, Class<?> type)Deprecated.static <T> Tcast(Object obj, Class<T> type)Deprecated.static intcompare(Class<?> c1, Class<?> c2)Compares twoClassobjects using their fully qualified names.static <T> Tconvert(Object value, Class<T> type)Deprecated.static <A extends Annotation>
List<Field>getAnnotatedFields(Class<?> c, Class<A> annotationClass)Gets the given class'sFields marked with the annotation of the specified class.static <A extends Annotation>
voidgetAnnotatedFields(Class<?> c, Class<A> annotationClass, List<Field> fields)Gets the given class'sFields marked with the annotation of the specified class.static <A extends Annotation>
List<Method>getAnnotatedMethods(Class<?> c, Class<A> annotationClass)Gets the given class'sMethods marked with the annotation of the specified class.static <A extends Annotation>
voidgetAnnotatedMethods(Class<?> c, Class<A> annotationClass, List<Method> methods)Gets the given class'sMethods marked with the annotation of the specified class.static Class<?>getArrayClass(Class<?> elementClass)Deprecated.UseTypes.array(Class)instead.static FieldgetField(Class<?> c, String fieldName)Deprecated.UseTypes.field(java.lang.Class<?>, java.lang.String)instead.static FieldgetField(String className, String fieldName)Deprecated.UseTypes.field(java.lang.Class<?>, java.lang.String)instead.static TypegetGenericType(Field field, Class<?> type)Deprecated.UseTypes.fieldType(Field, Class)instead.static URLgetLocation(Class<?> c)Deprecated.UseTypes.location(java.lang.Class<?>)andTypes.load(java.lang.String)instead.static URLgetLocation(String className)Deprecated.UseTypes.location(java.lang.Class<?>)andTypes.load(java.lang.String)instead.static URLgetLocation(String className, ClassLoader classLoader)Deprecated.UseTypes.location(java.lang.Class<?>)andTypes.load(java.lang.String)instead.static <T> Class<T>getNonprimitiveType(Class<T> type)Deprecated.useTypes.box(Class)static <T> TgetNullValue(Class<T> type)Deprecated.static List<Class<?>>getTypes(Field field, Class<?> type)Deprecated.UseTypes.fieldType(Field, Class)andTypes.raws(java.lang.reflect.Type)instead.static ObjectgetValue(Field field, Object instance)Gets the given field's value of the specified object instance, or null if the value cannot be obtained.static booleanhasClass(String className)Deprecated.UseTypes.load(String)instead.static booleanhasClass(String className, ClassLoader classLoader)Deprecated.UseTypes.load(String, ClassLoader)instead.static booleanisBoolean(Class<?> type)Deprecated.UseTypes.isBoolean(java.lang.Class<?>)instead.static booleanisByte(Class<?> type)Deprecated.UseTypes.isByte(java.lang.Class<?>)instead.static booleanisCharacter(Class<?> type)Deprecated.UseTypes.isCharacter(java.lang.Class<?>)instead.static booleanisDouble(Class<?> type)Deprecated.UseTypes.isDouble(java.lang.Class<?>)instead.static booleanisFloat(Class<?> type)Deprecated.UseTypes.isFloat(java.lang.Class<?>)instead.static booleanisInteger(Class<?> type)Deprecated.UseTypes.isInteger(java.lang.Class<?>)instead.static booleanisLong(Class<?> type)Deprecated.UseTypes.isLong(java.lang.Class<?>)instead.static booleanisNumber(Class<?> type)Deprecated.UseTypes.isNumber(java.lang.Class<?>)instead.static booleanisShort(Class<?> type)Deprecated.UseTypes.isShort(java.lang.Class<?>)instead.static booleanisText(Class<?> type)Deprecated.UseTypes.isText(java.lang.Class<?>)instead.static Class<?>loadClass(String name)Deprecated.UseTypes.load(String)instead.static Class<?>loadClass(String className, boolean quietly)Deprecated.UseTypes.load(String, boolean)instead.static Class<?>loadClass(String name, ClassLoader classLoader)Deprecated.UseTypes.load(String, ClassLoader)instead.static Class<?>loadClass(String name, ClassLoader classLoader, boolean quietly)Deprecated.UseTypes.load(String, ClassLoader, boolean)instead.static voidsetValue(Field field, Object instance, Object value)Sets the given field's value of the specified object instance.
-
-
-
Method Detail
-
getAnnotatedMethods
public static <A extends Annotation> List<Method> getAnnotatedMethods(Class<?> c, Class<A> annotationClass)
Gets the given class'sMethods 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 list containing all methods with the requested annotation. Note that for performance reasons, lists may be cached and reused, so it is best to make a copy of the result if you need to modify it.
-
getAnnotatedMethods
public static <A extends Annotation> void getAnnotatedMethods(Class<?> c, Class<A> annotationClass, List<Method> methods)
Gets the given class'sMethods 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'sFields 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 list containing all fields with the requested annotation. Note that for performance reasons, lists may be cached and reused, so it is best to make a copy of the result if you need to modify it.
-
getAnnotatedFields
public static <A extends Annotation> void getAnnotatedFields(Class<?> c, Class<A> annotationClass, List<Field> fields)
Gets the given class'sFields 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 supportedAnnotation:AnnotatedElementpairs. These are then cached to remove the need for future queries.By combining multiple
Annotation : AnnotatedElementpairs in one query, we can limit the number of times a class's superclass and interface hierarchy are traversed.- Parameters:
scannedClass- Class to scanquery- Pairs ofAnnotationandAnnotatedElements to discover.
-
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.
-
compare
public static int compare(Class<?> c1, Class<?> c2)
Compares twoClassobjects 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 implementingComparable.compareTo(T)orComparator.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.
-
loadClass
@Deprecated public static Class<?> loadClass(String name)
Deprecated.UseTypes.load(String)instead.
-
loadClass
@Deprecated public static Class<?> loadClass(String name, ClassLoader classLoader)
Deprecated.UseTypes.load(String, ClassLoader)instead.
-
loadClass
@Deprecated public static Class<?> loadClass(String className, boolean quietly)
Deprecated.UseTypes.load(String, boolean)instead.
-
loadClass
@Deprecated public static Class<?> loadClass(String name, ClassLoader classLoader, boolean quietly)
Deprecated.UseTypes.load(String, ClassLoader, boolean)instead.
-
hasClass
@Deprecated public static boolean hasClass(String className)
Deprecated.UseTypes.load(String)instead.
-
hasClass
@Deprecated public static boolean hasClass(String className, ClassLoader classLoader)
Deprecated.UseTypes.load(String, ClassLoader)instead.
-
getLocation
@Deprecated public static URL getLocation(String className)
Deprecated.UseTypes.location(java.lang.Class<?>)andTypes.load(java.lang.String)instead.
-
getLocation
@Deprecated public static URL getLocation(String className, ClassLoader classLoader)
Deprecated.UseTypes.location(java.lang.Class<?>)andTypes.load(java.lang.String)instead.
-
getLocation
@Deprecated public static URL getLocation(Class<?> c)
Deprecated.UseTypes.location(java.lang.Class<?>)andTypes.load(java.lang.String)instead.
-
isBoolean
@Deprecated public static boolean isBoolean(Class<?> type)
Deprecated.UseTypes.isBoolean(java.lang.Class<?>)instead.
-
isByte
@Deprecated public static boolean isByte(Class<?> type)
Deprecated.UseTypes.isByte(java.lang.Class<?>)instead.
-
isCharacter
@Deprecated public static boolean isCharacter(Class<?> type)
Deprecated.UseTypes.isCharacter(java.lang.Class<?>)instead.
-
isDouble
@Deprecated public static boolean isDouble(Class<?> type)
Deprecated.UseTypes.isDouble(java.lang.Class<?>)instead.
-
isFloat
@Deprecated public static boolean isFloat(Class<?> type)
Deprecated.UseTypes.isFloat(java.lang.Class<?>)instead.
-
isInteger
@Deprecated public static boolean isInteger(Class<?> type)
Deprecated.UseTypes.isInteger(java.lang.Class<?>)instead.
-
isLong
@Deprecated public static boolean isLong(Class<?> type)
Deprecated.UseTypes.isLong(java.lang.Class<?>)instead.
-
isShort
@Deprecated public static boolean isShort(Class<?> type)
Deprecated.UseTypes.isShort(java.lang.Class<?>)instead.
-
isNumber
@Deprecated public static boolean isNumber(Class<?> type)
Deprecated.UseTypes.isNumber(java.lang.Class<?>)instead.
-
isText
@Deprecated public static boolean isText(Class<?> type)
Deprecated.UseTypes.isText(java.lang.Class<?>)instead.
-
convert
@Deprecated public static <T> T convert(Object value, Class<T> type)
Deprecated.
-
canConvert
@Deprecated public static boolean canConvert(Class<?> c, Class<?> type)
Deprecated.
-
canConvert
@Deprecated public static boolean canConvert(Object value, Class<?> type)
Deprecated.
-
cast
@Deprecated public static <T> T cast(Object obj, Class<T> type)
Deprecated.
-
canCast
@Deprecated public static boolean canCast(Class<?> c, Class<?> type)
Deprecated.
-
canCast
@Deprecated public static boolean canCast(Object obj, Class<?> type)
Deprecated.
-
getNonprimitiveType
@Deprecated public static <T> Class<T> getNonprimitiveType(Class<T> type)
Deprecated.useTypes.box(Class)
-
getNullValue
@Deprecated public static <T> T getNullValue(Class<T> type)
Deprecated.
-
getTypes
@Deprecated public static List<Class<?>> getTypes(Field field, Class<?> type)
Deprecated.UseTypes.fieldType(Field, Class)andTypes.raws(java.lang.reflect.Type)instead.
-
getGenericType
@Deprecated public static Type getGenericType(Field field, Class<?> type)
Deprecated.UseTypes.fieldType(Field, Class)instead.
-
getField
@Deprecated public static Field getField(String className, String fieldName)
Deprecated.UseTypes.field(java.lang.Class<?>, java.lang.String)instead.
-
getField
@Deprecated public static Field getField(Class<?> c, String fieldName)
Deprecated.UseTypes.field(java.lang.Class<?>, java.lang.String)instead.
-
getArrayClass
@Deprecated public static Class<?> getArrayClass(Class<?> elementClass)
Deprecated.UseTypes.array(Class)instead.
-
-