12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
public class ReflectionUtil
{
public static void runMethodsFromObjects(ArrayList<Object> objects, String methodName, Object... args) {
for (int i = 0; i < objects.size(); i++) {
runMethodFromObj(objects.get(i), methodName, args );
}
}
public static void runMethodsFromClasses(ArrayList<Class> classes, String methodName, Object... args) {
for (int i = 0; i < classes.size(); i++) {
runMethodFromClass(classes.get(i), methodName, args);
}
}
public static void runMethodFromObj(Object obj, String methodName, Object... args) {
try {
Class[] params = new Class[args.length];
for (int i = 0; i < args.length; i++) {
params[i] = args[i].getClass();
}
Method method = obj.getClass().getDeclaredMethod(methodName, params);
method.setAccessible(true);
method.invoke(obj, args);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void runMethodFromClass(Class aClass, String methodName, Object... args) {
try {
Class[] params = new Class[args.length];
for (int i = 0; i < args.length; i++) {
params[i] = args[i].getClass();
}
Method method = aClass.getDeclaredMethod(methodName, params);
method.setAccessible(true);
method.invoke(aClass, args);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void changeFieldFromObj(Object obj, String fieldName, Object value) {
Class aClass = obj.getClass();
Field field;
try {
field = aClass.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static Object getFieldValFromObj(Object obj, String fieldName) {
Class aClass = obj.getClass();
Field field;
try {
field = aClass.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(obj);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}