private static <T extends Command> List<String> getCommandNames(Class<T> tClass) { Parameters paramAnn = tClass.getAnnotation(Parameters.class); if (paramAnn.commandNames().length == 0) { String simpleName = tClass.getClass().getSimpleName(); simpleName = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1, simpleName.length()); return Collections.singletonList(simpleName); } return Arrays.asList(paramAnn.commandNames()); }
@NotNull @Override public String getDescription() { return Optional.ofNullable(this.getClass().getAnnotation(Parameters.class)) .map(Parameters::commandDescription) .orElse(""); }
protected void appendPullRequestComment(StringBuilder builder) { builder.append(COMMAND_COMMENT_INDENT); Parameters annotation = getClass().getAnnotation(Parameters.class); if (annotation != null) { String[] commandNames = annotation.commandNames(); if (commandNames != null && commandNames.length > 0) { builder.append(commandNames[0]); } } appendPullRequestCommentArguments(builder); builder.append("\n"); }
public static String getCommandDescription( JCommander jc) { Parameters parameters = jc.getObjects().get(0).getClass().getAnnotation(Parameters.class); if (parameters == null) { return null; } return parameters.commandDescription(); }
@Test public void test_commandMap_allCommandsHaveDescriptions() throws Exception { for (Map.Entry<String, ? extends Class<? extends Command>> commandEntry : RegistryTool.COMMAND_MAP.entrySet()) { Parameters parameters = commandEntry.getValue().getAnnotation(Parameters.class); assertThat(parameters).isNotNull(); assertThat(parameters.commandDescription()).isNotEmpty(); } }
/** * Returns a list of all annotated command classes. * * @return a list of all command classes or an empty {@link List}, if none present */ public List<Class<?>> getCommandClasses() { List<Class<?>> commandClasses = new LinkedList<Class<?>>(); for (Class<?> innerClass : SongbirdDatabaseToolsCli.class.getDeclaredClasses()) { if (innerClass.isAnnotationPresent(Parameters.class)) { commandClasses.add(innerClass); } } return commandClasses; }
/** * Creates the command instances and adds them to {@link #commander}. * * @return a map mapping command string to command instance (command string in <b>lower case letters</b>) */ private Map<String, Object> createCommands() { Map<String, Object> commandString2Instance = new HashMap<String, Object>(); for (Class<?> innerClass : getCommandClasses()) { if (innerClass.isAnnotationPresent(Parameters.class) && Object.class.isAssignableFrom(innerClass)) { Object command = null; try { /* * Create an instance of the inner class relating to this instance of the outer class */ command = innerClass.getDeclaredConstructor(new Class[] { this.getClass() }).newInstance( new Object[] { this }); } catch (Exception e) { // NOSONAR: Method call preserves exception! printErrorThrowException(e); } String commandStr = innerClass.getSimpleName(); String commandStrLower = commandStr.toLowerCase(); String commandStrUpper = commandStr.toUpperCase(); commandString2Instance.put(commandStrLower, command); // Add command, define lower and upper version as aliases commander.addCommand(commandStr, command, commandStrLower, commandStrUpper); } } return commandString2Instance; }
private static <T extends Command> String getDescription(Class<T> tClass) { return Optional.ofNullable(tClass.getAnnotation(Parameters.class)) .map(Parameters::commandDescription) .orElse(""); }
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { Map<String, TypeMirror> types = new HashMap<>(); TypeMirror expectedInterfaceType = processingEnv.getElementUtils().getTypeElement(Command.class.getCanonicalName()) .asType(); PackageElement pkg = null; for (TypeElement a : annotations) { for (Element element : roundEnv.getElementsAnnotatedWith(a)) { Parameters parameters = Objects.requireNonNull(element.getAnnotation(Parameters.class)); TypeMirror typeMirror = element.asType(); if (pkg == null) { pkg = processingEnv.getElementUtils().getPackageOf(element); } if (!processingEnv.getTypeUtils().isAssignable(typeMirror, expectedInterfaceType)) { processingEnv.getMessager() .printMessage(Diagnostic.Kind.ERROR, String.format("Class %s does not implement %s", element.getSimpleName(), Command.class.getCanonicalName())); } for (String commandName : extractCommandNames(element.getSimpleName(), parameters.commandNames())) { types.put(commandName, typeMirror); } } } if (!types.isEmpty()) { try { writeFile(types, pkg); } catch (IOException e) { processingEnv.getMessager() .printMessage(Diagnostic.Kind.ERROR, "Unable to process jeesh annotations: " + e.getMessage()); } } return false; }
/** * Gets the {@link Parameters} annotation from the command. * * @return the {@link Parameters} annotation */ Parameters getParameters() { Parameters parameters = this.getClass().getAnnotation(Parameters.class); Preconditions.checkNotNull(parameters, "There must be @Parameters on the Command class: " + this.getClass()); return parameters; }
private JsonObject parseParameters() { // get the high level attributes from the annotation for the operation // (name and description) final JsonObject op_json = new JsonObject(); final String opId = operation.getId(); op_json.addProperty( "operationId", opId); final Parameters command_annotation = this.operation.getClass().getAnnotation( Parameters.class); op_json.addProperty( "description", command_annotation.commandDescription()); // iterate over the parameters for this operation and add them to the // json object final JsonArray fields_obj = new JsonArray(); final List<RestField<?>> fields = RestFieldFactory.createRestFields(operation.getClass()); for (final RestField<?> field : fields) { final JsonObject[] field_obj_array = processField( field.getName(), field.getType(), field.getDescription(), field.isRequired()); if (field_obj_array != null) { for (final JsonObject field_obj : field_obj_array) { fields_obj.add(field_obj); } } } op_json.add( "parameters", fields_obj); // build up the response codes for this operation final JsonObject resp_json = new JsonObject(); JsonObject codes_json = new JsonObject(); codes_json.addProperty( "description", "success"); resp_json.add( "200", codes_json); codes_json = new JsonObject(); codes_json.addProperty( "description", "route not found"); resp_json.add( "404", codes_json); codes_json = new JsonObject(); codes_json.addProperty( "description", "invalid or null parameter"); resp_json.add( "500", codes_json); op_json.add( "responses", resp_json); return op_json; }
public String getDescription() { Parameters params = supplier.get().getClass().getAnnotation(Parameters.class); return params == null ? "(No description)" : params.commandDescription(); }
public CliTool() { if (!getClass().isAnnotationPresent(Parameters.class)) { throw new RuntimeException(); } }
/** * Gets the names of this command (should be long name first followed by * short name). * * @return list of command names */ public List<String> getNames() { Parameters parameters = getParameters(); String[] commandNames = parameters.commandNames(); return Arrays.asList(commandNames); }