可恶的基本类型和包装类型

今天写程序,遇到一个通过调用类的方法改变值的案例,就写了个一个反射方法.

类为A,方法为public void setXXX(int i,float f);

调用方法为

			Class<?> clazz = A.getClass();
			Class<?>[] pts = new Class<?>[newArags.length];
			int index = 0;
			for (Object o : this.newArags) {
				pts[index++] = o.getClass();
			}
			try {
				Method m = clazz.getMethod(this.methodName, pts);
				m.invoke(this.owner, this.oldArgs);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}

 其中oldArgs为{1,2.1f},newArgs为{1,1.0f},想法是好,但是结果很是无情:

java.lang.NoSuchMethodException: A.setXXX(java.lang.Integer, java.lang.Float),

难道只能手动的把包装类型转换成基本类型吗?

			for (Object o : this.newArags) {
				Class<?> c = o.getClass();
				if (c == Integer.class) {
					c = int.class;
				} else if(c == Float.class) {
					c = float.class;
				} else if (c == Double.class) {
					c = double.class;
				}
			}

 有知道更好办法的童鞋请告知我,不胜感激

你可能感兴趣的:(java,SVN,jboss,jbpm,C#)