char[] 到 java class 方法参数的完整量化名称

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

java

中的反射问题

样本类

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

通过反射获取方法名和参数的方法

 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());

        }

    }

主程序

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

输出为:

a

class java.lang.String

class [C

int

问题是下一次测试失败

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

ClassUtils 来自 org.apache.commons.lang3

正在寻找图书馆以获取“[Ljava.lang.Character;”而不是“[C”,因为它出现 ClassUtils.primitivesToWrappers() 失败。

基于stephen的解决方案:

public Class<?> convertStringToClass(String str) throws
        ClassNotFoundException {
    Class<?> aClass = ClassUtils.getClass(str, true);

    if (aClass.isArray()) {

        Class<?> primitiveToWrapper =
                 ClassUtils.primitiveToWrapper(aClass.getComponentType());
        Object newInstance = Array.newInstance(primitiveToWrapper, 0);
        System.out.println("****" + newInstance.getClass().getName());
        return ClassUtils.
                getClass(newInstance.getClass().getName(), true);
    }
    else {
        return ClassUtils.primitiveToWrapper(aClass);
    }

}

失败的原因:

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

是“[C”表示 char[] 而不是 Character[]

你不能在 char[].class 上调用 ClassUtils.primitivesToWrappers() 的原因是 char[] 不是原始类型!

如果要将原始数组 class 映射到包装器数组 class,则:

  1. 使用Class.isArray()测试类型是否为数组
  2. 使用Class.getComponentType()获取基类型
  3. 如果基类型是基本类型,则映射它。
  4. 使用Arrays.newInstance(baseType, ...)创建数组,然后在其上调用getClass(),创建映射基类型的数组类型。

恐怕你的问题不仅仅在primitivesToWrappers.

另外 getMatchingAccessibleMethod 并不像我预期的那样:

在Java我可以调用:

    new Question().foo("a", new char[] {'a'}, 1);
    new Question().foo("b", new char[] {'b'}, Integer.valueOf(2));

    new Question().bar("c", new char[] {'c'}, 3);
    new Question().bar("d", new char[] {'d'}, Integer.valueOf(4));

这会导致 Java 中的编译错误:

    new Question().foo("b", new char[] {'b'}, Integer.valueOf(2));

但在 MethodUtils 测试中:

    Method m1 = MethodUtils.getMatchingAccessibleMethod(Question.class, "foo", String.class, char[].class, int.class);
    System.out.println("m1:" + m1);
    Method m2 = MethodUtils.getMatchingAccessibleMethod(Question.class, "foo", String.class, char[].class, Integer.class);
    System.out.println("m2: " + m2);
    Method m3 = MethodUtils.getMatchingAccessibleMethod(Question.class, "foo", String.class, Character[].class, Integer.class);
    System.out.println("m3: " + m3);

    Method mb1 = MethodUtils.getMatchingAccessibleMethod(Question.class, "bar", String.class, char[].class, int.class);
    System.out.println("mb1: " +mb1);
    Method mb2 = MethodUtils.getMatchingAccessibleMethod(Question.class, "bar", String.class, char[].class, Integer.class);
    System.out.println("mb2:" + mb2);

输出是(我希望 m2 和 mb1 不为空):

m1: public int LangTest$Question.foo(java.lang.String,char[],int)
m2: null
m3: null
mb1: null
mb2: public int LangTest$Question.bar(java.lang.String,char[],java.lang.Integer)

我将您的问题 class 修改为:

static class Question {
    public  int foo( String a, char[] c,int b) { return  b; }
    public  int bar( String a, char[] c,Integer b) { return  b; }
}