1 /*
2 * Copyright 2002-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
29 * @version $Id: ReflectedAccessorPairProperty.java 155436 2005-02-26 13:17:48Z dirkv $
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 * Constructor for ReflectedClazzProperty.
45 */
46 public ReflectedAccessorPairProperty(Clazz declaringClazz, String name) {
47 super(declaringClazz);
48 this.name = name;
49 }
50
51 /**
52 * @see org.apache.commons.clazz.ClazzProperty#getName()
53 */
54 public String getName() {
55 return name;
56 }
57
58 /**
59 * Returns the aliases.
60 * @return String[]
61 */
62 public String[] getAliases() {
63 return aliases;
64 }
65
66 /**
67 * Sets the aliases.
68 * @param aliases The aliases to set
69 */
70 public void setAliases(String[] aliases) {
71 this.aliases = aliases;
72 }
73
74 /**
75 * @see org.apache.commons.clazz.ClazzProperty#getClazz()
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 * @see org.apache.commons.clazz.ClazzProperty#getContentClazz()
87 */
88 public Clazz getContentClazz() {
89 return null;
90 }
91
92 /**
93 * @see org.apache.commons.clazz.ClazzProperty#getKeyClazz()
94 */
95 public Clazz getKeyClazz() {
96 return null;
97 }
98
99 /**
100 * @see org.apache.commons.clazz.ClazzProperty#isReadOnly()
101 */
102 public boolean isReadOnly() {
103 return false;
104 }
105
106 /**
107 * Returns the field.
108 * @return Field
109 */
110 public Field getField() {
111 return field;
112 }
113
114 /**
115 * Returns the readMethod.
116 * @return Method
117 */
118 public Method getReadMethod() {
119 return readMethod;
120 }
121
122
123 /**
124 * Sets the field.
125 * @param field The field to set
126 */
127 public void setField(Field field) {
128 this.field = field;
129 }
130
131 /**
132 * Sets the readMethod.
133 * @param readMethod The readMethod to set
134 */
135 public void setReadMethod(Method readMethod) {
136 this.readMethod = readMethod;
137 }
138
139 /**
140 * Returns the writeMethod.
141 * @return Method
142 */
143 public Method getWriteMethod() {
144 return writeMethod;
145 }
146
147 /**
148 * Sets the writeMethod.
149 * @param writeMethod The writeMethod to set
150 */
151 public void setWriteMethod(Method writeMethod) {
152 this.writeMethod = writeMethod;
153 }
154
155 /**
156 * @see org.apache.commons.clazz.ClazzProperty#get(java.lang.Object)
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 * @see org.apache.commons.clazz.ClazzProperty#set(Object, Object)
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 * Returns the type.
200 * @return Class
201 */
202 public Class getType() {
203 return type;
204 }
205
206 /**
207 * Sets the type.
208 * @param type The type to set
209 */
210 public void setType(Class type) {
211 this.type = type;
212 }
213
214 /**
215 * @see org.apache.commons.clazz.ClazzProperty#isCollection()
216 */
217 public boolean isCollection() {
218 return false;
219 }
220
221 /**
222 * @see org.apache.commons.clazz.ClazzProperty#isMap()
223 */
224 public boolean isMap() {
225 return false;
226 }
227
228 }