解决方案 »
- try catch finally问题
- JDK 如何自动升级?
- short s1 = 1; s1 = s1 + 1; (s1+1运算结果是int型,需要强制转换类型)
- 怎样将byte[]转成string呢?
- SWT的多线程问题
- 不是逼不得已我不会再次发帖求人 求求你们了嘛 行行好
- 讨论几个关于连接数据库的问题
- 关于多网卡主机的serverSocket的问题
- 有没有在tomcat中成功配置https的?救救我吧,已经几天了!!!!!
- 为什么在JB6中编译并运行正常的程序,用SUN JDK1.3编译后运行却出错
- 新手关于输入日期,函数Date的问题
- [技术讨论]谈谈Android开发中的Java。
这里的参数应该是 (java.lang.String.class,int.class),它是获取参数的类型。
你在调用方法的时候才是用new Object[]{"sss", 111}传具体的值。
我已经对参数进行了getClass的获型,只是因为原方法是int类型,我获型后是integer类型报错,我是问这个如何解决
我已经对参数进行了getClass的获型,只是因为原方法是int类型,我获型后是integer类型报错,我是问这个如何解决
我已经对参数进行了getClass的获型,只是因为原方法是int类型,我获型后是integer类型报错,我是问这个如何解决
个人感觉原因是,传入的是Object数组,所以里面装的都是对象. int -> Integer不知道对不对.- -.
这样声明,编译器肯定报错
首先有一个方法如下:public String run(String a,int b)
{
...
}
然后我调用如下方法反射获得上面的“run”方法:getCacheData("run",new Object[]{"sss", 111});
此方法内部为:
getCacheData(String methodName,Object[] parameters)
{
...
Class oc = o instanceof Class ? (Class)o : o.getClass();
Object returnValue = invoke(o, methodName, parameters);
}然后进入自定义的invoke方法,内部结构为:
private Object invoke(Object o, String methodName,Object[] parameters){
...
try{
Class[] pcs = null;
if (parameters != null){
pcs = new Class[parameters.length];
for (int i = 0; i < parameters.length; i++){
pcs[i] = parameters[i].getClass();
}
}
Class oc = o instanceof Class ? (Class)o : o.getClass();
Method m = matchMethod(oc, methodName, pcs);
}这样进入了matchMethod方法,内部调用了如下方法:
Method method = oc.getDeclaredMethod(methodName, pcs);此方法无法获得"run"方法,调试显示pcs中的第二个参数类型为integer而不是int。
换成 pcs[i] = parameters[i].class;
你那样得到的很多基本类型都没有getClass的,理由参照上面
method.invoke(obj, new Object[] {"aaa", 5})
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;/**
* desc:
* XXX<br>
* ----------------------------------------------------------------------------
* ver. date who what
* ----------------------------------------------------------------------------
* 0.0.1 2011-9-20 leisore add
* ----------------------------------------------------------------------------
*//**
* Clz2Book
*
* @author leisore
* @since version 0.0.1
*/
public class MatchedMethodTest { public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException { MatchedMethodTest clz2 = new MatchedMethodTest();
Object[] parameters = new Object[] { 1, "leisore", new Date() };
Method findMatchedMethod = findMatchedMethod(clz2, "print", parameters);
if (findMatchedMethod != null) {
findMatchedMethod.invoke(clz2, parameters);
}
} public void print(int i, String s, Object o) {
System.out.println(String.format("i=%d s=%s o=%s", i, s, o));
} public static Method findMatchedMethod(Object o, String methodName,
Object[] parameters) {
Method matchedMethod = null;
Method[] declaredMethods = o.getClass().getDeclaredMethods();
for (int j = 0; j < declaredMethods.length; j++) {
Method decMethod = declaredMethods[j];
Class<?>[] parameterTypes = decMethod.getParameterTypes();
if ((parameterTypes.length == parameters.length)) {
boolean matched = true;
for (int i = 0; i < parameterTypes.length; i++) {
Class<?> paramClz = parameterTypes[i];
if (!match0(paramClz, parameters[i])) {
matched = false;
break;
}
}
if (matched) {
matchedMethod = decMethod;
break;
}
}
} return matchedMethod;
} private static Map<Class<?>, Class<?>> prim2wrapperMap = new HashMap<Class<?>, Class<?>>(
8, 1.0f) {
{
put(Boolean.TYPE, Boolean.class);
put(Byte.TYPE, Byte.class);
put(Character.TYPE, Character.class);
put(Short.TYPE, Short.class);
put(Integer.TYPE, Integer.class);
put(Long.TYPE, Long.class);
put(Float.TYPE, Float.class);
put(Double.TYPE, Double.class);
}
}; private static boolean match0(Class<?> decParamClz, Object realParamObj) {
if (realParamObj == null) {
return true;
} else if (decParamClz.isPrimitive()) {
return prim2wrapperMap.get(decParamClz) == realParamObj.getClass();
} else if (decParamClz.isArray()) {
return realParamObj.getClass().isArray()
&& (decParamClz.getComponentType() == realParamObj
.getClass().getComponentType());
} else {
return decParamClz.isInstance(realParamObj);
}
}
}
/**
输出:
i=1 s=leisore o=Tue Sep 20 19:26:35 CST 2011
*/没有考虑的很仔细,比如对于一个long参,使用int是可以的等等