Package pro.johndunlap.getopt
Class ReflectionUtil
- java.lang.Object
-
- pro.johndunlap.getopt.ReflectionUtil
-
public class ReflectionUtil extends Object
A utility class which contains reflective methods for working with Java objects.- Author:
- John Dunlap
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MethodfindGetterMethod(Field field)Attempts to find a getter method for the given field.static MethodfindSetterMethod(Field field)Attempts to find a setter method for the given field.static ObjectgetFieldValue(Field field, Object instance)This method attempts to get the value of a field without violating its declared access modifiers.static Constructor<?>getNoArgConstructor(Class<?> clazz)Attempts to find a no-arg constructor for the given class.static <T> Tinstantiate(Class<?> type)This method attempts to construct an instance of the given class without violating its declared access modifiers.static ObjectinstantiateCollection(Class<Collection<?>> collectionClass)booleanisArray(Class<?> clazz)static booleanisBoolean(Class<?> type)Returns true if the given type is a byte type (Boolean or boolean).static booleanisByte(Class<?> type)Returns true if the given type is a byte type (Byte or byte).static booleanisCharacter(Class<?> type)Returns true if the given type is a character type (Character or char).static booleanisCollection(Class<?> clazz)static booleanisDouble(Class<?> type)Returns true if the given type is a double type (Double or double).static booleanisFloat(Class<?> type)Returns true if the given type is a float type (Float or float).static booleanisInteger(Class<?> type)Returns true if the given type is a byte type (Integer or int).static booleanisLong(Class<?> type)Returns true if the given type is a byte type (Long or long).static booleanisShort(Class<?> type)Returns true if the given type is a byte type (Short or short).static <T> Tparse(Class<T> fieldType, String value)Parses a string value into an object of the given type.static voidsetFieldValue(Field field, Object instance, Object value)This method attempts to set the value of a field without violating its declared access modifiers.
-
-
-
Method Detail
-
getFieldValue
public static Object getFieldValue(Field field, Object instance) throws IllegalAccessException
This method attempts to get the value of a field without violating its declared access modifiers. However, if it is unable to do so, it will override the access modifiers and try again. If available, getter methods will be used. Direct field access will only be used as a last resort. However, if there is a public field with a private getter (unlikely), then public field will be used. If both the field and the getter are private, the getter will be used.- Parameters:
field- The field from which the value should be takeninstance- The instance from which the value should be taken- Returns:
- The value of the field
- Throws:
IllegalAccessException- Thrown if the field or method is inaccessible
-
instantiate
public static <T> T instantiate(Class<?> type) throws MissingNoArgConstructorException
This method attempts to construct an instance of the given class without violating its declared access modifiers.- Type Parameters:
T- The type of object to instantiate- Parameters:
type- The type of object to instantiate- Returns:
- The instantiated object
- Throws:
MissingNoArgConstructorException- Thrown if the class does not have a no-arg constructor
-
setFieldValue
public static void setFieldValue(Field field, Object instance, Object value) throws IllegalAccessException
This method attempts to set the value of a field without violating its declared access modifiers. However, if it is unable to do so, it will override the access modifiers and try again. If available, setter methods will be used. Direct field access will only be used as a last resort. However, if there is a public field with a private setter (unlikely), then public field will be used. If both the field and the setter are private,the setter will be used.- Parameters:
field- The field to which the value should be setinstance- The instance to which the value should be setvalue- The value to set- Throws:
IllegalAccessException- Thrown if the field or method is inaccessible
-
findSetterMethod
public static Method findSetterMethod(Field field)
Attempts to find a setter method for the given field. If one is not found, null is returned.- Parameters:
field- The field for which to find a setter method- Returns:
- The setter method, or null if one is not found
-
findGetterMethod
public static Method findGetterMethod(Field field)
Attempts to find a getter method for the given field. If one is not found, null is returned.- Parameters:
field- The field for which to find a getter method- Returns:
- The getter method, or null if one is not found
-
getNoArgConstructor
public static Constructor<?> getNoArgConstructor(Class<?> clazz)
Attempts to find a no-arg constructor for the given class. If one is not found, null is returned.- Parameters:
clazz- The class for which to find a no-arg constructor- Returns:
- The no-arg constructor, or null if one is not found
-
isCollection
public static boolean isCollection(Class<?> clazz)
-
isArray
public boolean isArray(Class<?> clazz)
-
instantiateCollection
public static Object instantiateCollection(Class<Collection<?>> collectionClass)
-
parse
public static <T> T parse(Class<T> fieldType, String value) throws ParseException
Parses a string value into an object of the given type.- Type Parameters:
T- The type of the object to be returned- Parameters:
fieldType- The type of the object to be returnedvalue- The string value to be parsed- Returns:
- The parsed value
- Throws:
ParseException- Thrown if the value cannot be parsed into the given type
-
isBoolean
public static boolean isBoolean(Class<?> type)
Returns true if the given type is a byte type (Boolean or boolean).- Parameters:
type- The type to check- Returns:
- True if the given type is a boolean type
-
isInteger
public static boolean isInteger(Class<?> type)
Returns true if the given type is a byte type (Integer or int).- Parameters:
type- The type to check- Returns:
- True if the given type is a byte type
-
isShort
public static boolean isShort(Class<?> type)
Returns true if the given type is a byte type (Short or short).- Parameters:
type- The type to check- Returns:
- True if the given type is a short type
-
isLong
public static boolean isLong(Class<?> type)
Returns true if the given type is a byte type (Long or long).- Parameters:
type- The type to check- Returns:
- True if the given type is a long type
-
isFloat
public static boolean isFloat(Class<?> type)
Returns true if the given type is a float type (Float or float).- Parameters:
type- The type to check- Returns:
- True if the given type is a float type
-
isDouble
public static boolean isDouble(Class<?> type)
Returns true if the given type is a double type (Double or double).- Parameters:
type- The type to check- Returns:
- True if the given type is a double type
-
isByte
public static boolean isByte(Class<?> type)
Returns true if the given type is a byte type (Byte or byte).- Parameters:
type- The type to check- Returns:
- True if the given type is a byte type
-
isCharacter
public static boolean isCharacter(Class<?> type)
Returns true if the given type is a character type (Character or char).- Parameters:
type- The type to check- Returns:
- True if the given type is a character type
-
-