Want to support development? Maybe get a nice server to do your own development on? Check out BuyVM or Vultr using our affiliate links! New users get $50 of free credit with Vultr, and BuyVM offers affordable servers with affordable, true DDoS filtering. You can also support us on Patreon.
Submitted on November 29, 2014 at 02:59 AM

Section 1 (Java)

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;
    }
}