Basically, I have a few loadBlah() 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 spesific order is.
Aucun commentaire:
Enregistrer un commentaire