public class ReflectUtil extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static Class<?>[] |
EMPTY_CLASS_ARRAY |
static char |
INNER_CLASS_SEPARATOR_CHAR
The inner class separator character:
'$' == 36. |
static char |
PACKAGE_SEPARATOR_CHAR
The package separator character:
'.' == 46. |
| 构造器和说明 |
|---|
ReflectUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static Object |
callMethod(Object obj,
String methodName,
Class<?>[] parameterTypes,
Object... args)
Calls an instance or static method of the given object.
|
static Object |
callMethod(Object obj,
String methodName,
Object... args)
Calls an instance or static method of the given object.
|
static Object |
callStaticMethod(Class<?> clazz,
String methodName,
Class<?>[] parameterTypes,
Object... args)
Calls a static method of the given class.
|
static Object |
callStaticMethod(Class<?> clazz,
String methodName,
Object... args)
Calls a static method of the given class.
|
static int |
compareParameterTypes(Class<?>[] left,
Class<?>[] right,
Class<?>[] actual) |
static String |
deleteWhitespace(String str)
Deletes all whitespaces from a String as defined by
Character.isWhitespace(char). |
static Class<?> |
findClass(String className,
ClassLoader classLoader)
Look up a class with the specified class loader.
|
static Class<?> |
findClassIfExists(String className,
ClassLoader classLoader)
Look up and return a class if it exists.
|
static Constructor<?> |
findConstructorExact(Class<?> clazz,
Class<?>... parameterTypes)
Look up a constructor of a class and set it to accessible.
|
static Constructor<?> |
findConstructorExact(Class<?> clazz,
Object... parameterTypes)
Look up a constructor of a class and set it to accessible.
|
static Constructor<?> |
findConstructorExact(String className,
ClassLoader classLoader,
Object... parameterTypes)
Look up a constructor of a class and set it to accessible.
|
static Constructor<?> |
findConstructorExactIfExists(Class<?> clazz,
Object... parameterTypes)
Look up and return a constructor if it exists.
|
static Constructor<?> |
findConstructorExactIfExists(String className,
ClassLoader classLoader,
Object... parameterTypes)
Look up and return a constructor if it exists.
|
static Field |
findField(Class<?> clazz,
String fieldName)
Look up a field in a class and set it to accessible.
|
static Field |
findFieldIfExists(Class<?> clazz,
String fieldName)
Look up and return a field if it exists.
|
static Field |
findFirstFieldByExactType(Class<?> clazz,
Class<?> type)
Returns the first field of the given type in a class.
|
static Method |
findMethodBestMatch(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
Look up a method in a class and set it to accessible.
|
static Method |
findMethodBestMatch(Class<?> clazz,
String methodName,
Class<?>[] parameterTypes,
Object[] args)
Look up a method in a class and set it to accessible.
|
static Method |
findMethodBestMatch(Class<?> clazz,
String methodName,
Object... args)
Look up a method in a class and set it to accessible.
|
static Method |
findMethodExact(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
Look up a method in a class and set it to accessible.
|
static Method |
findMethodExact(Class<?> clazz,
String methodName,
Object... parameterTypes)
Look up a method in a class and set it to accessible.
|
static Method |
findMethodExact(String className,
ClassLoader classLoader,
String methodName,
Object... parameterTypes)
Look up a method in a class and set it to accessible.
|
static Method |
findMethodExactIfExists(Class<?> clazz,
String methodName,
Object... parameterTypes)
Look up and return a method if it exists.
|
static Method |
findMethodExactIfExists(String className,
ClassLoader classLoader,
String methodName,
Object... parameterTypes)
Look up and return a method if it exists.
|
static Method[] |
findMethodsByExactParameters(Class<?> clazz,
Class<?> returnType,
Class<?>... parameterTypes)
Returns an array of all methods declared/overridden in a class with the specified parameter types.
|
static boolean |
getBooleanField(Object obj,
String fieldName)
Returns the value of a
boolean field in the given object instance. |
static byte |
getByteField(Object obj,
String fieldName)
Returns the value of a
byte field in the given object instance. |
static char |
getCharField(Object obj,
String fieldName)
Returns the value of a
char field in the given object instance. |
static Class<?> |
getClass(ClassLoader classLoader,
String className,
boolean initialize)
Returns the class represented by
className using the
classLoader. |
static Class<?>[] |
getClassesAsArray(Class<?>... clazzes)
Returns an array of the given classes.
|
static double |
getDoubleField(Object obj,
String fieldName)
Returns the value of a
double field in the given object instance. |
static <T> T |
getFieldValue(Object object,
String fieldName) |
static float |
getFloatField(Object obj,
String fieldName)
Returns the value of a
float field in the given object instance. |
static int |
getIntField(Object obj,
String fieldName)
Returns the value of an
int field in the given object instance. |
static int |
getLength(Object array) |
static long |
getLongField(Object obj,
String fieldName)
Returns the value of a
long field in the given object instance. |
static Class<?>[] |
getParameterTypes(Object... args)
Returns an array with the classes of the given objects.
|
static short |
getShortField(Object obj,
String fieldName)
Returns the value of a
short field in the given object instance. |
static <T> T |
getStaticFiled(Class<?> clazz,
String filedName) |
static boolean |
isAssignable(Class<?>[] classArray,
Class<?>[] toClassArray,
boolean autoboxing) |
static boolean |
isAssignable(Class<?> cls,
Class<?> toClass,
boolean autoboxing) |
static boolean |
isSameLength(Object[] array1,
Object[] array2) |
static Class<?> |
primitiveToWrapper(Class<?> cls) |
static void |
setFieldValue(Object object,
String fieldName,
Object value) |
static Class<?> |
wrapperToPrimitive(Class<?> cls) |
public static final char PACKAGE_SEPARATOR_CHAR
'.' == 46.public static final char INNER_CLASS_SEPARATOR_CHAR
'$' == 36.public static final Class<?>[] EMPTY_CLASS_ARRAY
public static boolean getBooleanField(Object obj, String fieldName)
boolean field in the given object instance. A class reference is not sufficient! See also findField(java.lang.Class<?>, java.lang.String).public static byte getByteField(Object obj, String fieldName)
byte field in the given object instance. A class reference is not sufficient! See also findField(java.lang.Class<?>, java.lang.String).public static char getCharField(Object obj, String fieldName)
char field in the given object instance. A class reference is not sufficient! See also findField(java.lang.Class<?>, java.lang.String).public static double getDoubleField(Object obj, String fieldName)
double field in the given object instance. A class reference is not sufficient! See also findField(java.lang.Class<?>, java.lang.String).public static float getFloatField(Object obj, String fieldName)
float field in the given object instance. A class reference is not sufficient! See also findField(java.lang.Class<?>, java.lang.String).public static int getIntField(Object obj, String fieldName)
int field in the given object instance. A class reference is not sufficient! See also findField(java.lang.Class<?>, java.lang.String).public static long getLongField(Object obj, String fieldName)
long field in the given object instance. A class reference is not sufficient! See also findField(java.lang.Class<?>, java.lang.String).public static short getShortField(Object obj, String fieldName)
short field in the given object instance. A class reference is not sufficient! See also findField(java.lang.Class<?>, java.lang.String).public static Object callMethod(Object obj, String methodName, Object... args)
findMethodBestMatch(Class, String, Object...).obj - The object instance. A class reference is not sufficient!methodName - The method name.args - The arguments for the method call.NoSuchMethodError - In case no suitable method was found.public static Object callMethod(Object obj, String methodName, Class<?>[] parameterTypes, Object... args)
callMethod(Object, String, Object...).
This variant allows you to specify parameter types, which can help in case there are multiple
methods with the same name, especially if you call it with null parameters.
public static Object callStaticMethod(Class<?> clazz, String methodName, Object... args)
findMethodBestMatch(Class, String, Object...).clazz - The class reference.methodName - The method name.args - The arguments for the method call.NoSuchMethodError - In case no suitable method was found.public static Object callStaticMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes, Object... args)
callStaticMethod(Class, String, Object...).
This variant allows you to specify parameter types, which can help in case there are multiple
methods with the same name, especially if you call it with null parameters.
public static Method findMethodExact(Class<?> clazz, String methodName, Object... parameterTypes)
findMethodExact(String, ClassLoader, String, Object...) for details.public static Method findMethodExactIfExists(Class<?> clazz, String methodName, Object... parameterTypes)
findMethodExactIfExists(String, ClassLoader, String, Object...) for details.public static Method findMethodExact(String className, ClassLoader classLoader, String methodName, Object... parameterTypes)
the method and parameter type resolution.
className - The name of the class which implements the method.classLoader - The class loader for resolving the target and parameter classes.methodName - The target method name.parameterTypes - The parameter types of the target method.NoSuchMethodError - In case the method was not found.public static Method findMethodExactIfExists(String className, ClassLoader classLoader, String methodName, Object... parameterTypes)
findMethodExact(String, ClassLoader, String, Object...), but doesn't throw an
exception if the method doesn't exist.className - The name of the class which implements the method.classLoader - The class loader for resolving the target and parameter classes.methodName - The target method name.parameterTypes - The parameter types of the target method.null if it doesn't exist.public static Method findMethodExact(Class<?> clazz, String methodName, Class<?>... parameterTypes)
findMethodExact(String, ClassLoader, String, Object...) for details.
This variant requires that you already have reference to all the parameter types.
public static Method[] findMethodsByExactParameters(Class<?> clazz, Class<?> returnType, Class<?>... parameterTypes)
The return type is optional, it will not be compared if it is null.
Use void.class if you want to search for methods returning nothing.
clazz - The class to look in.returnType - The return type, or null (see above).parameterTypes - The parameter types.public static Method findMethodBestMatch(Class<?> clazz, String methodName, Class<?>... parameterTypes)
This does'nt only look for exact matches, but for the best match. All considered candidates must be compatible with the given parameter types, i.e. the parameters must be assignable to the method's formal parameters. Inherited methods are considered here.
clazz - The class which declares, inherits or overrides the method.methodName - The method name.parameterTypes - The types of the method's parameters.NoSuchMethodError - In case no suitable method was found.public static Method findMethodBestMatch(Class<?> clazz, String methodName, Object... args)
See findMethodBestMatch(Class, String, Class...) for details. This variant
determines the parameter types from the classes of the given objects.
public static Method findMethodBestMatch(Class<?> clazz, String methodName, Class<?>[] parameterTypes, Object[] args)
See findMethodBestMatch(Class, String, Class...) for details. This variant
determines the parameter types from the classes of the given objects. For any item that is
null, the type is taken from parameterTypes instead.
public static Class<?>[] getParameterTypes(Object... args)
public static Class<?>[] getClassesAsArray(Class<?>... clazzes)
public static Constructor<?> findConstructorExact(Class<?> clazz, Object... parameterTypes)
findMethodExact(String, ClassLoader, String, Object...) for details.public static Constructor<?> findConstructorExactIfExists(Class<?> clazz, Object... parameterTypes)
findMethodExactIfExists(String, ClassLoader, String, Object...) for details.public static Constructor<?> findConstructorExact(String className, ClassLoader classLoader, Object... parameterTypes)
findMethodExact(String, ClassLoader, String, Object...) for details.public static Constructor<?> findConstructorExactIfExists(String className, ClassLoader classLoader, Object... parameterTypes)
findMethodExactIfExists(String, ClassLoader, String, Object...) for details.public static Constructor<?> findConstructorExact(Class<?> clazz, Class<?>... parameterTypes)
findMethodExact(String, ClassLoader, String, Object...) for details.public static Class<?> findClass(String className, ClassLoader classLoader)
There are various allowed syntaxes for the class name, but it's recommended to use one of
these:java.lang.String
java.lang.String[] (array)
android.app.ActivityThread.ResourcesKey
android.app.ActivityThread$ResourcesKey
className - The class name in one of the formats mentioned above.classLoader - The class loader, or null for the boot class loader.public static Class<?> findClassIfExists(String className, ClassLoader classLoader)
findClass(java.lang.String, java.lang.ClassLoader), but doesn't throw an exception if the class doesn't exist.className - The class name.classLoader - The class loader, or null for the boot class loader.null if it doesn't exist.public static Field findField(Class<?> clazz, String fieldName)
clazz - The class which either declares or inherits the field.fieldName - The field name.NoSuchFieldError - In case the field was not found.public static Field findFieldIfExists(Class<?> clazz, String fieldName)
findField(java.lang.Class<?>, java.lang.String), but doesn't throw an exception if the field doesn't exist.clazz - The class which either declares or inherits the field.fieldName - The field name.null if it doesn't exist.public static Field findFirstFieldByExactType(Class<?> clazz, Class<?> type)
clazz - The class which either declares or inherits the field.type - The type of the field.NoSuchFieldError - In case no matching field was not found.public static String deleteWhitespace(String str)
Deletes all whitespaces from a String as defined by
Character.isWhitespace(char).
StringUtils.deleteWhitespace(null) = null
StringUtils.deleteWhitespace("") = ""
StringUtils.deleteWhitespace("abc") = "abc"
StringUtils.deleteWhitespace(" ab c ") = "abc"
str - the String to delete whitespace from, may be nullnull if null String inputpublic static Class<?> getClass(ClassLoader classLoader, String className, boolean initialize) throws ClassNotFoundException
className using the
classLoader. This implementation supports the syntaxes
"java.util.Map.Entry[]", "java.util.Map$Entry[]",
"[Ljava.util.Map.Entry;", and "[Ljava.util.Map$Entry;".classLoader - the class loader to use to load the classclassName - the class nameinitialize - whether the class must be initializedclassName using the classLoaderClassNotFoundException - if the class is not foundpublic static int getLength(Object array)
public static boolean isAssignable(Class<?>[] classArray, Class<?>[] toClassArray, boolean autoboxing)
Copyright © 2022. All rights reserved.