1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.util; |
17 | |
|
18 | |
import java.lang.reflect.Constructor; |
19 | |
import java.lang.reflect.InvocationTargetException; |
20 | |
|
21 | |
import javax.faces.FacesException; |
22 | |
|
23 | |
import org.seasar.framework.exception.IllegalAccessRuntimeException; |
24 | |
import org.seasar.framework.exception.InstantiationRuntimeException; |
25 | |
import org.seasar.framework.exception.InvocationTargetRuntimeException; |
26 | |
import org.seasar.framework.util.ClassUtil; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class MarshalUtil { |
32 | |
|
33 | |
public static Object createMarshalInstance(String className, |
34 | |
Class beforeClass, Object current) { |
35 | 31 | return createMarshalInstance(ClassUtil.forName(className), beforeClass, |
36 | |
current); |
37 | |
} |
38 | |
|
39 | |
public static Object createMarshalInstance(Class clazz, Class beforeClass, |
40 | |
Object current) { |
41 | 31 | if (clazz == null) { |
42 | 0 | if (current == null) { |
43 | 0 | throw new IllegalArgumentException(); |
44 | |
} else { |
45 | 0 | return current; |
46 | |
} |
47 | |
} |
48 | 31 | Constructor constructor = getConstructorWithNoException(clazz, |
49 | |
new Class[] { beforeClass }); |
50 | 31 | if (constructor != null) { |
51 | 6 | current = newInstanceByConstructor(constructor, |
52 | |
new Object[] { current }); |
53 | |
} else { |
54 | 25 | current = ClassUtil.newInstance(clazz); |
55 | |
} |
56 | 31 | return current; |
57 | |
} |
58 | |
|
59 | |
public static Constructor getConstructorWithNoException(Class clazz, |
60 | |
Class[] argTypes) { |
61 | 31 | if (clazz == null || argTypes == null) { |
62 | 0 | throw new IllegalArgumentException(); |
63 | |
} |
64 | |
try { |
65 | 31 | return clazz.getConstructor(argTypes); |
66 | 25 | } catch (NoSuchMethodException e) { |
67 | 25 | return null; |
68 | |
} |
69 | |
} |
70 | |
|
71 | |
public static Object newInstanceByConstructor(Constructor constructor, |
72 | |
Object[] args) { |
73 | 6 | if (constructor == null || args == null) { |
74 | 0 | throw new IllegalArgumentException(); |
75 | |
} |
76 | |
try { |
77 | 6 | return constructor.newInstance(args); |
78 | 0 | } catch (IllegalArgumentException e) { |
79 | 0 | throw new FacesException(e); |
80 | 0 | } catch (InstantiationException e) { |
81 | 0 | throw new InstantiationRuntimeException(constructor.getClass(), e); |
82 | 0 | } catch (IllegalAccessException e) { |
83 | 0 | throw new IllegalAccessRuntimeException(constructor.getClass(), e); |
84 | 0 | } catch (InvocationTargetException e) { |
85 | 0 | throw new InvocationTargetRuntimeException(constructor.getClass(), |
86 | |
e); |
87 | |
} |
88 | |
} |
89 | |
|
90 | |
} |