lundi 25 avril 2016

char[] to full quantified name of java class for method parameter

Problem with reflectiion in java

SampleClass

Class Question{
        public  int a   ( String a, char[] c,int b) { return  b; }    
}

Method to get method with name and parameters via reflection

 public Method getMethodWithParams(Class<?> klasa, String methodName, Class<?>[] params) throws
            SecurityException, NoSuchMethodException {
       Class<?>[] primitivesToWrappers =
                  ClassUtils.primitivesToWrappers(params);
        Method publicMethod = MethodUtils.getMatchingAccessibleMethod(klasa,
                                                                      methodName,
                                                                      primitivesToWrappers );
        System.out.println(publicMethod.toGenericString());

        return publicMethod;
    }

 private void printParams(Type[] types) throws ClassNotFoundException {

        for (Type genericParameterType : types) {
            System.out.println(genericParameterType.toString());

        }

    }

Main Program

Question cls = new Question();
    Class<?>[] paramString = new Class<?>[3];
            paramString[0] = String.class;
            paramString[1] = char[].class;
            paramString[2] = int.class;
     Method methodParams1 = getMethodParams(cls.getClass(),"a", paramString);
            System.out.println(methodParams1.getName());
            Type[] genericTypes = methodParams1.getParameterTypes();
            printParams(genericTypes);

output is:

a

class java.lang.String

class [C

int

Problem is that next test fails

Character testCharacterObjArray = new Character[]
Class<?> aClass = ClassUtils.getClass("[C", true);
Assert.assertEquals(testCharacterObjArray.getClass(), aClass);

ClassUtils is from org.apache.commons.lang3

Looking for a library to get "[Ljava.lang.Character;" instead of "[C", since it appears ClassUtils.primitivesToWrappers() fails.





Aucun commentaire:

Enregistrer un commentaire