java中反思的问题
SampleClass
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);
输出是:
一
class java.lang.String
类[C
INT
问题是下一次测试失败
Character testCharacterObjArray = new Character[]
Class<?> aClass = ClassUtils.getClass("[C", true);
Assert.assertEquals(testCharacterObjArray.getClass(), aClass);
ClassUtils来自org.apache.commons.lang3
寻找图书馆来获取&#34; [Ljava.lang.Character;&#34;而不是&#34; [C&#34;,因为看起来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);
}
}
答案 0 :(得分:2)
失败的原因:
Character[] testCharacterObjArray = new Character[]
Class<?> aClass = ClassUtils.getClass("[C", true);
Assert.assertSame(testCharacterObjArray.getClass(), aClass);
是&#34; [C&#34;表示char[]
而不是Character[]
。
您无法在ClassUtils.primitivesToWrappers()
上致电char[].class
的原因是char[]
不是原始类型!
如果要将基本数组类映射到数组包装类,则:
Class.isArray()
测试类型是否为数组Class.getComponentType()
获取基本类型Arrays.newInstance(baseType, ...)
创建数组,然后在其上调用getClass()
,创建映射基类型的数组类型。答案 1 :(得分:1)
我担心您的问题不仅存在于scope.openGallery = function (i) {
if (typeof i !== undefined) {
scope.index = i;
showImage(scope.index);
}
scope.opened = true;
document.body.style.overflow = 'hidden';
/////*******Here*******/////
$timeout(function() {
var calculatedWidth = calculateThumbsWidth();
scope.thumbs_width = calculatedWidth.width;
$thumbnails.css({ width: calculatedWidth.width + 'px' });
$thumbwrapper.css({ width: calculatedWidth.visible_width + 'px' });
smartScroll(scope.index);
});
};
。
同样primitivesToWrappers
并不像我期望的那样:
在Java中我可以打电话:
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));
但是在MethodUtils测试中:
new Question().foo("b", new char[] {'b'}, Integer.valueOf(2));
输出是(我希望m2和mb1不为空):
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);
我将您的问题类修改为:
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)