【问题标题】:NoSuchMethodException while using JAVA Reflection使用 JAVA 反射时出现 NoSuchMethodException
【发布时间】:2010-05-05 09:57:49
【问题描述】:

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

【问题讨论】:

  • -1 用于发布错误代码并在可调试性之外进行混淆。

标签: java reflection nosuchmethoderror


【解决方案1】:

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

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

【讨论】:

  • 当我将代码复制到这个论坛时,这是一个错误,它只是 setAddress1
【解决方案2】:

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

("set" + methodName)

【讨论】:

  • 当我将代码复制到这个论坛时,这是一个错误,它只是 setAddress1
  • 那么为什么要运行“set”+方法名?为什么不只是隐式发送方法名称,你在你的示例代码中做了。
【解决方案3】:

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

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. 试图将String String data="TestData"; 作为参数传递,即使您指定参数的类型应为DoubledoubleArrayParamTypes[ 0 ] = Double.class;

【讨论】:

  • 非常感谢您的回复。我的方法 performMapping 必须接受 Object 类型的数据。异常 setAddress1(java.lang.Double) 不存在。但实际上那个方法是有的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
相关资源
最近更新 更多