Java code optimization

public static void doSome(String className, String methodName,String[]methodArgs){
    Class c = Class.forName(className);
    Object target = c.newInstance();
    if(methodArgs == null) {
        Method m = c.getMethod(methodName);
        m.invoke(target);
    } else if(methodArgs.length == 1) {
        Method m = c.getMethod(methodName,methodArgs[0].getClass());
        m.invoke(target,methodArgs[0]);
    } else if(methodArgs.length == 2) {
        Method m = c.getMethod(methodName,methodArgs[0].getClass(),methodArgs[1].getClass());
        m.invoke(target,methodArgs[0],methodArgs[1]);
    } else if(methodArgs.length == 3) {
        Method m = c.getMethod(methodName,methodArgs[0].getClass(),methodArgs[1].getClass(),methodArgs[2].getClass());
        m.invoke(target,methodArgs[0],methodArgs[1],methodArgs[2]);
    } else if(methodArgs.length == 4) {
        Method m = c.getMethod(methodName,methodArgs[0].getClass(),methodArgs[1].getClass(),methodArgs[2].getClass(),methodArgs[3].getClass());
        m.invoke(target,methodArgs[0],methodArgs[1],methodArgs[2],methodArgs[3]);
    } else if(methodArgs.length == 5) {
        Method m = c.getMethod(methodName,methodArgs[0].getClass(),methodArgs[1].getClass(),methodArgs[2].getClass(),methodArgs[3].getClass(),methodArgs[4].getClass());
        m.invoke(target,methodArgs[0],methodArgs[1],methodArgs[2],methodArgs[3],methodArgs[4]);
    }
}




The

code is like this, which is to implement a dynamic proxy through reflection. The parameter is the class name of the proxy class, the name of the method to be executed by the proxy class, and the parameter of the method executed by the proxy class. How can it be written better?

(that is, the parameter problem of the proxied class. When there are several parameters invoke, you need to add several parameters, and when you get method, you need to add several parameter class names)

Mar.18,2021

public static void doSome(String className, String methodName, Object[] methodArgs) throws Exception {
    Class c = Class.forName(className);
    Object target = c.newInstance();
    if (methodArgs == null) {
        methodArgs = new Object[0];
    }
    Class[] classes = new Class[methodArgs.length];
    for (int i = 0; i < methodArgs.length; iPP) {
        classes[i] = methodArgs[i].getClass();
    }
    Method method = c.getMethod(methodName, classes);
    method.invoke(target, methodArgs);
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b39577-411c8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b39577-411c8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?