public final class Reflector
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
PRIMITIVE_BOOLEAN
Boolean primitive class name
|
static java.lang.String |
PRIMITIVE_BYTE
Byte primitive class name
|
static java.lang.String |
PRIMITIVE_CHAR
Char primitive class name
|
static java.lang.String |
PRIMITIVE_DOUBLE
Double primitive class name
|
static java.lang.String |
PRIMITIVE_FLOAT
Float primitive class name
|
static java.lang.String |
PRIMITIVE_INT
Int primitive class name
|
static java.lang.String |
PRIMITIVE_LONG
Long primitive class name
|
static java.lang.String |
PRIMITIVE_SHORT
Short primitive class name
|
| Constructor and Description |
|---|
Reflector() |
| Modifier and Type | Method and Description |
|---|---|
static void |
forceChangeField(java.lang.Object object,
java.lang.String fieldName,
boolean newValue)
Force change a boolean field value
|
static void |
forceChangeField(java.lang.Object object,
java.lang.String fieldName,
java.lang.Object newValue)
Force change a field value.
|
static java.lang.Object |
forceGetField(java.lang.Object object,
java.lang.String fieldName)
Force to obtain a field value
|
static java.lang.Object |
forceGetStaticField(java.lang.Class<?> claz,
java.lang.String fieldName)
Get a static field value
|
static void |
forceInvokeMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Class<?>[] types,
java.lang.Object[] values)
Force invoke a method
|
static java.lang.Object |
invokePublicMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Object... parameters)
Invoke a public method from a class.
|
static java.lang.Object |
invokePublicMethod(java.lang.Object instance,
java.lang.String methodName,
java.lang.Object... parameters)
Invoke a public method form a class represents by its instance.
|
static boolean |
isInherit(java.lang.Class<?> test,
java.lang.Class<?> parent)
Indicates if a class extends an other one
|
static boolean |
isSubTypeOf(java.lang.Class<?> testedClass,
java.lang.Class<?> parentOrInterface)
Indicates if a class extends or implements directly or not a class or a interface
|
static java.lang.Object |
newInstance(java.lang.Class<?> type)
Try create an instance of a class.
|
static java.lang.Object |
newInstance(java.lang.String typeName)
Try create an instance of a class.
|
static java.lang.Object |
newInstance(java.lang.String typeName,
java.lang.ClassLoader classLoader)
Try create an instance of a class.
|
static java.lang.reflect.Field |
obtainField(java.lang.Object object,
java.lang.String fieldName)
Obtain a field
|
static java.lang.reflect.Method |
obtainPublicMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... types)
Obtain public method from a class
|
static java.lang.Class<?>[] |
obtainTypes(java.lang.Object... parameters)
Extract all types of given parameters.
|
static boolean |
typeMatch(java.lang.Class<?>[] types1,
java.lang.Class<?>[] types2)
Indicates if 2 arrays of types are similar.
|
static boolean |
typeMatch(java.lang.Class<?> class1,
java.lang.Class<?> class2)
Indicates if 2 types are similar.
|
public static final java.lang.String PRIMITIVE_BOOLEAN
public static final java.lang.String PRIMITIVE_BYTE
public static final java.lang.String PRIMITIVE_CHAR
public static final java.lang.String PRIMITIVE_DOUBLE
public static final java.lang.String PRIMITIVE_FLOAT
public static final java.lang.String PRIMITIVE_INT
public static final java.lang.String PRIMITIVE_LONG
public static final java.lang.String PRIMITIVE_SHORT
public static final void forceChangeField(java.lang.Object object,
java.lang.String fieldName,
boolean newValue)
throws java.lang.NoSuchFieldException,
java.lang.IllegalArgumentException,
java.lang.IllegalAccessException
object - Object where lies the fieldfieldName - Field namenewValue - New valuejava.lang.NoSuchFieldException - If field not existsjava.lang.IllegalArgumentException - If field isn't booleanjava.lang.IllegalAccessException - If force failedpublic static final void forceChangeField(java.lang.Object object,
java.lang.String fieldName,
java.lang.Object newValue)
throws java.lang.NoSuchFieldException,
java.lang.IllegalArgumentException,
java.lang.IllegalAccessException
object - Object where lies the fieldfieldName - Field namenewValue - New valuejava.lang.NoSuchFieldException - If field dosen't existsjava.lang.IllegalArgumentException - If value is not validjava.lang.IllegalAccessException - If failed to change the fieldpublic static final java.lang.Object forceGetField(java.lang.Object object,
java.lang.String fieldName)
throws java.lang.NoSuchFieldException,
java.lang.IllegalArgumentException,
java.lang.IllegalAccessException
object - Object where lies the fieldfieldName - Field namejava.lang.NoSuchFieldException - If field not existsjava.lang.IllegalArgumentException - Should never appendjava.lang.IllegalAccessException - If access cant be forcepublic static final java.lang.Object forceGetStaticField(java.lang.Class<?> claz,
java.lang.String fieldName)
throws java.lang.NoSuchFieldException,
java.lang.IllegalArgumentException,
java.lang.IllegalAccessException
claz - Class where lies the fieldfieldName - Filed namejava.lang.NoSuchFieldException - If field not existsjava.lang.IllegalArgumentException - If field isn't staticjava.lang.IllegalAccessException - If force failedpublic static final void forceInvokeMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Class<?>[] types,
java.lang.Object[] values)
throws java.lang.NoSuchMethodException,
java.lang.IllegalArgumentException,
java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException
object - Object where lies the methodmethodName - Method nametypes - Types of argumentsvalues - Parameters valuejava.lang.NoSuchMethodException - If method not existsjava.lang.IllegalArgumentException - If one of argument not validjava.lang.IllegalAccessException - If force failedjava.lang.reflect.InvocationTargetException - If launch method failedpublic static final java.lang.Object invokePublicMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Object... parameters)
throws java.lang.NoSuchMethodException,
java.lang.IllegalArgumentException,
java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException
invokePublicMethod(Object, String, Object...)newInstance(Class) to do it.clazz - Class where the method liesmethodName - Method name to invokeparameters - Methods parametersnulljava.lang.NoSuchMethodException - If the given method is not public, or not exists with the parametersjava.lang.IllegalArgumentException - When invoke failedjava.lang.IllegalAccessException - When invoke failedjava.lang.reflect.InvocationTargetException - When invoke failedpublic static final java.lang.Object invokePublicMethod(java.lang.Object instance,
java.lang.String methodName,
java.lang.Object... parameters)
throws java.lang.NoSuchMethodException,
java.lang.IllegalArgumentException,
java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException
instance - Instance of the class where method liesmethodName - Method name to invokeparameters - Methods parametersjava.lang.NoSuchMethodException - If the given method is not public, or not exists with the parametersjava.lang.IllegalArgumentException - When invoke failedjava.lang.IllegalAccessException - When invoke failedjava.lang.reflect.InvocationTargetException - When invoke failedpublic static final boolean isInherit(java.lang.Class<?> test,
java.lang.Class<?> parent)
test - Class to testparent - Class to extendstrue if class extends an other onepublic static final boolean isSubTypeOf(java.lang.Class<?> testedClass,
java.lang.Class<?> parentOrInterface)
testedClass - Class testedparentOrInterface - Class or interface to extends or implementstrue if the class extends or implements directly or not the class or interfacepublic static final java.lang.Object newInstance(java.lang.Class<?> type)
type - Class type to createpublic static final java.lang.Object newInstance(java.lang.String typeName)
throws java.lang.ClassNotFoundException
typeName - Class namejava.lang.ClassNotFoundException - If the class can't be resolvepublic static final java.lang.Object newInstance(java.lang.String typeName,
java.lang.ClassLoader classLoader)
throws java.lang.ClassNotFoundException
typeName - Type nameclassLoader - Class loader to usejava.lang.ClassNotFoundException - If the class can't be resolvepublic static final java.lang.reflect.Field obtainField(java.lang.Object object,
java.lang.String fieldName)
throws java.lang.NoSuchFieldException,
java.lang.IllegalAccessException
object - Object where lies the fieldfieldName - Field namejava.lang.NoSuchFieldException - If field not existsjava.lang.IllegalAccessException - If force failedpublic static final java.lang.reflect.Method obtainPublicMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... types)
throws java.lang.NoSuchMethodException
clazz - Class where method liesmethodName - Method nametypes - Method parameters typejava.lang.NoSuchMethodException - If the method not public or not exists with specified typespublic static final java.lang.Class<?>[] obtainTypes(java.lang.Object... parameters)
null, just null is return.null, the corresponding type was also nullparameters - Parameters to extract is typespublic static final boolean typeMatch(java.lang.Class<?> class1,
java.lang.Class<?> class2)
null is accepted for parametersclass1 - First typeclass2 - Second typetrue if types are similarpublic static final boolean typeMatch(java.lang.Class<?>[] types1,
java.lang.Class<?>[] types2)
null are consider like zero length arraynull elements.typeMatch(Class, Class)types1 - First arraytypes2 - Second arraytrue if arrays are similar