1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.clazz.reflect.common;
17
18 import java.lang.reflect.Field;
19 import java.lang.reflect.Method;
20
21 import org.apache.commons.clazz.Clazz;
22 import org.apache.commons.clazz.ClazzAccessException;
23 import org.apache.commons.clazz.common.ClazzFeatureSupport;
24 import org.apache.commons.clazz.reflect.ReflectedProperty;
25
26
27
28
29
30
31 public class ReflectedAccessorPairProperty extends ClazzFeatureSupport
32 implements ReflectedProperty
33 {
34 private String name;
35 private static final String[] EMPTY_STRING_ARRAY = new String[0];
36 private String[] aliases = EMPTY_STRING_ARRAY;
37 private Class type;
38 private Clazz clazz;
39 private Field field;
40 private Method readMethod;
41 private Method writeMethod;
42
43
44
45
46 public ReflectedAccessorPairProperty(Clazz declaringClazz, String name) {
47 super(declaringClazz);
48 this.name = name;
49 }
50
51
52
53
54 public String getName() {
55 return name;
56 }
57
58
59
60
61
62 public String[] getAliases() {
63 return aliases;
64 }
65
66
67
68
69
70 public void setAliases(String[] aliases) {
71 this.aliases = aliases;
72 }
73
74
75
76
77 public Clazz getClazz() {
78 if (clazz == null) {
79 clazz = getDeclaringClazz().
80 getClazzLoader().getClazzForName(type.getName());
81 }
82 return clazz;
83 }
84
85
86
87
88 public Clazz getContentClazz() {
89 return null;
90 }
91
92
93
94
95 public Clazz getKeyClazz() {
96 return null;
97 }
98
99
100
101
102 public boolean isReadOnly() {
103 return false;
104 }
105
106
107
108
109
110 public Field getField() {
111 return field;
112 }
113
114
115
116
117
118 public Method getReadMethod() {
119 return readMethod;
120 }
121
122
123
124
125
126
127 public void setField(Field field) {
128 this.field = field;
129 }
130
131
132
133
134
135 public void setReadMethod(Method readMethod) {
136 this.readMethod = readMethod;
137 }
138
139
140
141
142
143 public Method getWriteMethod() {
144 return writeMethod;
145 }
146
147
148
149
150
151 public void setWriteMethod(Method writeMethod) {
152 this.writeMethod = writeMethod;
153 }
154
155
156
157
158 public Object get(Object instance) {
159 if (readMethod == null) {
160 throw new ClazzAccessException(
161 "Cannot read property " + name + ": no read method");
162 }
163 try {
164 return readMethod.invoke(instance, null);
165 }
166 catch (Exception ex) {
167 throw new ClazzAccessException(
168 "Cannot get property : "
169 + name
170 + ": cannot invoke method: "
171 + readMethod.getName(),
172 ex);
173 }
174 }
175
176
177
178
179 public void set(Object instance, Object value) {
180 if (writeMethod == null) {
181 throw new ClazzAccessException(
182 "Cannot modify property " + name + ": no write method");
183 }
184 try {
185 writeMethod.invoke(instance, new Object[]{value});
186 }
187 catch (Exception ex) {
188 throw new ClazzAccessException(
189 "Cannot set property : "
190 + name
191 + ": cannot invoke method: "
192 + writeMethod.getName(),
193 ex);
194 }
195 }
196
197
198
199
200
201
202 public Class getType() {
203 return type;
204 }
205
206
207
208
209
210 public void setType(Class type) {
211 this.type = type;
212 }
213
214
215
216
217 public boolean isCollection() {
218 return false;
219 }
220
221
222
223
224 public boolean isMap() {
225 return false;
226 }
227
228 }