io.github.laskowski.shell.script.CommandsMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shell-commander Show documentation
Show all versions of shell-commander Show documentation
Library to launch .bat and .sh scripts of your choice with different configurations. Allows you to read tasks and services output from terminal
package io.github.laskowski.shell.script;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class CommandsMapper {
private final Class> clazz;
private final Object object;
public CommandsMapper(Class> clazz) {
this.clazz = clazz;
object = createNewInstance();
}
public List getCommands() {
List commandEntities = new ArrayList<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Command command = field.getAnnotation(Command.class);
if (command != null) {
Object variableValue;
String variableValueAsString;
try {
variableValue = field.get(object);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
Class> fieldType = field.getType();
if (fieldType.isAssignableFrom(String.class)) {
variableValueAsString = (String) variableValue;
} else if (fieldType.isAssignableFrom(Optional.class)) {
Optional optional = (Optional) variableValue;
if (optional.isPresent()) {
variableValueAsString = optional.get();
} else {
continue;
}
} else {
throw new IllegalArgumentException(String.format("Field: [%s] has unsupported type: [%s]", field.getName(), fieldType.toString()));
}
commandEntities.add(new CommandEntity() {
@Override
public CommandType getCommandType() {
return command.type();
}
@Override
public String getAnnotationValue() {
return command.value();
}
@Override
public String getVariableValue() {
return variableValueAsString;
}
});
}
field.setAccessible(false);
}
return commandEntities;
}
private Object createNewInstance() {
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}