Package com.ocs.dynamo.utils
Class ClassUtils
- java.lang.Object
-
- com.ocs.dynamo.utils.ClassUtils
-
public final class ClassUtils extends Object
- Author:
- patrick.deenen Utility class for dealing with classes and annotations
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancanSetProperty(Object obj, String fieldName)Checks if the specified property can be set for the specified object.static voidclearFieldValue(Object obj, String fieldName, Class<?> argType)Clears a fieldstatic <T,S extends T>
voidcopyFields(T from, S to)static Class<?>forClass(String clazz)forClass which doesn't throw an exception and can handle empty class namesstatic <T extends Annotation>
TgetAnnotation(Class<?> clazz, String fieldName, Class<T> annotationClass)Tries to retrieve an annotation, by first looking at the field name, and then at the getter methodstatic <T extends Annotation,R>
RgetAnnotationAttributeValue(Field field, Class<T> annotationClass, String attributeName)Get the value of a field in an annotationstatic <T extends Annotation>
TgetAnnotationOnField(Class<?> clazz, String fieldName, Class<T> annotationClass)Return an annotation on a certain fieldstatic <T extends Annotation>
TgetAnnotationOnField(Field field, Class<T> annotationClass)Returns an annotation on a certain fieldstatic <T extends Annotation>
TgetAnnotationOnMethod(Class<?> clazz, String fieldName, Class<T> annotationClass)Looks for an annotation on a getter methodstatic <T extends Annotation>
TgetAnnotationOnMethod(Method method, Class<T> annotationClass)Looks for an annotation on a (getter) methodstatic byte[]getBytes(Object obj, String fieldName)Retrieves the contents of a field as a byte arraystatic <T> Constructor<T>getConstructor(Class<T> clazz, Object... args)Find constructor based on the types of the given arguments used to instantiate the class with the found constructorstatic FieldgetField(Class<?> clazz, String fieldName)Returns a field with a certain name from a classstatic ObjectgetFieldValue(Object obj, String fieldName)Retrieves a field valuestatic StringgetFieldValueAsString(Object obj, String fieldName)Returns a field value as a Stringstatic StringgetFieldValueAsString(Object obj, String fieldName, String defValue)Returns a field value as a Stringstatic MethodgetGetterMethod(Class<?> clazz, String fieldName)Retrieves the getter method for a certain propertystatic intgetMaxLength(Class<?> clazz, String fieldName)Returns the maximum allowed length of a fieldstatic PropertyDescriptorgetPropertyDescriptorForNestedProperty(Class<?> clazz, String property)Returns the property descriptor for a nested propertystatic <T> Class<?>getResolvedType(Class<T> type, String fieldName, int... indexes)Return a Class> representing the generic parameter for the given indexes.static Class<?>getResolvedType(Object object, int... indexes)Return a Class> representing the generic parameter for the given indexes.static booleanhasMethod(Object obj, String methodName)Check if the object has a (public) method that has the specified namestatic <T> TinstantiateClass(Class<T> clazz, Object... args)Instantiate a class with the given arguments; assumed is that all arguments are not null so the types can be determined and a matching constructor can be found.static voidsetBytes(byte[] bytes, Object obj, String fieldName)static voidsetFieldValue(Object obj, String fieldName, Object value)
-
-
-
Method Detail
-
canSetProperty
public static boolean canSetProperty(Object obj, String fieldName)
Checks if the specified property can be set for the specified object. This method supports nested properties- Parameters:
obj- the objectfieldName- the name of the field- Returns:
-
clearFieldValue
public static void clearFieldValue(Object obj, String fieldName, Class<?> argType)
Clears a field- Parameters:
obj- the object on which to clear the fieldfieldName- the name of the fieldargType-
-
forClass
public static Class<?> forClass(String clazz)
forClass which doesn't throw an exception and can handle empty class names- Parameters:
clazz- The fully qualified class name- Returns:
- the specified class and null when class is not found
-
getAnnotation
public static <T extends Annotation> T getAnnotation(Class<?> clazz, String fieldName, Class<T> annotationClass)
Tries to retrieve an annotation, by first looking at the field name, and then at the getter method- Parameters:
clazz- the classfieldName- the name of the fieldannotationClass- the annotation class to look for- Returns:
-
getAnnotationAttributeValue
public static <T extends Annotation,R> R getAnnotationAttributeValue(Field field, Class<T> annotationClass, String attributeName)
Get the value of a field in an annotation- Parameters:
field- The field with annotationsannotionType- The name of the annotation type to findattributeName- The name of the attribute on the annotation type to find the value- Returns:
- the value of the field of the annotation or null when not found
-
getAnnotationOnField
public static <T extends Annotation> T getAnnotationOnField(Class<?> clazz, String fieldName, Class<T> annotationClass)
Return an annotation on a certain field- Parameters:
clazz- the classfieldName- the field nameannotationClass- the class of the annotation- Returns:
-
getAnnotationOnField
public static <T extends Annotation> T getAnnotationOnField(Field field, Class<T> annotationClass)
Returns an annotation on a certain field- Parameters:
field- the fieldannotationClass- the annotation class- Returns:
-
getAnnotationOnMethod
public static <T extends Annotation> T getAnnotationOnMethod(Class<?> clazz, String fieldName, Class<T> annotationClass)
Looks for an annotation on a getter method- Parameters:
clazz- the classfieldName- the name of the field (will be prepended with "get")annotationClass- the class of the annotation to look for- Returns:
-
getAnnotationOnMethod
public static <T extends Annotation> T getAnnotationOnMethod(Method method, Class<T> annotationClass)
Looks for an annotation on a (getter) method- Parameters:
method- the methodannotationClass- the class of the annotation- Returns:
-
getBytes
public static byte[] getBytes(Object obj, String fieldName)
Retrieves the contents of a field as a byte array- Parameters:
obj-fieldName-- Returns:
-
getConstructor
public static <T> Constructor<T> getConstructor(Class<T> clazz, Object... args)
Find constructor based on the types of the given arguments used to instantiate the class with the found constructor- Parameters:
clazz-args-- Returns:
-
getField
public static Field getField(Class<?> clazz, String fieldName)
Returns a field with a certain name from a class- Parameters:
clazz- the classfieldName- the name of the filed- Returns:
-
getFieldValue
public static Object getFieldValue(Object obj, String fieldName)
Retrieves a field value- Parameters:
obj- the object from which to retrieve the field valuefieldName- the name of the field- Returns:
-
getFieldValueAsString
public static String getFieldValueAsString(Object obj, String fieldName)
Returns a field value as a String- Parameters:
obj- the objectfieldName- the name of the field- Returns:
-
getFieldValueAsString
public static String getFieldValueAsString(Object obj, String fieldName, String defValue)
Returns a field value as a String- Parameters:
obj- the objectfieldName- the name of the fielddefValue- the default value that is used in case of a null value- Returns:
-
getGetterMethod
public static Method getGetterMethod(Class<?> clazz, String fieldName)
Retrieves the getter method for a certain property- Parameters:
clazz- the classfieldName- the name of the property- Returns:
-
getMaxLength
public static int getMaxLength(Class<?> clazz, String fieldName)
Returns the maximum allowed length of a field- Parameters:
clazz-fieldName-- Returns:
-
getPropertyDescriptorForNestedProperty
public static PropertyDescriptor getPropertyDescriptorForNestedProperty(Class<?> clazz, String property)
Returns the property descriptor for a nested property- Parameters:
clazz- the classproperty- the name of the property- Returns:
-
getResolvedType
public static <T> Class<?> getResolvedType(Class<T> type, String fieldName, int... indexes)
Return a Class> representing the generic parameter for the given indexes. Indexes are zero based; for example given the type Map>, getGeneric(0) will access the Integer. Nested generics can be accessed by specifying multiple indexes; for example getGeneric(1, 0) will access the String from the nested List. For convenience, if no indexes are specified the first generic is returned. - Parameters:
type-fieldName-indexes-- Returns:
-
getResolvedType
public static Class<?> getResolvedType(Object object, int... indexes)
Return a Class> representing the generic parameter for the given indexes. Indexes are zero based; for example given the type Map>, getGeneric(0) will access the Integer. Nested generics can be accessed by specifying multiple indexes; for example getGeneric(1, 0) will access the String from the nested List. For convenience, if no indexes are specified the first generic is returned. - Parameters:
method- or method parameter or typeindexes-- Returns:
-
hasMethod
public static boolean hasMethod(Object obj, String methodName)
Check if the object has a (public) method that has the specified name- Parameters:
obj-methodName-- Returns:
-
instantiateClass
public static <T> T instantiateClass(Class<T> clazz, Object... args)
Instantiate a class with the given arguments; assumed is that all arguments are not null so the types can be determined and a matching constructor can be found. When no constructor is found null is returned.- Parameters:
clazz-args-- Returns:
-
setFieldValue
public static void setFieldValue(Object obj, String fieldName, Object value)
- Parameters:
obj-fieldName-value-
-
copyFields
public static <T,S extends T> void copyFields(T from, S to)
-
-