1 package com.beust.jcommander; 2 3 import java.lang.reflect.Field; 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Method; 6 7 /** 8 * Encapsulates the operations common to @Parameter and @DynamicParameter 9 */ 10 public class WrappedParameter { 11 private Parameter parameter; 12 private DynamicParameter dynamicParameter; 13 WrappedParameter(Parameter p)14 public WrappedParameter(Parameter p) { 15 parameter = p; 16 } 17 WrappedParameter(DynamicParameter p)18 public WrappedParameter(DynamicParameter p) { 19 dynamicParameter = p; 20 } 21 getParameter()22 public Parameter getParameter() { 23 return parameter; 24 } 25 getDynamicParameter()26 public DynamicParameter getDynamicParameter() { 27 return dynamicParameter; 28 } 29 arity()30 public int arity() { 31 return parameter != null ? parameter.arity() : 1; 32 } 33 hidden()34 public boolean hidden() { 35 return parameter != null ? parameter.hidden() : dynamicParameter.hidden(); 36 } 37 required()38 public boolean required() { 39 return parameter != null ? parameter.required() : dynamicParameter.required(); 40 } 41 password()42 public boolean password() { 43 return parameter != null ? parameter.password() : false; 44 } 45 names()46 public String[] names() { 47 return parameter != null ? parameter.names() : dynamicParameter.names(); 48 } 49 variableArity()50 public boolean variableArity() { 51 return parameter != null ? parameter.variableArity() : false; 52 } 53 validateWith()54 public Class<? extends IParameterValidator>[] validateWith() { 55 return parameter != null ? parameter.validateWith() : dynamicParameter.validateWith(); 56 } 57 validateValueWith()58 public Class<? extends IValueValidator>[] validateValueWith() { 59 return parameter != null 60 ? parameter.validateValueWith() 61 : dynamicParameter.validateValueWith(); 62 } 63 echoInput()64 public boolean echoInput() { 65 return parameter != null ? parameter.echoInput() : false; 66 } 67 addValue(Parameterized parameterized, Object object, Object value)68 public void addValue(Parameterized parameterized, Object object, Object value) { 69 try { 70 addValue(parameterized, object, value, null); 71 } catch (IllegalAccessException e) { 72 throw new ParameterException("Couldn't set " + object + " to " + value, e); 73 } 74 } 75 addValue(Parameterized parameterized, Object object, Object value, Field field)76 public void addValue(Parameterized parameterized, Object object, Object value, Field field) 77 throws IllegalAccessException { 78 if (parameter != null) { 79 if (field != null) { 80 field.set(object, value); 81 } else { 82 parameterized.set(object, value); 83 } 84 } else { 85 String a = dynamicParameter.assignment(); 86 String sv = value.toString(); 87 88 int aInd = sv.indexOf(a); 89 if (aInd == -1) { 90 throw new ParameterException( 91 "Dynamic parameter expected a value of the form a" + a + "b" 92 + " but got:" + sv); 93 } 94 callPut(object, parameterized, sv.substring(0, aInd), sv.substring(aInd + 1)); 95 } 96 } 97 callPut(Object object, Parameterized parameterized, String key, String value)98 private void callPut(Object object, Parameterized parameterized, String key, String value) { 99 try { 100 Method m; 101 m = findPut(parameterized.getType()); 102 m.invoke(parameterized.get(object), key, value); 103 } catch (SecurityException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { 104 e.printStackTrace(); 105 } 106 } 107 findPut(Class<?> cls)108 private Method findPut(Class<?> cls) throws SecurityException, NoSuchMethodException { 109 return cls.getMethod("put", Object.class, Object.class); 110 } 111 getAssignment()112 public String getAssignment() { 113 return dynamicParameter != null ? dynamicParameter.assignment() : ""; 114 } 115 isHelp()116 public boolean isHelp() { 117 return parameter != null && parameter.help(); 118 } 119 isNonOverwritableForced()120 public boolean isNonOverwritableForced() { 121 return parameter != null && parameter.forceNonOverwritable(); 122 } 123 } 124