0

您好我正在尝试使用反射来调用方法并更新该方法的设置器值。但是在调用该方法时我得到了 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;
4

3 回答 3

1

我看到的是你通过setAddress1并将它连接起来set,从而得到setsetAddress1。传递属性名称并将其大写,或set从串联中删除 。

此外,您提供的代码不会编译。你不能有一个名为的变量class

于 2010-05-05T10:00:31.127 回答
0

从臀部射门,但你不是想得到这个方法setsetAddress1吗?

( "set" + methodName)

于 2010-05-05T10:02:45.457 回答
0

下面的代码有效。你有两个错误(除了语法类名错误):

package com.test;

import java.io.IOException;
import java.lang.reflect.*;
import java.util.Arrays;

public class Test {

    Test() throws ClassNotFoundException, InstantiationException,
            IllegalAccessException, IllegalArgumentException,
            SecurityException, InvocationTargetException, NoSuchMethodException {

    }

    private void m() throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException, InstantiationException {

        Class[] doubleArrayParamTypes = new Class[1];
        doubleArrayParamTypes[0] = Double.class;
        Class clazz = Class.forName("com.test.Test");
        Object voObject = clazz.newInstance();
        Double data = 5.0;

        performMapping(clazz, "Address1", doubleArrayParamTypes, voObject, data);

    }

    public static void main(String... args) throws IOException,
            ClassNotFoundException, InstantiationException,
            IllegalAccessException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException {
        new Test().m();
    }


    /* Reflection to set the data */
    @SuppressWarnings("unchecked")
    private void performMapping(Class clazz1, String methodName, Class[] clazz,
                                Object voObject, Double data)
            throws IllegalArgumentException, SecurityException,
            IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {
        for (Method m : clazz1.getMethods()) {
            System.out.println(m.getName()+ " " + Arrays.toString(m.getParameterTypes()));
        }
        clazz1.getMethod("set" + methodName, clazz).invoke(voObject, data);
    }

    public void setAddress1(Double arg) {
        System.out.println(arg);
    }
}
  1. 正如其他作者所指出的,您在方法名称中添加了两次“set”
  2. 尝试将 aString String data="TestData";作为参数传递,即使您指定参数应该是 typeDoubledoubleArrayParamTypes[ 0 ] = Double.class;
于 2010-05-05T10:12:01.880 回答