summaryrefslogtreecommitdiff
path: root/src/main/java/com/beust/jcommander/ParameterDescription.java
blob: bed5ba16f1ae54c29a19437a1d59d7daacfae078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
 * Copyright (C) 2010 the original author or authors.
 * See the notice.md file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.beust.jcommander;

import com.beust.jcommander.validators.NoValidator;
import com.beust.jcommander.validators.NoValueValidator;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
import java.util.ResourceBundle;

public class ParameterDescription {
  private Object object;

  private WrappedParameter wrappedParameter;
  private Parameter parameterAnnotation;
  private DynamicParameter dynamicParameterAnnotation;

  /** The field/method */
  private Parameterized parameterized;
  /** Keep track of whether a value was added to flag an error */
  private boolean assigned = false;
  private ResourceBundle bundle;
  private String description;
  private JCommander jCommander;
  private Object defaultObject;
  /** Longest of the names(), used to present usage() alphabetically */
  private String longestName = "";

  public ParameterDescription(Object object, DynamicParameter annotation,
      Parameterized parameterized,
      ResourceBundle bundle, JCommander jc) {
    if (! Map.class.isAssignableFrom(parameterized.getType())) {
      throw new ParameterException("@DynamicParameter " + parameterized.getName()
          + " should be of type "
          + "Map but is " + parameterized.getType().getName());
    }

    dynamicParameterAnnotation = annotation;
    wrappedParameter = new WrappedParameter(dynamicParameterAnnotation);
    init(object, parameterized, bundle, jc);
  }

  public ParameterDescription(Object object, Parameter annotation, Parameterized parameterized,
      ResourceBundle bundle, JCommander jc) {
    parameterAnnotation = annotation;
    wrappedParameter = new WrappedParameter(parameterAnnotation);
    init(object, parameterized, bundle, jc);
  }

  /**
   * Find the resource bundle in the annotations.
   * @return
   */
  @SuppressWarnings("deprecation")
  private ResourceBundle findResourceBundle(Object o) {
    ResourceBundle result = null;

    Parameters p = o.getClass().getAnnotation(Parameters.class);
    if (p != null && ! isEmpty(p.resourceBundle())) {
      result = ResourceBundle.getBundle(p.resourceBundle(), Locale.getDefault());
    } else {
      com.beust.jcommander.ResourceBundle a = o.getClass().getAnnotation(
          com.beust.jcommander.ResourceBundle.class);
      if (a != null && ! isEmpty(a.value())) {
        result = ResourceBundle.getBundle(a.value(), Locale.getDefault());
      }
    }

    return result;
  }

  private boolean isEmpty(String s) {
    return s == null || "".equals(s);
  }

  private void initDescription(String description, String descriptionKey, String[] names) {
    this.description = description;
    if (! "".equals(descriptionKey)) {
      if (bundle != null) {
        this.description = bundle.getString(descriptionKey);
      }
    }

    for (String name : names) {
      if (name.length() > longestName.length()) longestName = name;
    }
  }

  @SuppressWarnings("unchecked")
  private void init(Object object, Parameterized parameterized, ResourceBundle bundle,
      JCommander jCommander) {
    this.object = object;
    this.parameterized = parameterized;
    this.bundle = bundle;
    if (this.bundle == null) {
      this.bundle = findResourceBundle(object);
    }
    this.jCommander = jCommander;

    if (parameterAnnotation != null) {
      String description;
      if (Enum.class.isAssignableFrom(parameterized.getType())
          && parameterAnnotation.description().isEmpty()) {
        description = "Options: " + EnumSet.allOf((Class<? extends Enum>) parameterized.getType());
      }else {
        description = parameterAnnotation.description();
      }
      initDescription(description, parameterAnnotation.descriptionKey(),
          parameterAnnotation.names());
    } else if (dynamicParameterAnnotation != null) {
      initDescription(dynamicParameterAnnotation.description(),
          dynamicParameterAnnotation.descriptionKey(),
          dynamicParameterAnnotation.names());
    } else {
      throw new AssertionError("Shound never happen");
    }

    try {
      defaultObject = parameterized.get(object);
    } catch (Exception e) {
    }

    //
    // Validate default values, if any and if applicable
    //
    if (defaultObject != null) {
      if (parameterAnnotation != null) {
        validateDefaultValues(parameterAnnotation.names());
      }
    }
  }

  private void validateDefaultValues(String[] names) {
    String name = names.length > 0 ? names[0] : "";
    validateValueParameter(name, defaultObject);
  }

  public String getLongestName() {
    return longestName;
  }

  public Object getDefault() {
   return defaultObject;
  }

  public String getDescription() {
    return description;
  }

  public Object getObject() {
    return object;
  }

  public String getNames() {
    StringBuilder sb = new StringBuilder();
    String[] names = wrappedParameter.names();
    for (int i = 0; i < names.length; i++) {
      if (i > 0) sb.append(", ");
      sb.append(names[i]);
    }
    return sb.toString();
  }

  public WrappedParameter getParameter() {
    return wrappedParameter;
  }

  public Parameterized getParameterized() {
    return parameterized;
  }

  private boolean isMultiOption() {
    Class<?> fieldType = parameterized.getType();
    return fieldType.equals(List.class) || fieldType.equals(Set.class)
        || parameterized.isDynamicParameter();
  }

  public void addValue(String value) {
    addValue(value, false /* not default */);
  }

  /**
   * @return true if this parameter received a value during the parsing phase.
   */
  public boolean isAssigned() {
    return assigned;
  }


  public void setAssigned(boolean b) {
    assigned = b;
  }

  /**
   * Add the specified value to the field. First, validate the value if a
   * validator was specified. Then look up any field converter, then any type
   * converter, and if we can't find any, throw an exception.
   */
  public void addValue(String value, boolean isDefault) {
    addValue(null, value, isDefault, true, -1);
  }

  Object addValue(String name, String value, boolean isDefault, boolean validate, int currentIndex) {
    p("Adding " + (isDefault ? "default " : "") + "value:" + value
        + " to parameter:" + parameterized.getName());
    if(name == null) {
      name = wrappedParameter.names()[0];
    }
    if (currentIndex == 00 && assigned && ! isMultiOption() && !jCommander.isParameterOverwritingAllowed()
            || isNonOverwritableForced()) {
      throw new ParameterException("Can only specify option " + name + " once.");
    }

    if (validate) {
      validateParameter(name, value);
    }

    Class<?> type = parameterized.getType();

    Object convertedValue = jCommander.convertValue(getParameterized(), getParameterized().getType(), name, value);
    if (validate) {
      validateValueParameter(name, convertedValue);
    }
    boolean isCollection = Collection.class.isAssignableFrom(type);

    Object finalValue;
    if (isCollection) {
      @SuppressWarnings("unchecked")
      Collection<Object> l = (Collection<Object>) parameterized.get(object);
      if (l == null || fieldIsSetForTheFirstTime(isDefault)) {
          l = newCollection(type);
          parameterized.set(object, l);
      }
      if (convertedValue instanceof Collection) {
          l.addAll((Collection) convertedValue);
      } else {
          l.add(convertedValue);
      }
      finalValue = l;
    } else {
      // If the field type is not a collection, see if it's a type that contains @SubParameters annotations
      List<SubParameterIndex> subParameters = findSubParameters(type);
      if (! subParameters.isEmpty()) {
        // @SubParameters found
        finalValue = handleSubParameters(value, currentIndex, type, subParameters);
      } else {
        // No, regular parameter
        wrappedParameter.addValue(parameterized, object, convertedValue);
        finalValue = convertedValue;
      }
    }
    if (! isDefault) assigned = true;

    return finalValue;
  }

  private Object handleSubParameters(String value, int currentIndex, Class<?> type,
      List<SubParameterIndex> subParameters) {
    Object finalValue;// Yes, assign each following argument to the corresponding field of that object
    SubParameterIndex sai = null;
    for (SubParameterIndex si: subParameters) {
      if (si.order == currentIndex) {
        sai = si;
        break;
      }
    }
    if (sai != null) {
      Object objectValue = parameterized.get(object);
      try {
        if (objectValue == null) {
          objectValue = type.newInstance();
          parameterized.set(object, objectValue);
        }
        wrappedParameter.addValue(parameterized, objectValue, value, sai.field);
        finalValue = objectValue;
      } catch (InstantiationException | IllegalAccessException e) {
        throw new ParameterException("Couldn't instantiate " + type, e);
      }
    } else {
      throw new ParameterException("Couldn't find where to assign parameter " + value + " in " + type);
    }
    return finalValue;
  }

  public Parameter getParameterAnnotation() {
    return parameterAnnotation;
  }

  class SubParameterIndex {
    int order = -1;
    Field field;

    public SubParameterIndex(int order, Field field) {
      this.order = order;
      this.field = field;
    }
  }

  private List<SubParameterIndex> findSubParameters(Class<?> type) {
    List<SubParameterIndex> result = new ArrayList<>();
    for (Field field: type.getDeclaredFields()) {
      Annotation subParameter = field.getAnnotation(SubParameter.class);
      if (subParameter != null) {
        SubParameter sa = (SubParameter) subParameter;
        result.add(new SubParameterIndex(sa.order(), field));
      }
    }
    return result;
  }

  private void validateParameter(String name, String value) {
    final Class<? extends IParameterValidator> validators[] = wrappedParameter.validateWith();
    if (validators != null && validators.length > 0) {
        for(final Class<? extends IParameterValidator> validator: validators) {
          validateParameter(this, validator, name, value);
        }
    }
  }

  void validateValueParameter(String name, Object value) {
    final Class<? extends IValueValidator> validators[] = wrappedParameter.validateValueWith();
    if (validators != null && validators.length > 0) {
      for(final Class<? extends IValueValidator> validator: validators) {
        validateValueParameter(validator, name, value);
      }
    }
  }

  public static void validateValueParameter(Class<? extends IValueValidator> validator,
      String name, Object value) {
    try {
      if (validator != NoValueValidator.class) {
        p("Validating value parameter:" + name + " value:" + value + " validator:" + validator);
      }
      validator.newInstance().validate(name, value);
    } catch (InstantiationException | IllegalAccessException e) {
      throw new ParameterException("Can't instantiate validator:" + e);
    }
  }

  public static void validateParameter(ParameterDescription pd,
      Class<? extends IParameterValidator> validator,
      String name, String value) {
    try {
    	
      if (validator != NoValidator.class) {
        p("Validating parameter:" + name + " value:" + value + " validator:" + validator);
      }
      validator.newInstance().validate(name, value);
      if (IParameterValidator2.class.isAssignableFrom(validator)) {
        IParameterValidator2 instance = (IParameterValidator2) validator.newInstance();
        instance.validate(name, value, pd);
      }
    } catch (InstantiationException | IllegalAccessException e) {
      throw new ParameterException("Can't instantiate validator:" + e);
    } catch(ParameterException ex) {
      throw ex;
    } catch(Exception ex) {
      throw new ParameterException(ex);
    }
  }

  /*
   * Creates a new collection for the field's type.
   *
   * Currently only List and Set are supported. Support for
   * Queues and Stacks could be useful.
   */
  @SuppressWarnings("unchecked")
  private Collection<Object> newCollection(Class<?> type) {
    if (SortedSet.class.isAssignableFrom(type)) return new TreeSet();
    else if (LinkedHashSet.class.isAssignableFrom(type)) return new LinkedHashSet();
    else if (Set.class.isAssignableFrom(type)) return new HashSet();
    else if (List.class.isAssignableFrom(type)) return new ArrayList();
    else {
      throw new ParameterException("Parameters of Collection type '" + type.getSimpleName()
                                  + "' are not supported. Please use List or Set instead.");
    }
  }

  /*
   * Tests if its the first time a non-default value is
   * being added to the field.
   */
  private boolean fieldIsSetForTheFirstTime(boolean isDefault) {
    return (!isDefault && !assigned);
  }

  private static void p(String string) {
    if (System.getProperty(JCommander.DEBUG_PROPERTY) != null) {
      JCommander.getConsole().println("[ParameterDescription] " + string);
    }
  }

  @Override
  public String toString() {
    return "[ParameterDescription " + parameterized.getName() + "]";
  }

  public boolean isDynamicParameter() {
    return dynamicParameterAnnotation != null;
  }

  public boolean isHelp() {
    return wrappedParameter.isHelp();
  }
  
  public boolean isNonOverwritableForced() {
    return wrappedParameter.isNonOverwritableForced();
  }
}