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