dimanche 10 juin 2018

How to convert a String which looks like an Array into an actual Object Array?

Since

public static String requestMethodExecution(String objectName, String className, String methodName, Object...
            params) {
        return String.format("%s,%s,%s,%s", objectName, className, methodName, Arrays.toString(params));
    }

returns a String, and if you would, for example, call the method like this:

requestMethodExecution("foo","bar","fooBar",2.0,3.0,"Hello");

You'd get a String like this: foo,bar,fooBar,[2.0,3.0,Hello]

I would love to iterate over that Array, but I can't since it is a String.

Reason behind this is this method: (I just started with reflection, so I do not know how else to do it)

 public static Class[] getParameterType(String ...params) throws ClassNotFoundException {
            Class[] paramTypes = new Class[params.length];
            for(int i=0; i<params.length;i++){
                Class paramClass = Class.forName(params[i]);
                if (paramClass == Double.class) {
                    paramTypes[i] = (double.class);
                } else if (paramClass == Integer.class) {
                    paramTypes[i] = (int.class);
                } else {
                    paramTypes[i] = paramClass;
                }
            }
            return paramTypes;
    }





Aucun commentaire:

Enregistrer un commentaire