Class ReflectionUtil
java.lang.Object
de.tomatengames.util.ReflectionUtil
Provides convenient methods to utilize the Reflection API.
- Since:
- 1.5
-
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?> Get a class by its name, wrapsClass.forName(String).static <T> TConstructs a new object of the given class using a matching constructor.static <T> Tconstruct(Constructor<T> constr) Constructs a new object using the given constructor and no parameters.static <T> Tconstruct(Constructor<T> constr, Object... param) Constructs a new object using the given constructor and constructor parameters, wrapsConstructor.newInstance(Object...).static <T> TConstructs a new object of the given class name using a matching constructor.static <T> Constructor<T> constructor(Class<T> c, Class<?>... paramTypes) Get a constructor by its parameter types, wrapsClass.getDeclaredConstructor(Class...).static FieldGet a class field by its name, wrapsClass.getDeclaredField(String).static <T> Constructor<T> findConstructor(Class<T> c, Class<?>... compatibleParamTypes) searchConstructor(Class, Class...)but throws an exception if no constructor is found.static FieldsearchField(Class, String)but throws an exception when no field is found.static MethodfindMethod(Class<?> c, String name, Class<?>... compatibleParamTypes) searchMethod(Class, String, Class...)but throws an exception if no method is found.static <T> TGet the current value of the field of the given class and name in the given object.static ObjectGet the current value of the given field in the given object as an Object, wrapsField.get(Object).static <T> TGet the current value of the field of the given name in the given object.static booleangetBoolean(Object object, Field field) Get the current value of the given field in the given object as a boolean, wrapsField.getBoolean(Object).static byteGet the current value of the given field in the given object as a byte, wrapsField.getByte(Object).static charGet the current value of the given field in the given object as a char, wrapsField.getChar(Object).static doubleGet the current value of the given field in the given object as a double, wrapsField.getDouble(Object).static floatGet the current value of the given field in the given object as a float, wrapsField.getFloat(Object).static intGet the current value of the given field in the given object as an int, wrapsField.getInt(Object).static longGet the current value of the given field in the given object as a long, wrapsField.getLong(Object).static shortGet the current value of the given field in the given object as a short, wrapsField.getShort(Object).static <T> TGet the current value of the static field of the given class and name.static ObjectGet the current value of the given static field as an Object.static <T> TGet the current value of the static field of the given class name and field name.static booleangetStaticBoolean(Field field) Get the current value of the given static field as a boolean.static bytegetStaticByte(Field field) Get the current value of the given static field as a byte.static chargetStaticChar(Field field) Get the current value of the given static field as a char.static doublegetStaticDouble(Field field) Get the current value of the given static field as a double.static floatgetStaticFloat(Field field) Get the current value of the given static field as a float.static intgetStaticInt(Field field) Get the current value of the given static field as an int.static longgetStaticLong(Field field) Get the current value of the given static field as a long.static shortgetStaticShort(Field field) Get the current value of the given static field as a short.static MethodGet a method by its name and parameter types, wrapsClass.getDeclaredMethod(String, Class...).static ObjectRuns the given method of the given object with no parameters.static ObjectRuns the given method of the given object using the given parameters.static <T> TRuns a matching method of the given object and name.static <T> TRuns a matching method of the given class and name.static ObjectRuns the given static method using no parameters.static ObjectRuns the given static method using the given parameters.static <T> TRuns a matching method of the given class name and method name.static <T> Constructor<T> searchConstructor(Class<T> c, Class<?>... compatibleParamTypes) Searches the given class for a constructor that the given parameter types can be passed to.static FieldsearchField(Class<?> c, String name) Searches the given class hierarchy for any field of the given name.static MethodsearchMethod(Class<?> c, String name, Class<?>... compatibleParamTypes) Searches the given class hierarchy for a method of the given name that the given parameter types can be passed to.static voidSet the current value of the field of the given class and name in the given object.static voidSet the current value of the given field in the given object to an Object, wrapsField.set(Object, Object).static voidSet the current value of the field of the given name in the given object.static voidsetBoolean(Object object, Field field, boolean value) Set the current value of the given field in the given object to a boolean, wrapsField.setBoolean(Object, boolean).static voidSet the current value of the given field in the given object to a byte, wrapsField.setByte(Object, byte).static voidSet the current value of the given field in the given object to a char, wrapsField.setChar(Object, char).static voidSet the current value of the given field in the given object to a double, wrapsField.setDouble(Object, double).static voidSet the current value of the given field in the given object to a float, wrapsField.setFloat(Object, float).static voidSet the current value of the given field in the given object to an int, wrapsField.setInt(Object, int).static voidSet the current value of the given field in the given object to a long, wrapsField.setLong(Object, long).static voidSet the current value of the given field in the given object to a short, wrapsField.setShort(Object, short).static voidSet the current value of the static field of the given class and name.static voidSet the current value of the given static field to an Object.static voidSet the current value of the static field of the given class name and field name.static voidsetStaticBoolean(Field field, boolean value) Set the current value of the given static field to a boolean.static voidsetStaticByte(Field field, byte value) Set the current value of the given static field to a byte.static voidsetStaticChar(Field field, char value) Set the current value of the given static field to a char.static voidsetStaticDouble(Field field, double value) Set the current value of the given static field to a double.static voidsetStaticFloat(Field field, float value) Set the current value of the given static field to a float.static voidsetStaticInt(Field field, int value) Set the current value of the given static field to an int.static voidsetStaticLong(Field field, long value) Set the current value of the given static field to a long.static voidsetStaticShort(Field field, short value) Set the current value of the given static field to a short.
-
Method Details
-
Class
Get a class by its name, wrapsClass.forName(String).- Parameters:
name- the name of the class- Returns:
- the class reference
- Throws:
ReflectionException- if the class is not found
-
field
Get a class field by its name, wrapsClass.getDeclaredField(String).- Parameters:
c- the classname- the name of the field- Returns:
- the field reference
- Throws:
ReflectionException- if no such field is found
-
constructor
Get a constructor by its parameter types, wrapsClass.getDeclaredConstructor(Class...).- Type Parameters:
T- the class type, made available to the output constructor reference without casting- Parameters:
c- the classparamTypes- the parameter types- Returns:
- the constructor reference
- Throws:
ReflectionException- if no such constructor is found
-
method
Get a method by its name and parameter types, wrapsClass.getDeclaredMethod(String, Class...).- Parameters:
c- the classname- the name of the methodparamTypes- the parameter types- Returns:
- the method reference
- Throws:
ReflectionException- if no such method is found
-
searchField
Searches the given class hierarchy for any field of the given name.- Parameters:
c- the most specific class searchedname- the name of the field- Returns:
- the first found field reference or null if no such field is found in the class or any superclass
-
findField
searchField(Class, String)but throws an exception when no field is found.- Parameters:
c- the most specific class searchedname- the name of the field- Returns:
- the first found field reference
- Throws:
ReflectionException- if no such field is found
-
searchConstructor
Searches the given class for a constructor that the given parameter types can be passed to.- Type Parameters:
T- the class type, made available to the output constructor reference without casting- Parameters:
c- the classcompatibleParamTypes- the passable parameter types- Returns:
- a constructor reference matching the given parameter types or null if no such constructor is found
-
findConstructor
searchConstructor(Class, Class...)but throws an exception if no constructor is found.- Type Parameters:
T- the class type- Parameters:
c- the classcompatibleParamTypes- the passable parameter types- Returns:
- a constructor reference matching the given parameter types
- Throws:
ReflectionException- if no such constructor is found
-
searchMethod
Searches the given class hierarchy for a method of the given name that the given parameter types can be passed to.- Parameters:
c- the most specific classname- the name of the methodcompatibleParamTypes- the passable parameter types- Returns:
- the first found method reference matching the given name and parameter types or null if no such method is found
-
findMethod
searchMethod(Class, String, Class...)but throws an exception if no method is found.- Parameters:
c- the most specific classname- the name of the methodcompatibleParamTypes- the passable parameter types- Returns:
- the first found method reference matching the given name and parameter types
- Throws:
ReflectionException- if no such method is found
-
construct
Constructs a new object using the given constructor and constructor parameters, wrapsConstructor.newInstance(Object...).- Type Parameters:
T- the type of the returned object- Parameters:
constr- the constructor referenceparam- the constructor parameters- Returns:
- the new object
- Throws:
ReflectionException- if aReflectiveOperationExceptionoccurs, which may be anIllegalAccessException, anInstantiationExceptionor anInvocationTargetException.
-
construct
Constructs a new object using the given constructor and no parameters.- Type Parameters:
T- the type of the returned object- Parameters:
constr- the constructor reference- Returns:
- the new object
- Throws:
ReflectionException- if aReflectiveOperationExceptionoccurs- See Also:
-
run
Runs the given method of the given object using the given parameters.- Parameters:
o- the target objectm- the method referenceparam- the method parameters- Returns:
- the return value
- Throws:
ReflectionException- if aReflectiveOperationExceptionoccurs, which may be anIllegalAccessExceptionor anInvocationTargetException.
-
run
Runs the given method of the given object with no parameters.- Parameters:
o- the target objectm- the method reference- Returns:
- the return value
- Throws:
ReflectionException- if aReflectiveOperationExceptionoccurs- See Also:
-
runStatic
Runs the given static method using the given parameters.- Parameters:
m- the method referenceparam- the parameters- Returns:
- the return value
- Throws:
ReflectionException- if aReflectiveOperationExceptionoccurs- See Also:
-
runStatic
Runs the given static method using no parameters.- Parameters:
m- the method reference- Returns:
- the return value
- Throws:
ReflectionException- if aReflectiveOperationExceptionoccurs- See Also:
-
get
Get the current value of the field of the given class and name in the given object. For efficiency, useget(Object, Field)instead.- Type Parameters:
T- the returned type, automatically casted- Parameters:
o- the target objectfieldClass- the field classfieldName- the field name- Returns:
- the value of the found field
- Throws:
ReflectionException- if no such field is found or anIllegalAccessExceptionoccurs
-
set
Set the current value of the field of the given class and name in the given object. For efficiency, useset(Object, Field, Object)instead.- Parameters:
o- the target objectfieldClass- the field classfieldName- the field namevalue- the value for the found field- Throws:
ReflectionException- if no such field is found or anIllegalAccessExceptionoccurs
-
get
Get the current value of the field of the given name in the given object. For efficiency, useget(Object, Field)instead.- Type Parameters:
T- the returned type, automatically casted- Parameters:
o- the target objectfieldName- the name of the field- Returns:
- the value of the found field
- Throws:
ReflectionException- if no such field is found or anIllegalAccessExceptionoccurs
-
set
Set the current value of the field of the given name in the given object. For efficiency, useset(Object, Field, Object)instead.- Parameters:
o- the target objectfieldName- the name of the fieldvalue- the value for the found field- Throws:
ReflectionException- if no such field is found or anIllegalAccessExceptionoccurs
-
getStatic
Get the current value of the static field of the given class and name. For efficiency, usegetStatic(Field)instead.- Type Parameters:
T- the returned type, automatically casted- Parameters:
c- the field classfieldName- the field name- Returns:
- the value of the found field
- Throws:
ReflectionException- if no such field is found or anIllegalAccessExceptionoccurs
-
setStatic
Set the current value of the static field of the given class and name. For efficiency, usesetStatic(Field, Object)instead.- Parameters:
c- the field classfieldName- the field namevalue- the value for the found field- Throws:
ReflectionException- if no such field is found or anIllegalAccessExceptionoccurs
-
getStatic
Get the current value of the static field of the given class name and field name. For efficiency, usegetStatic(Field)instead.- Type Parameters:
T- the returned type, automatically casted- Parameters:
className- the field class namefieldName- the field name- Returns:
- the value of the found field
- Throws:
ReflectionException- if no such class or field is found or anIllegalAccessExceptionoccurs
-
setStatic
Set the current value of the static field of the given class name and field name. For efficiency, usesetStatic(Field, Object)instead.- Parameters:
className- the field class namefieldName- the field namevalue- the value for the found field- Throws:
ReflectionException- if no such class or field is found or anIllegalAccessExceptionoccurs
-
construct
Constructs a new object of the given class using a matching constructor. For efficiency, useconstruct(Constructor, Object...)instead.- Type Parameters:
T- the returned type, automatically casted- Parameters:
c- the class to be instantiatedparam- the constructor parameters- Returns:
- the new object
- Throws:
ReflectionException- if no matching constructor is found or aReflectiveOperationExceptionoccurs- See Also:
-
construct
Constructs a new object of the given class name using a matching constructor. For efficiency, useconstruct(Constructor, Object...)instead.- Type Parameters:
T- the returned type, automatically casted- Parameters:
className- the name of the class to be instantiatedparam- the constructor parameters- Returns:
- the new object
- Throws:
ReflectionException- if no such class or no matching constructor is found or anReflectiveOperationExceptionoccurs- See Also:
-
run
Runs a matching method of the given object and name. For efficiency, userun(Object, Method, Object...)instead.- Type Parameters:
T- the returned type, automatically casted- Parameters:
o- the target objectmethodName- the name of the methodparam- the method parameters- Returns:
- the return value
- Throws:
ReflectionException- if no matching method is found or aReflectiveOperationExceptionoccurs- See Also:
-
runStatic
Runs a matching method of the given class and name. For efficiency, userunStatic(Method, Object...)instead.- Type Parameters:
T- the returned type, automatically casted- Parameters:
c- the method classmethodName- the method nameparam- the method parameters- Returns:
- the return value
- Throws:
ReflectionException- if no matching method is found or aReflectiveOperationExceptionoccurs- See Also:
-
runStatic
Runs a matching method of the given class name and method name. For efficiency, userunStatic(Method, Object...)instead.- Type Parameters:
T- the returned type, automatically casted- Parameters:
className- the method classmethodName- the method nameparam- the method parameters- Returns:
- the return value
- Throws:
ReflectionException- if no such class or matching method is found or aReflectiveOperationExceptionoccurs- See Also:
-
get
Get the current value of the given field in the given object as an Object, wrapsField.get(Object).- Parameters:
object- the objectfield- the field- Returns:
- the value as an Object
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
set
Set the current value of the given field in the given object to an Object, wrapsField.set(Object, Object).- Parameters:
object- the objectfield- the fieldvalue- the value as an Object- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
getStatic
Get the current value of the given static field as an Object.- Parameters:
field- the field- Returns:
- the value as an Object
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
setStatic
Set the current value of the given static field to an Object.- Parameters:
field- the fieldvalue- the value as an Object- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
getBoolean
Get the current value of the given field in the given object as a boolean, wrapsField.getBoolean(Object).- Parameters:
object- the objectfield- the field- Returns:
- the value as a boolean
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
setBoolean
Set the current value of the given field in the given object to a boolean, wrapsField.setBoolean(Object, boolean).- Parameters:
object- the objectfield- the fieldvalue- the value as a boolean- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
getStaticBoolean
Get the current value of the given static field as a boolean.- Parameters:
field- the field- Returns:
- the value as a boolean
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
setStaticBoolean
Set the current value of the given static field to a boolean.- Parameters:
field- the fieldvalue- the value as a boolean- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
getChar
Get the current value of the given field in the given object as a char, wrapsField.getChar(Object).- Parameters:
object- the objectfield- the field- Returns:
- the value as a char
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
setChar
Set the current value of the given field in the given object to a char, wrapsField.setChar(Object, char).- Parameters:
object- the objectfield- the fieldvalue- the value as a char- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
getStaticChar
Get the current value of the given static field as a char.- Parameters:
field- the field- Returns:
- the value as a char
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
setStaticChar
Set the current value of the given static field to a char.- Parameters:
field- the fieldvalue- the value as a char- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
getByte
Get the current value of the given field in the given object as a byte, wrapsField.getByte(Object).- Parameters:
object- the objectfield- the field- Returns:
- the value as a byte
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
setByte
Set the current value of the given field in the given object to a byte, wrapsField.setByte(Object, byte).- Parameters:
object- the objectfield- the fieldvalue- the value as a byte- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
getStaticByte
Get the current value of the given static field as a byte.- Parameters:
field- the field- Returns:
- the value as a byte
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
setStaticByte
Set the current value of the given static field to a byte.- Parameters:
field- the fieldvalue- the value as a byte- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
getShort
Get the current value of the given field in the given object as a short, wrapsField.getShort(Object).- Parameters:
object- the objectfield- the field- Returns:
- the value as a short
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
setShort
Set the current value of the given field in the given object to a short, wrapsField.setShort(Object, short).- Parameters:
object- the objectfield- the fieldvalue- the value as a short- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
getStaticShort
Get the current value of the given static field as a short.- Parameters:
field- the field- Returns:
- the value as a short
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
setStaticShort
Set the current value of the given static field to a short.- Parameters:
field- the fieldvalue- the value as a short- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
getInt
Get the current value of the given field in the given object as an int, wrapsField.getInt(Object).- Parameters:
object- the objectfield- the field- Returns:
- the value as an int
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
setInt
Set the current value of the given field in the given object to an int, wrapsField.setInt(Object, int).- Parameters:
object- the objectfield- the fieldvalue- the value as an int- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
getStaticInt
Get the current value of the given static field as an int.- Parameters:
field- the field- Returns:
- the value as an int
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
setStaticInt
Set the current value of the given static field to an int.- Parameters:
field- the fieldvalue- the value as an int- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
getLong
Get the current value of the given field in the given object as a long, wrapsField.getLong(Object).- Parameters:
object- the objectfield- the field- Returns:
- the value as a long
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
setLong
Set the current value of the given field in the given object to a long, wrapsField.setLong(Object, long).- Parameters:
object- the objectfield- the fieldvalue- the value as a long- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
getStaticLong
Get the current value of the given static field as a long.- Parameters:
field- the field- Returns:
- the value as a long
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
setStaticLong
Set the current value of the given static field to a long.- Parameters:
field- the fieldvalue- the value as a long- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
getFloat
Get the current value of the given field in the given object as a float, wrapsField.getFloat(Object).- Parameters:
object- the objectfield- the field- Returns:
- the value as a float
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
setFloat
Set the current value of the given field in the given object to a float, wrapsField.setFloat(Object, float).- Parameters:
object- the objectfield- the fieldvalue- the value as a float- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
getStaticFloat
Get the current value of the given static field as a float.- Parameters:
field- the field- Returns:
- the value as a float
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
setStaticFloat
Set the current value of the given static field to a float.- Parameters:
field- the fieldvalue- the value as a float- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
getDouble
Get the current value of the given field in the given object as a double, wrapsField.getDouble(Object).- Parameters:
object- the objectfield- the field- Returns:
- the value as a double
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
setDouble
Set the current value of the given field in the given object to a double, wrapsField.setDouble(Object, double).- Parameters:
object- the objectfield- the fieldvalue- the value as a double- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs
-
getStaticDouble
Get the current value of the given static field as a double.- Parameters:
field- the field- Returns:
- the value as a double
- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-
setStaticDouble
Set the current value of the given static field to a double.- Parameters:
field- the fieldvalue- the value as a double- Throws:
ReflectionException- if anIllegalAccessExceptionoccurs or the given field is not static
-