user3489955
user3489955

Reputation: 3

reflection in java with method parameters

i want to do do a simple reflection with java

i want use multiple parameters with the methods?

my class

package reflection;

import java.util.Date;

public class SimpleClass {

    public Date datum1() {
        Date d = new Date();
        return d;
    }

    public Object datum2(String str) { 
        Date d = new Date();    
        return d;
    }

}

my test

package reflection;

import java.lang.reflect.Method;

public class ReflectionTest {

    public Object getMethodProperty(Object object, String... args) {

        Object value = null;

        try {
            Method m = object.getClass().getMethod(args[0], new Class[] {});
            value = m.invoke(object, new Object[] {});
        } catch (Exception e) {}
        return value;
    }

    public static void main(String[] args) {

        Reflection r = new Reflection();
        SimpleClass s = new SimpleClass();

        System.out.println(r.getMethodProperty(s, "datum1", "test"));
        System.out.println(r.getMethodProperty(s, "datum2", "test"));
    }


}

the first result is the actual date

the second result is null

how can i use parameters for the methods?

thanks icke

Upvotes: 0

Views: 88

Answers (2)

user3489955
user3489955

Reputation: 3

thanks martijn, with your help it's now:

class SimpleClass:

package reflection;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleClass {

    public Date datum1() {
        Date d = new Date();
        return d;
    }

    public String datum2(String str) {
        return str + new SimpleDateFormat("dd.MM.yyyy").format(new Date());
    }

}

class ReflectionTest:

package reflection;

import java.lang.reflect.Method;

public class ReflectionTest {

    public Object getMethodProperty(Object object, String methodName, Object... args) {

        Object value = null;

        Method[] methods = object.getClass().getDeclaredMethods();

        for (Method m : methods) {

            if (m.getName().equals(methodName)) {

                try {
                    value = m.invoke(object, args);
                    System.out.println(m.getName());
                } catch (Exception e) {
                }
            }
        }
        return value;
    }

    public static void main(String[] args) {

        ReflectionTest r = new ReflectionTest();
        SimpleClass s = new SimpleClass();
        Object[] o1 = new Object[] { new String("datum: ")};

        System.out.println(r.getMethodProperty(s, "datum1"));
        System.out.println(r.getMethodProperty(s, "datum2", o1));
    }
}

Upvotes: 0

Martijn Courteaux
Martijn Courteaux

Reputation: 68907

The datum2() method takes a String. In order to find that method you should add that paramater in your reflection call:

SimpleClass s = new SimpleClass();
Method m = s.getClass().getMethod("datum2", new Class[]{String.class});
m.invoke(s, new Object[]{"foo"});

Upvotes: 1

Related Questions