0

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;
1
  • 2
    -1 for posting faulty code and obfuscating beyond debugability. Commented May 5, 2010 at 12:29

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That was a mistake when I copied the code to this forum it is just setAddress1
0

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

("set" + methodName)

2 Comments

That was a mistake when I copied the code to this forum it is just setAddress1
So why do you run "set" + methodname? Why not just send in the method name implicitly, you did you in your example code.
0

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;

1 Comment

Thank you very much for your reply. My method performMapping has to accept data of type Object. The exception setAddress1(java.lang.Double) is not there. But actually that method is there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.