Skip to content

Commit 5c82e9e

Browse files
committed
allow parameter with a converter that returns list
1 parent bf07e4b commit 5c82e9e

8 files changed

Lines changed: 31 additions & 20 deletions

File tree

compiler/src/main/java/net/jbock/annotated/AnnotatedMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public abstract class AnnotatedMethod {
2525

2626
public abstract boolean isParameter();
2727

28-
public abstract boolean isParameters();
28+
public abstract boolean isVarargsParameter();
2929

3030
public final String enumName() {
3131
return enumName;

compiler/src/main/java/net/jbock/annotated/AnnotatedOption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public boolean isParameter() {
6060
}
6161

6262
@Override
63-
public boolean isParameters() {
63+
public boolean isVarargsParameter() {
6464
return false;
6565
}
6666

compiler/src/main/java/net/jbock/annotated/AnnotatedParameter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public boolean isParameter() {
3838
}
3939

4040
@Override
41-
public boolean isParameters() {
41+
public boolean isVarargsParameter() {
4242
return false;
4343
}
4444

compiler/src/main/java/net/jbock/annotated/AnnotatedVarargsParameter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public boolean isParameter() {
3737
}
3838

3939
@Override
40-
public boolean isParameters() {
40+
public boolean isVarargsParameter() {
4141
return true;
4242
}
4343
}

compiler/src/main/java/net/jbock/convert/match/ListMatcher.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class ListMatcher implements Matcher {
2929
public <M extends AnnotatedMethod>
3030
Optional<Match<M>> tryMatch(
3131
M sourceMethod) {
32+
if (sourceMethod.isParameter()) {
33+
return Optional.empty(); // Not a VarargsParameter, so definitely not repeatable.
34+
}
3235
TypeMirror returnType = sourceMethod.returnType();
3336
return elements.getTypeElement("java.util.List")
3437
.flatMap(utilList -> tool.getSingleTypeArgument(returnType, utilList))

compiler/src/main/java/net/jbock/convert/match/MatchFinder.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io.jbock.util.Either;
44
import jakarta.inject.Inject;
5-
import net.jbock.Option;
65
import net.jbock.VarargsParameter;
76
import net.jbock.annotated.AnnotatedMethod;
87
import net.jbock.common.SafeTypes;
@@ -43,7 +42,7 @@ public class MatchFinder {
4342
Either<ValidationFailure, Match<M>> findMatch(
4443
M sourceMethod) {
4544
Match<M> match = findMatchInternal(sourceMethod);
46-
return validateParameterVsParameters(match)
45+
return validateVarargsIsList(match)
4746
.<Either<ValidationFailure, Match<M>>>map(Either::left)
4847
.orElseGet(() -> right(match));
4948
}
@@ -54,7 +53,7 @@ Either<ValidationFailure, Match<M>> findMatch(
5453
M sourceMethod) {
5554
PrimitiveType bool = types.getPrimitiveType(BOOLEAN);
5655
Match<M> match = Match.create(bool, OPTIONAL, sourceMethod);
57-
return validateParameterVsParameters(match)
56+
return validateVarargsIsList(match)
5857
.<Either<ValidationFailure, Match<M>>>map(Either::left)
5958
.orElseGet(() -> right(match));
6059
}
@@ -76,19 +75,10 @@ Either<ValidationFailure, Match<M>> findMatch(
7675
}
7776

7877
private <M extends AnnotatedMethod>
79-
Optional<ValidationFailure> validateParameterVsParameters(
78+
Optional<ValidationFailure> validateVarargsIsList(
8079
Match<M> match) {
8180
M sourceMethod = match.sourceMethod();
82-
if (sourceMethod.isParameter()
83-
&& match.multiplicity() == Multiplicity.REPEATABLE) {
84-
return Optional.of(sourceMethod.fail("method '" +
85-
sourceMethod.method().getSimpleName() +
86-
"' returns a list-based type, so it must be annotated with @" +
87-
Option.class.getSimpleName() +
88-
" or @" +
89-
VarargsParameter.class.getSimpleName()));
90-
}
91-
if (sourceMethod.isParameters()
81+
if (sourceMethod.isVarargsParameter()
9282
&& match.multiplicity() != Multiplicity.REPEATABLE) {
9383
return Optional.of(sourceMethod.fail("method '" +
9484
sourceMethod.method().getSimpleName() +

compiler/src/test/java/net/jbock/processor/ConverterTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,24 @@ void invalidConverterReturnsOptional() {
268268
.withErrorContaining("invalid converter class: should extend StringConverter<Integer> or implement Supplier<StringConverter<Integer>>");
269269
}
270270

271+
@Test
272+
void validConvertToList() {
273+
JavaFileObject javaFile = fromSource(
274+
"@Command",
275+
"abstract class Arguments {",
276+
"",
277+
" @Parameter(converter = MyConverter.class, index = 0)",
278+
" abstract List<Integer> something();",
279+
"",
280+
" static class MyConverter extends StringConverter<List<Integer>> {",
281+
" public List<Integer> convert(String token) { return null; }",
282+
" }",
283+
"}");
284+
assertAbout(javaSources()).that(singletonList(javaFile))
285+
.processedWith(Processor.testInstance())
286+
.compilesWithoutError();
287+
}
288+
271289
@Test
272290
void parameterInvalidList() {
273291
JavaFileObject javaFile = fromSource(
@@ -284,7 +302,7 @@ void parameterInvalidList() {
284302
assertAbout(javaSources()).that(singletonList(javaFile))
285303
.processedWith(Processor.testInstance())
286304
.failsToCompile()
287-
.withErrorContaining("method 'something' returns a list-based type, so it must be annotated with @Option or @VarargsParameter");
305+
.withErrorContaining("invalid converter class: should extend StringConverter<List<Integer>> or implement Supplier<StringConverter<List<Integer>>>");
288306
}
289307

290308
@Test

compiler/src/test/java/net/jbock/processor/PositionalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,6 @@ void parameterInvalidList() {
299299
assertAbout(javaSources()).that(singletonList(javaFile))
300300
.processedWith(Processor.testInstance())
301301
.failsToCompile()
302-
.withErrorContaining("method 'something' returns a list-based type, so it must be annotated with @Option or @VarargsParameter");
302+
.withErrorContaining("define a converter class that extends StringConverter<List<Integer>> or implements Supplier<StringConverter<List<Integer>>>");
303303
}
304304
}

0 commit comments

Comments
 (0)