Fabinout
Fabinout

Reputation: 878

Dynamically invoke methods

I need a little bit of help here, tell me if you have any idea how to solve my problem.

Let's say i have this class :

public testClass{

    public int example1(){
    return 2;
    }
    public int example2(){
    return 0;
    }
    public int example3(){
    return 456;
    }
}

I want a method which will do the same thing that this method, but in a dynamic way

public int methodeSwitch(int a){
   if (a==1){return method1;}
   if (a==2){return method2;}
   if (a==3){return method3;}
   return null;
}

My problem is that I have a huge class (dto) with 50+ fields, so i'd like to use getters and setters depending on the fields that i use at the moment (so yeah, dynamically). I know how to access fields (with java.lang.Field, wouuu), but i have no clue on how I could cast a method by its name (which will be created dynamically).

Just giving me a hint would be amazing!

Thanks Fabien

EDIT: to clarify, I have to write a method who basically use every setters of my class, so if I could use something like

useMethod("set"+fields[i]+"();");

That would be helpful and prevent me from writing dozens of lines of code.

Thanks again for the ones helping! ;)

Upvotes: 1

Views: 251

Answers (3)

Serkan Arıkuşu
Serkan Arıkuşu

Reputation: 5619

Using reflection, you may try something like this.

public int methodeSwitch(int a)  {
    Map<Integer,String> methods = new HashMap<Integer,String>();
    methods.put(1, "example1");
    methods.put(2, "example2");
    methods.put(3, "example3");

    java.lang.reflect.Method method;
    try {
        method = this.getClass().getMethod(methods.get(a));
        return (Integer) method.invoke(this);
    } catch(Exception ex){//lots of exception to catch}
    return 0;
}

This is just a proof of concept. Of course you should initialize your dynamic methods in another place (static initialize), and check for methods.get(a) if it is not in the valid range etc.

Upvotes: 0

Boris the Spider
Boris the Spider

Reputation: 61148

You need to use reflection to get the declared method from you class. I have assumed that these methods live in the class on which you want to invoke the getter/setter and that fields is a String[] of field names.

private Object callGet(final String fieldName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    final Method method = getClass().getDeclaredMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
    return method.invoke(this, (Object[]) null);
}

private void callSet(final String fieldName, final Object valueToSet) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    final Method method = getClass().getDeclaredMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), new Class[]{valueToSet.getClass()});
    method.invoke(this, new Object[]{valueToSet});
}

You could also have a look at Commons BeansUtil which is a library designed for doing exactly this...

Upvotes: 2

krampstudio
krampstudio

Reputation: 3621

You can use the reflection API and Method.invoke :

http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html#invoke%28java.lang.Object,%20java.lang.Object...%29

event though I'm convinced that's a not a good practice to do that.

Upvotes: 0

Related Questions