mardi 10 octobre 2017

Reflection: How to create a Class[] with method parameter of double[] [duplicate]

This question already has an answer here:

I have a method:

 static double sum(double[] out, int numberVerticies) {}

I am trying to unit test it using reflection. I keep getting NoSuchMethodException.

I think it is because the args I am passing in are not matching the method.

here is my test method:

 @Test
void sum(){
    double[] out ={1,2,3,4};
    int numberVerticies = 4;
    Class[] args = new Class[]{Array.class,Integer.TYPE};
    Object[] params = new Object[]{out,numberVerticies};

    try {
        Method sum = reflectClass.getDeclaredMethod("sum", args);
        sum.setAccessible(true);


        double returnValue = (double) sum.invoke(pageRank,params);

    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

I think it is this line that is causing the problem:

        Class[] args = new Class[]{Array.class,Integer.TYPE};

What type or class is a double[].





Aucun commentaire:

Enregistrer un commentaire