Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8a75e38e946fb92b00e57f08896a95209f1c3fce (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004, 2005, 2006 Eike Stepper, Sympedia Methods and Tools.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 *******************************************************************************/
package org.eclipse.net4j.util;


import java.lang.reflect.Method;


public class BeanHelper
{
  public static Class mostSpecificClass(Class[] classes)
  {
    int size = classes.length;

    for (int i = 0; i < size; i++)
    {
      Class iClass = classes[i];

      for (int k = i + 1; k < size; k++)
      {
        Class kClass = classes[k];

        if (iClass.isAssignableFrom(kClass))
        {
          // iClass is super class of kClass
          // iClass is less specific
          classes[i] = classes[--size];
          break;
        }
        else if (kClass.isAssignableFrom(iClass))
        {
          // kClass is super class of iClass
          // kClass is less specific
          classes[k] = classes[--size];
          break;
        }
      }
    }

    if (size > 1)
    {
      throw new RuntimeException("Ambiguous ViewFacories");
    }

    if (size < 1)
    {
      throw new RuntimeException("No ViewFacories");
    }

    return classes[0];
  }

  public static void dispatchChild(Object object, String name, Object value)
  {
    Method adder = findAdder(object.getClass(), name);
    if (adder == null) throw new BeanException("Child " + name + " not reognized");

    try
    {
      adder.invoke(object, new Object[] { value});
    }
    catch (Throwable t)
    {
      throw new BeanException("Child " + name + " not accessible", t);
    }
  }

  public static Object[] children(Object object, String name)
  {
    Method getter = findChildGetter(object.getClass(), name);
    if (getter == null) throw new BeanException("Child " + name + " not reognized");

    try
    {
      return (Object[]) getter.invoke(object, (Object[])null);
    }
    catch (Throwable t)
    {
      throw new BeanException("Child " + name + " not accessible", t);
    }
  }

  public static void dispatchAttributeValue(Object object, String name, String value)
  {
    Method setter = findSetter(object.getClass(), name);
    if (setter == null) throw new BeanException("Attribute " + name + " not reognized");

    try
    {
      Class type = setter.getParameterTypes()[0];

      if (type == Boolean.class)
      {
        Boolean bool = value == null ? null : Boolean.valueOf(value);
        setter.invoke(object, new Object[] { bool});
      }
      else
      {
        // if (type == String)
        setter.invoke(object, new Object[] { value});
      }
    }
    catch (Throwable t)
    {
      throw new BeanException("Attribute " + name + " not accessible", t);
    }
  }

  public static void dispatchAttributeValueBoolean(Object object, String name, Boolean value)
  {
    Method setter = findSetter(object.getClass(), name);
    if (setter == null) throw new BeanException("Attribute " + name + " not reognized");
    if (setter.getParameterTypes()[0] != Boolean.class)
      throw new BeanException("Attribute " + name + " is not Boolean");

    try
    {
      setter.invoke(object, new Object[] { value});
    }
    catch (Throwable t)
    {
      throw new BeanException("Attribute " + name + " not accessible", t);
    }
  }

  public static String attributeValue(Object object, String name)
  {
    Method getter = findGetter(object.getClass(), name);
    if (getter == null) throw new BeanException("Attribute " + name + " not reognized");

    try
    {
      Object returnValue = getter.invoke(object, (Object[])null);
      if (returnValue == null) return null;
      if (returnValue instanceof String) return (String) returnValue;

      //if (returnValue instanceof Boolean) 
      return ((Boolean) returnValue).toString();

    }
    catch (Throwable t)
    {
      throw new BeanException("Attribute " + name + " not accessible", t);
    }
  }

  public static Boolean attributeValueBoolean(Object object, String name)
  {
    Method getter = findGetter(object.getClass(), name);
    if (getter == null) throw new BeanException("Attribute " + name + " not reognized");
    if (getter.getReturnType() != Boolean.class)
      throw new BeanException("Attribute " + name + " is not Boolean");

    try
    {
      return (Boolean) getter.invoke(object, (Object[])null);
    }
    catch (Throwable t)
    {
      throw new BeanException("Attribute " + name + " accessible", t);
    }
  }

  public static Method findGetter(Class clazz, String name)
  {
    String accessor = "is" + capitalize(name);
    Method method = findMethod(clazz, accessor);
    if (validGetter(method)) return method;

    accessor = "get" + capitalize(name);
    method = findMethod(clazz, accessor);
    if (validGetter(method)) return method;

    return null;
  }

  public static Method findChildGetter(Class clazz, String name)
  {
    String accessor = "get" + capitalize(name) + "s";
    Method method = findMethod(clazz, accessor);
    if (validChildGetter(method)) return method;

    return null;
  }

  public static Method findSetter(Class clazz, String name)
  {
    String accessor = "set" + capitalize(name);
    Method method = findMethod(clazz, accessor);
    return validSetter(method) ? method : null;
  }

  public static Method findAdder(Class clazz, String name)
  {
    String accessor = "add" + capitalize(name);
    Method method = findMethod(clazz, accessor);
    return validAdder(method) ? method : null;
  }

  public static Method findMethod(Class clazz, String name)
  {
    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++)
    {
      Method method = methods[i];
      if (name.equals(method.getName())) return method;
    }
    return null;
  }

  private static boolean validGetter(Method method)
  {
    if (method == null) return false;

    Class[] paramTypes = method.getParameterTypes();
    if (paramTypes != null || paramTypes.length != 0) return false;

    Class returnType = method.getReturnType();
    if (returnType == null || !validAttributeType(returnType)) return false;

    return true;
  }

  private static boolean validSetter(Method method)
  {
    if (method == null) return false;

    Class[] paramTypes = method.getParameterTypes();
    if (paramTypes == null || paramTypes.length != 1 || !validAttributeType(paramTypes[0]))
      return false;

    Class returnType = method.getReturnType();
    if (returnType != null && returnType != void.class) return false;

    return true;
  }

  private static boolean validAdder(Method method)
  {
    if (method == null) return false;

    Class[] paramTypes = method.getParameterTypes();
    if (paramTypes == null || paramTypes.length != 1) return false;

    Class returnType = method.getReturnType();
    if (returnType != null && returnType != void.class) return false;

    return true;
  }

  private static boolean validChildGetter(Method method)
  {
    if (method == null) return false;

    Class[] paramTypes = method.getParameterTypes();
    if (paramTypes != null || paramTypes.length != 0) return false;

    Class returnType = method.getReturnType();
    if (returnType == null || !validChildType(returnType)) return false;

    return true;
  }

  private static boolean validAttributeType(Class type)
  {
    return type == String.class || type == Boolean.class;
  }

  private static boolean validChildType(Class type)
  {
    return type.isArray();
  }

  private static String capitalize(String name)
  {
    return StringHelper.firstToUpper(name);
  }
}

Back to the top