0

In Java is it possible to execute a method call given the class instance and the method call saved as a string?

I want to make a sequence of different method calls random so I was going to put the method parts in a list as Strings and shuffle them. How do I then use this string as part of a method call? e.g. myClass.temp below is being treated as a property. If I put the full myClaims. in the List as Objects it will try to execute them there.

List<String> methodList = new ArrayList<String>()
methodList.add("createMethodx(params...)")
methodList.add("createMethody(params...)")
methodList.add("insertMethodz(params...)")

String temp = methodList.get(0)
myClass.temp    //Execute the method.....doesn't work
user3668517
  • 43
  • 1
  • 3
  • what exactly is 'myClass.temp' supposed to be? – Stultuske May 23 '14 at 10:50
  • myClass.temp is the full method call e.g. myClass.createMethodx(param1,param2) – user3668517 May 23 '14 at 10:52
  • You can randomize a value from a `List` and use reflection to invoke a method corresponding to the given `String`. Although this is probably a very very bad idea design-wise. I would recommend you just randomize any argument given to a single method, whatever your purpose is. – Mena May 23 '14 at 10:52
  • what you have added in the list is a String. so how can you expect that it will execute – vikeng21 May 23 '14 at 10:53
  • possible duplicate of http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string – faisalbhagat May 23 '14 at 10:54
  • myClass.temp is not a method call. – Stultuske May 23 '14 at 10:55
  • I've a few different methods that I call that insert data into a database. Each method sets different columns so that's why I kept them separate. I wanted to randomise the order the data goes into the database as part of a testing project. – user3668517 May 23 '14 at 10:58
  • I added to the List as a string because I thought I might be able to somehow concatenate with the class instance afterwards. Fairly new to Java so never done this before.. – user3668517 May 23 '14 at 11:02

1 Answers1

0

That's a bad way of doing it. Instead, create a list of Runnable or Callable objects, shuffle that list, and then call the runnables:

final MyClass myClass = ...;
List<Runnable> actions = new ArrayList<>();
actions.add(new Runnable() {
    public void run() {
        myClass.createMethodx(...);
    }
});
actions.add(new Runnable() {
    public void run() {
        myClass.createMethody(...);
    }
});

Collections.shuffle(actions);

for (Runnable action : actions) {
    action.run();
}

With Java 8, that code would become

MyClass myClass = ...;
List<Runnable> actions = new ArrayList<>();
actions.add(() -> myClass.createMethodx(...));
actions.add(() -> myClass.createMethody(...));

Collections.shuffle(actions);

for (Runnable action : actions) {
    action.run();
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks very much JB - that works for me. That piece of code is great - I would never have been able to do anything like that by myself and it will be really useful. – user3668517 May 23 '14 at 12:17