使用 JAVA 反射时出现 NoSuchMethodException

发布于 2024-08-31 01:27:13 字数 1068 浏览 10 评论 0原文

您好,我正在尝试使用反射来调用方法并更新该方法的 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;

Hi I'm trying to use reflection to invoke a method and update the setter value of that method. But I'm getting NoSuchMethodException while ivoking that method.
I've updated the code. I'm so sorry for the errors in the previous code. I've refractored the code. The exception occurs when the setMethod of the class accepts primitive type arguments.

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幽蝶幻影 2024-09-07 01:27:13

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

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

What I see is that you pass setAddress1 and concatenate it with set, thus getting setsetAddress1. Either pass the property name and capitalize it, or remove the set from the concatenation.

Also, the code you have provided won't compile. You can't have a variable called class

隱形的亼 2024-09-07 01:27:13

一次大胆的尝试,但您不是想获取 setsetAddress1 方法吗?

(“设置”+方法名称)

A shot from the hip, but aren't you trying to get the method setsetAddress1?

("set" + methodName)

箹锭⒈辈孓 2024-09-07 01:27:13

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

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

The code below works. You had two bugs (except for the syntactical class-name error):

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. As pointed out by the other authors, you added "set" two times to the method name
  2. You tried to pass a String String data="TestData"; as argument, even though you specified that the argument should be of type Double: doubleArrayParamTypes[ 0 ] = Double.class;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文