1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.apache.portals.graffito.jcr.reflection; |
17 |
|
|
18 |
|
import java.lang.reflect.InvocationTargetException; |
19 |
|
|
20 |
|
import org.apache.commons.beanutils.ConstructorUtils; |
21 |
|
import org.apache.commons.beanutils.PropertyUtils; |
22 |
|
import org.apache.portals.graffito.jcr.exception.JcrMappingException; |
23 |
|
import org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
0 |
abstract public class ReflectionUtils { |
32 |
|
public static Object getNestedProperty(Object object, String fieldName) { |
33 |
0 |
if (null == object) { |
34 |
0 |
return null; |
35 |
|
} |
36 |
|
|
37 |
|
try { |
38 |
0 |
return PropertyUtils.getNestedProperty(object, fieldName); |
39 |
|
} |
40 |
0 |
catch(IllegalAccessException e) { |
41 |
0 |
throw new JcrMappingException("Cannot access property " |
42 |
|
+ fieldName, |
43 |
|
e); |
44 |
|
} |
45 |
0 |
catch(InvocationTargetException e) { |
46 |
0 |
throw new JcrMappingException("Cannot access property " |
47 |
|
+ fieldName, |
48 |
|
e); |
49 |
|
} |
50 |
0 |
catch(NoSuchMethodException e) { |
51 |
0 |
throw new JcrMappingException("Cannot access property " |
52 |
|
+ fieldName, |
53 |
|
e); |
54 |
|
} |
55 |
|
} |
56 |
|
|
57 |
|
public static Class getPropertyType(Object object, String fieldName) { |
58 |
|
try { |
59 |
0 |
return PropertyUtils.getPropertyType(object, fieldName); |
60 |
|
} |
61 |
0 |
catch(Exception ex) { |
62 |
0 |
throw new JcrMappingException("Cannot access property " |
63 |
|
+ fieldName, |
64 |
|
ex); |
65 |
|
} |
66 |
|
} |
67 |
|
|
68 |
|
public static Object newInstance(Class clazz) { |
69 |
|
try { |
70 |
0 |
return clazz.newInstance(); |
71 |
|
} |
72 |
0 |
catch(Exception ex) { |
73 |
0 |
throw new JcrMappingException("Cannot create instance for class " |
74 |
|
+ clazz, |
75 |
|
ex); |
76 |
|
} |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
public static CollectionConverter invokeConstructor(String className, |
85 |
|
Object[] params) { |
86 |
|
try { |
87 |
0 |
Class converterClass= Class.forName(className); |
88 |
|
|
89 |
0 |
return (CollectionConverter) ConstructorUtils.invokeConstructor(converterClass, params); |
90 |
|
} |
91 |
0 |
catch(Exception ex) { |
92 |
0 |
throw new JcrMappingException("Cannot create instance for class " |
93 |
|
+ className, |
94 |
|
ex); |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
public static void setNestedProperty(Object object, String fieldName, Object value) { |
104 |
|
try { |
105 |
0 |
PropertyUtils.setNestedProperty(object, fieldName, value); |
106 |
|
} |
107 |
0 |
catch(Exception ex) { |
108 |
0 |
throw new JcrMappingException("Cannot set the field " + fieldName, |
109 |
|
ex); |
110 |
0 |
} |
111 |
0 |
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
public static Object newInstance(String clazz) { |
118 |
|
try { |
119 |
0 |
return Class.forName(clazz).newInstance(); |
120 |
|
} |
121 |
0 |
catch(Exception ex) { |
122 |
0 |
throw new JcrMappingException("Cannot create instance for class " |
123 |
|
+ clazz, |
124 |
|
ex); |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
public static Class forName(String clazz) { |
133 |
|
try { |
134 |
0 |
return Class.forName(clazz); |
135 |
|
} |
136 |
0 |
catch(Exception ex) { |
137 |
0 |
throw new JcrMappingException("Cannot load class " + clazz, ex); |
138 |
|
} |
139 |
|
} |
140 |
|
} |