gnodet-bot commented on code in PR #1273:
URL:
https://github.com/apache/maven-plugin-tools/pull/1273#discussion_r4567288558
##########
maven-plugin-annotations/src/main/java/org/apache/maven/plugins/annotations/Parameter.java:
##########
@@ -62,14 +62,29 @@
String property() default "";
/**
- * parameter default value, may contain <code>${...}</code> expressions
which will be interpreted at
+ * Parameter default value, may contain <code>${...}</code> expressions
which will be interpreted at
* inject time: see
* <a
href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">
* PluginParameterExpressionEvaluator</a>.
+ * The type conversion from String into the target type is described in <a
href="/guides/mini/guide-configuring-plugins.html#Configuring_Parameters">Configuring
Parameters</a>.
+ * Cannot be specified together with {@link #defaultValues()}.
* @return the default value
+ * @see #defaultValues()
*/
String defaultValue() default "";
+ /**
+ * Default values concatenated with commas in the generated plugin
descriptor.
+ * Each String value has the format outlined in {@link #defaultValue()}.
+ * Multiple values are only supported for collection or array parameters.
An empty array specifies no default.
Review Comment:
⚠️ **Missing restriction in Javadoc:** The Javadoc says values are
"concatenated with commas in the generated plugin descriptor", but doesn't warn
that individual values must therefore **not contain commas**. Since
`String.join(",", defaultValues)` is used without escaping, a value like
`"a,b"` would produce an ambiguous descriptor entry that gets split into two
values when parsed. Consider adding:
```suggestion
* Default values concatenated with commas in the generated plugin
descriptor.
* Each String value has the format outlined in {@link #defaultValue()}.
* Individual values must not contain commas (the separator character).
* Multiple values are only supported for collection or array
parameters. An empty array specifies no default.
```
##########
maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java:
##########
@@ -774,6 +745,78 @@ private List<MojoDescriptor> toMojoDescriptors(
return mojoDescriptors;
}
+ private org.apache.maven.plugin.descriptor.Parameter extractParameter(
+ ParameterAnnotationContent parameterAnnotationContent) throws
InvalidParameterException {
+ org.apache.maven.plugin.descriptor.Parameter parameter = new
org.apache.maven.plugin.descriptor.Parameter();
+ String name = StringUtils.isEmpty(parameterAnnotationContent.name())
+ ? parameterAnnotationContent.getFieldName()
+ : parameterAnnotationContent.name();
+ parameter.setName(name);
+ parameter.setAlias(parameterAnnotationContent.alias());
+ parameter.setDeprecated(parameterAnnotationContent.getDeprecated());
+ parameter.setDescription(parameterAnnotationContent.getDescription());
+ parameter.setEditable(!parameterAnnotationContent.readonly());
+ String property = parameterAnnotationContent.property();
+ if (StringUtils.contains(property, '$')
+ || StringUtils.contains(property, '{')
+ || StringUtils.contains(property, '}')) {
+ throw new InvalidParameterException(
+ "Invalid property for parameter '" + parameter.getName() +
"', " + "forbidden characters ${}: "
+ + property,
+ null);
+ }
+ parameter.setExpression((property == null || property.isEmpty()) ? ""
: "${" + property + "}");
+ StringBuilder type = new
StringBuilder(parameterAnnotationContent.getClassName());
+ String rawType = type.toString();
+ if (!parameterAnnotationContent.getTypeParameters().isEmpty()) {
+ type.append(parameterAnnotationContent.getTypeParameters().stream()
+ .collect(Collectors.joining(", ", "<", ">")));
+ }
+ parameter.setType(type.toString());
+ parameter.setSince(parameterAnnotationContent.getSince());
+ parameter.setRequired(parameterAnnotationContent.required());
+ String defaultValue = parameterAnnotationContent.defaultValue();
+ String[] defaultValues = parameterAnnotationContent.defaultValues();
+ if (defaultValue != null && defaultValues != null) {
+ throw new InvalidParameterException(
+ "Invalid default value for parameter '" +
parameter.getName()
+ + "': defaultValue and defaultValues cannot both
be specified",
+ null);
+ }
+ if (defaultValue != null) {
+ parameter.setDefaultValue(defaultValue);
+ } else if (defaultValues != null) {
+ if (defaultValues.length == 1) {
+ parameter.setDefaultValue(defaultValues[0]);
+ } else if (defaultValues.length > 1) {
+ // check if type is either a collection or an array, otherwise
we cannot set a default value with
+ // multiple values
+ if (!isCollection(rawType)) {
+ throw new InvalidParameterException(
+ "Invalid default value for parameter '" +
parameter.getName() + "' of type '"
+ + type.toString() + "', "
+ + "multiple values are only allowed for
collection or array types",
+ null);
+ }
+ parameter.setDefaultValue(String.join(",", defaultValues));
+ }
+ }
+ return parameter;
+ }
+
+ /**
+ * Checks if the given class name represents a collection type
(Collection, List, Set, or array).
+ * This is just a heuristics as the full class hierarchy is not known at
this point.
+ * @param className
+ * @return true if the class name represents a collection type, false
otherwise
+ */
+ static boolean isCollection(String className) {
+ return className.endsWith("Collection")
+ || className.endsWith("List")
+ || className.endsWith("Set")
+ || className.endsWith("[]");
Review Comment:
💡 **`isCollection()` is missing `Queue` and `Deque` subtypes.** The
heuristic currently checks `Collection`, `List`, `Set`, and `[]`, but `Queue`
and `Deque` (and their common implementations like `ArrayDeque`, `LinkedList`
when used as `Deque`) are also valid collection types in Mojo parameters.
Consider:
```suggestion
static boolean isCollection(String className) {
return className.endsWith("Collection")
|| className.endsWith("List")
|| className.endsWith("Set")
|| className.endsWith("Queue")
|| className.endsWith("Deque")
|| className.endsWith("[]");
}
```
The Javadoc already notes "this is just a heuristic" — worth extending while
the method is being added.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]