1

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! ;)

8
  • You mean you want to set and get the value of the fields using reflection, having only the name of the field? Oh, and BTW, welcome to SO! Commented Mar 5, 2013 at 15:48
  • You want to call the methods dynamically or write the methods dynamically? Does you class have example1() etc defined? Commented Mar 5, 2013 at 15:49
  • what do you mean by return method1? call method1 or is it a field Commented Mar 5, 2013 at 15:49
  • java.lang.Method? If the the type of the parameters is known (in your example empty), then using Class.getMethod() could do what you want Commented Mar 5, 2013 at 15:50
  • Yeah, sorry this wasn't very clear. Commented Mar 5, 2013 at 15:51

3 Answers 3

2

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...

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

1 Comment

I checked it, i don't know if it did the trick as well as your methods.
0

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.

Comments

0

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.

Comments

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.