您好我正在尝试使用反射来调用方法并更新该方法的设置器值。但是在调用该方法时我得到了 NoSuchMethodException 。我已经更新了代码。对于前面代码中的错误,我深表歉意。我已经重构了代码。当类的 setMethod 接受原始类型参数时会发生异常。
private static Object performMapping( Class voClass, Class[] clazz, Object voObject, Object data,String fieldType ){
voClass.getMethod( "set" + fieldType, clazz ).invoke( voObject, data );
return voObject;
}
private static Object mapField(ResultSet rs){
Class voClass=Class.forName( "com.test.Test" );
Object voObject = voClass.newInstance();
Class[] doubleArrayParamTypes = new Class[ 1 ];
doubleArrayParamTypes[ 0 ] = Double.class;
voObject = performMapping( voClass, doubleArrayParamTypes, voObject, rs.getDouble(fieldType.getColumn()), "Mark" );
}
/* This is my Class. I need to set the Mark. But it is primitive double. Is it possible to set the mark using the above code? */
public class Test{
private double mark;
public double getMark() {
return mark;
}
public void setMark(double mark) {
this.mark = mark;