summaryrefslogtreecommitdiff
path: root/src/main/java/com/beust/jcommander/Parameterized.java
blob: ff8753b452c9b31310b4dbbd53455ce0e465e97c (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
package com.beust.jcommander;

import com.beust.jcommander.internal.Lists;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Encapsulate a field or a method annotated with @Parameter or @DynamicParameter
 */
public class Parameterized {

  // Either a method or a field
  private Field m_field;
  private Method m_method;
  private Method m_getter;

  // Either of these two
  private WrappedParameter m_wrappedParameter;
  private ParametersDelegate m_parametersDelegate;

  public Parameterized(WrappedParameter wp, ParametersDelegate pd,
      Field field, Method method) {
    m_wrappedParameter = wp;
    m_method = method;
    m_field = field;
    if (m_field != null) {
      m_field.setAccessible(true);
    }
    m_parametersDelegate = pd;
  }

  public static List<Parameterized> parseArg(Object arg) {
    List<Parameterized> result = Lists.newArrayList();

    Class<? extends Object> cls = arg.getClass();
    while (!Object.class.equals(cls)) {
      for (Field f : cls.getDeclaredFields()) {
        Annotation annotation = f.getAnnotation(Parameter.class);
        Annotation delegateAnnotation = f.getAnnotation(ParametersDelegate.class);
        Annotation dynamicParameter = f.getAnnotation(DynamicParameter.class);
        if (annotation != null) {
          result.add(new Parameterized(new WrappedParameter((Parameter) annotation), null,
              f, null));
        } else if (dynamicParameter != null) {
          result.add(new Parameterized(new WrappedParameter((DynamicParameter) dynamicParameter), null,
              f, null));
        } else if (delegateAnnotation != null) {
          result.add(new Parameterized(null, (ParametersDelegate) delegateAnnotation,
              f, null));
        }
      }
      cls = cls.getSuperclass();
    }

    // Reassigning
    cls = arg.getClass();
    while (!Object.class.equals(cls)) {
      for (Method m : cls.getDeclaredMethods()) {
        Annotation annotation = m.getAnnotation(Parameter.class);
        Annotation delegateAnnotation = m.getAnnotation(ParametersDelegate.class);
        Annotation dynamicParameter = m.getAnnotation(DynamicParameter.class);
        if (annotation != null) {
          result.add(new Parameterized(new WrappedParameter((Parameter) annotation), null,
              null, m));
        } else if (dynamicParameter != null) {
          result.add(new Parameterized(new WrappedParameter((DynamicParameter) annotation), null,
              null, m));
        } else if (delegateAnnotation != null) {
          result.add(new Parameterized(null, (ParametersDelegate) delegateAnnotation,
              null, m));
        }
      }
      cls = cls.getSuperclass();
    }

    return result;
  }

  public WrappedParameter getWrappedParameter() {
    return m_wrappedParameter;
  }

  public Class<?> getType() {
    if (m_method != null) {
      return m_method.getParameterTypes()[0];
    } else {
      return m_field.getType();
    }
  }

  public String getName() {
    if (m_method != null) {
      return m_method.getName();
    } else {
      return m_field.getName();
    }
  }

  public Object get(Object object) {
    try {
      if (m_method != null) {
        if (m_getter == null) {
            m_getter = m_method.getDeclaringClass()
                .getMethod("g" + m_method.getName().substring(1),
                new Class[0]);
        }
        return m_getter.invoke(object);
      } else {
        return m_field.get(object);
      }
    } catch (SecurityException e) {
      throw new ParameterException(e);
    } catch (NoSuchMethodException e) {
      // Try to find a field
      String name = m_method.getName();
      String fieldName = Character.toLowerCase(name.charAt(3)) + name.substring(4);
      Object result = null;
      try {
        Field field = m_method.getDeclaringClass().getDeclaredField(fieldName);
        if (field != null) {
          field.setAccessible(true);
          result = field.get(object);
        }
      } catch(NoSuchFieldException ex) {
        // ignore
      } catch(IllegalAccessException ex) {
        // ignore
      }
      return result;
    } catch (IllegalArgumentException e) {
      throw new ParameterException(e);
    } catch (IllegalAccessException e) {
      throw new ParameterException(e);
    } catch (InvocationTargetException e) {
      throw new ParameterException(e);
    }
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((m_field == null) ? 0 : m_field.hashCode());
    result = prime * result + ((m_method == null) ? 0 : m_method.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Parameterized other = (Parameterized) obj;
    if (m_field == null) {
      if (other.m_field != null)
        return false;
    } else if (!m_field.equals(other.m_field))
      return false;
    if (m_method == null) {
      if (other.m_method != null)
        return false;
    } else if (!m_method.equals(other.m_method))
      return false;
    return true;
  }

  public boolean isDynamicParameter(Field field) {
    if (m_method != null) {
      return m_method.getAnnotation(DynamicParameter.class) != null;
    } else {
      return m_field.getAnnotation(DynamicParameter.class) != null;
    }
  }

  public void set(Object object, Object value) {
    try {
      if (m_method != null) {
        m_method.invoke(object, value);
      } else {
          m_field.set(object, value);
      }
    } catch (IllegalArgumentException ex) {
      throw new ParameterException(ex);
    } catch (IllegalAccessException ex) {
      throw new ParameterException(ex);
    } catch (InvocationTargetException ex) {
      // If a ParameterException was thrown, don't wrap it into another one
      if (ex.getTargetException() instanceof ParameterException) {
        throw (ParameterException) ex.getTargetException();
      } else {
        throw new ParameterException(ex);
      }
    }
  }

  public ParametersDelegate getDelegateAnnotation() {
    return m_parametersDelegate;
  }

  public Type getGenericType() {
    if (m_method != null) {
      return m_method.getGenericParameterTypes()[0];
    } else {
      return m_field.getGenericType();
    }
  }

  public Parameter getParameter() {
    return m_wrappedParameter.getParameter();
  }

  /**
   * @return the generic type of the collection for this field, or null if not applicable.
   */
  public Type findFieldGenericType() {
    if (m_method != null) {
      return null;
    } else {
      if (m_field.getGenericType() instanceof ParameterizedType) {
        ParameterizedType p = (ParameterizedType) m_field.getGenericType();
        Type cls = p.getActualTypeArguments()[0];
        if (cls instanceof Class) {
          return cls;
        }
      }
    }

    return null;
  }

  public boolean isDynamicParameter() {
    return m_wrappedParameter.getDynamicParameter() != null;
  }

}