11
11
import javax .lang .model .element .TypeElement ;
12
12
import javax .lang .model .type .TypeKind ;
13
13
import javax .lang .model .type .TypeMirror ;
14
- import java .util .Arrays ;
15
- import java .util .Collections ;
16
- import java .util .List ;
17
- import java .util .Optional ;
18
- import java .util .OptionalInt ;
19
- import java .util .stream .Stream ;
14
+ import java .util .*;
20
15
21
16
import static java .lang .Character .isWhitespace ;
22
17
import static net .jbock .compiler .AnnotationUtil .getCollectorClass ;
@@ -113,7 +108,6 @@ private Param(
113
108
OptionType paramType ,
114
109
ExecutableElement sourceMethod ,
115
110
String name ,
116
- boolean positional ,
117
111
String bundleKey , Coercion coercion ,
118
112
List <String > description ,
119
113
String descriptionArgumentName ,
@@ -147,16 +141,6 @@ private Param(
147
141
throw ValidationException .create (sourceMethod , "Declare a flag, or use a custom mapper." );
148
142
}
149
143
}
150
- if (Stream .of (optional , repeatable , flag ).mapToInt (Param ::booleanToInt ).sum () >= 2 ) {
151
- throw ValidationException .create (sourceMethod , "Only one of optional, repeatable and flag can be true." );
152
- }
153
- if (positional && positionalOrder () == null ) {
154
- throw new AssertionError ("positional, but positionalType is null" );
155
- }
156
- }
157
-
158
- private static int booleanToInt (boolean b ) {
159
- return b ? 1 : 0 ;
160
144
}
161
145
162
146
FieldSpec field () {
@@ -199,6 +183,15 @@ private static Param createNonpositional(
199
183
boolean optional = parameter .optional ();
200
184
boolean flag = parameter .flag ();
201
185
boolean required = !repeatable && !optional && !flag ;
186
+ if (optional && repeatable ) {
187
+ throw ValidationException .create (sourceMethod , "A parameter can be either repeatable or optional, but not both." );
188
+ }
189
+ if (optional && flag ) {
190
+ throw ValidationException .create (sourceMethod , "A flag cannot be declared optional." );
191
+ }
192
+ if (repeatable && flag ) {
193
+ throw ValidationException .create (sourceMethod , "A flag cannot be declared repeatable." );
194
+ }
202
195
if (flag && mapperClass != null ) {
203
196
throw ValidationException .create (sourceMethod ,
204
197
"A flag parameter can't have a mapper." );
@@ -215,7 +208,6 @@ private static Param createNonpositional(
215
208
type ,
216
209
sourceMethod ,
217
210
name ,
218
- false ,
219
211
parameter .bundleKey (),
220
212
typeInfo ,
221
213
cleanDesc (description ),
@@ -238,6 +230,9 @@ private static Param createPositional(
238
230
boolean repeatable = parameter .repeatable ();
239
231
boolean optional = parameter .optional ();
240
232
boolean required = !repeatable && !optional ;
233
+ if (optional && repeatable ) {
234
+ throw ValidationException .create (sourceMethod , "A parameter can be either repeatable or optional, but not both." );
235
+ }
241
236
Coercion coercion = CoercionProvider .getInstance ().findCoercion (sourceMethod , name , mapperClass , collectorClass , repeatable , optional );
242
237
OptionType type = optionType (repeatable , false );
243
238
String descriptionArgumentName = parameter .descriptionArgumentName ().isEmpty () ?
@@ -250,7 +245,6 @@ private static Param createPositional(
250
245
type ,
251
246
sourceMethod ,
252
247
name ,
253
- true ,
254
248
parameter .bundleKey (),
255
249
coercion ,
256
250
cleanDesc (description ),
0 commit comments