0

I am writing a small processor. To store the functions, I am using a HashMap<String, Command> commands, which holds classes that implement the interface command. Those classes, along with the interface, are in their own package Commands

Currently, I am using a bunch of commands.put(...) instructions to fill the HashMap.

I would like to make the filling of the HashMap iterative, so that I can just create or delete classes that implement the interface command without having to worry about updating the HashMap. Is something like this possible without too much hassle?

  • Use a loop in your filling function that checks at each iteration if the corresponding `Object` implements `Command`: `if(x instanceof Command)`, do what you want with it and then add it to the `HashMap` – Paul Lemarchand May 10 '19 at 13:59
  • I would like to do that, but how do I iterate over classes that implement an inteface / classes in a package? – UmBottesWillen May 10 '19 at 14:00
  • 1
    Apparently you can use `The Reflections Library`. See this answer: https://stackoverflow.com/a/520339/6252134 – Paul Lemarchand May 10 '19 at 14:03

0 Answers0