views:

200

answers:

3

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  A: 

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

Bozho
The latter is probably just an obfuscation error, I think =)
mikek
That was a mistake when I copied the code to this forum it is just setAddress1
Appps
A: 

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

("set" + methodName)

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

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;
aioobe
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.
Appps