0

Basically, I have a few loadSomething() void methods, and I need to execute them all. Since I don't want to just have:

//not actual names
loadMethod1();
loadMethod2();

etc.

I decided to reflect all the methods. Here is what I have.

Arrays.stream(getClass().getDeclaredMethods())
    .filter(method -> method.getName().startsWith("load"))
    .forEach(method -> {
        try {
            method.invoke(instance);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    });

I don't know if that is good or bad code, if it's not the best let me know.

The problem is, the methods have to be invoked in a certain order. However I read in the Javadocs that the getDeclaredMethods() method returns the methods in no particular order. So I don't know how to do this. I was thinking about maybe renaming them to load1, load2, load3 etc. because then I could sort them in the correct order.

Hence, I want to know what the best way to invoke the methods in a specific order is.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Arraying
  • 169
  • 2
  • 3
  • 10
  • if you werent using reflection, what would be the order to invoke those meth? alphabetically???? – ΦXocę 웃 Пepeúpa ツ Jan 17 '17 at 17:38
  • 4
    I would avoid doing like that. Your code becomes hard to read (and with reflection the app becomes slower). Better "waste" 20 lines of code with 20 **clear** method calls in the right order instead of having less lines which you need to read 10 times to understand what is going on. If you don't want to place them in the middle of other code, you can create a method named `loadAll` which calls all the `load*` methods in the right order. You can then simply call `loadAll()` to load everything your app needs to load. – BackSlash Jan 17 '17 at 17:39
  • @ΦXocę웃Пepeúpaツ Nope, basically first I'd execute loadInstances() which makes new instances required for loadSql() etc. hence I need them in a spesific order, if that makes sense. – Arraying Jan 17 '17 at 17:39
  • @Arraying *"I don't know if that is good or bad code, if it's not the best let me know."* using reflection for no reasion (other than lasyness) is bad code. – Timothy Truckle Jan 17 '17 at 17:42
  • Thanks for the tip BackSlash, and @TimothyTruckleI guess you could consider it lazy, but I'll probably end up adding more load methods and knowing me I'll forget to execute them, but thanks for the advice, will keep it in mind next time – Arraying Jan 17 '17 at 17:44
  • Why not simply put the name of your methods in the right order into a array, then use this array to invoke your methods? – Nicolas Filotto Jan 17 '17 at 17:44
  • 1
    @Arraying Then maybe you need to do some serious work on your concentration while programming (or working in general). Forgetting leads to bad products, and using reflection because "you'll forget" leads to even worse products. – BackSlash Jan 17 '17 at 17:48
  • I agree with the others. Reflection has a very bad smell here. If these methods are similar in some way, you should try to unify them. Otherwise, there's no reason not to specify each one individually. But to answer your question, you should be able to annotate your methods with an index so you can sort them manually. – shmosel Jan 17 '17 at 17:51
  • Maybe use initializer blocks. They run in the order they're declared in the class when the class gets constructed. In this way you can just construct your class, and all of its blocks get run automatically. See http://stackoverflow.com/questions/3987428/what-is-an-initialization-block – john16384 Jan 17 '17 at 18:17
  • 2
    Why so many `load()` methods in the same class? Is the class doing too much? Perhaps there should be a `List` and call .load() on each. – Andrew S Jan 17 '17 at 18:31

0 Answers0