Skip to content

Instantly share code, notes, and snippets.

@jaen
Created January 12, 2014 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaen/8391394 to your computer and use it in GitHub Desktop.
Save jaen/8391394 to your computer and use it in GitHub Desktop.
Reflections reflections = new Reflections("pl.edu.polsl.labjava");
commandMap = new TreeMap<String, Class<? extends Command>>();
Set<Class<? extends Command>> commandClasses = reflections.getSubTypesOf(Command.class);
commandClasses.stream()
.map((Function<Class<? extends Command>, KeyValue<String, Class<? extends Command>>>) (commandClass -> {
String commandName;
try {
commandName = (String) commandClass.getField("COMMAND_NAME").get(null);
} catch (IllegalAccessException | NoSuchFieldException e) {
System.err.format("Class \"%s\" does not hace a COMMAND_NAME field. Skipping.", commandClass);
return null;
}
return new KeyValue<>(commandName, commandClass);
}))
.filter(pair -> pair != null)
.forEachOrdered(pair -> commandMap.put(pair.getKey(), pair.getValue()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment