use of org.apache.ratis.shell.cli.Command in project incubator-ratis by apache.
the class RatisShell method loadCommands.
/**
* Get instances of all subclasses of {@link Command} in a sub-package called "command" the given
* package.
*
* @param pkgName package prefix to look in
* @param classArgs type of args to instantiate the class
* @param objectArgs args to instantiate the class
* @return a mapping from command name to command instance
*/
public static Map<String, Command> loadCommands(String pkgName, Class[] classArgs, Object[] objectArgs) {
Map<String, Command> commandsMap = new HashMap<>();
Reflections reflections = new Reflections(pkgName);
for (Class<? extends Command> cls : reflections.getSubTypesOf(Command.class)) {
// Add commands from <pkgName>.command.*
if (cls.getPackage().getName().equals(pkgName + ".command") && !Modifier.isAbstract(cls.getModifiers())) {
// Only instantiate a concrete class
final Command cmd = ReflectionUtils.newInstance(cls, classArgs, objectArgs);
commandsMap.put(cmd.getCommandName(), cmd);
}
}
return commandsMap;
}
Aggregations