001    /*
002     * Copyright 2002-2004 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.clazz.reflect.common;
017    
018    import java.lang.reflect.Field;
019    import java.lang.reflect.Method;
020    
021    import org.apache.commons.clazz.Clazz;
022    import org.apache.commons.clazz.ClazzAccessException;
023    import org.apache.commons.clazz.common.ClazzFeatureSupport;
024    import org.apache.commons.clazz.reflect.ReflectedProperty;
025    
026    /**
027     * 
028     * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
029     * @version $Id: ReflectedAccessorPairProperty.java 155436 2005-02-26 13:17:48Z dirkv $
030     */
031    public class ReflectedAccessorPairProperty extends ClazzFeatureSupport 
032            implements ReflectedProperty 
033    {
034        private String name;
035        private static final String[] EMPTY_STRING_ARRAY = new String[0];
036        private String[] aliases = EMPTY_STRING_ARRAY;
037        private Class type;
038        private Clazz clazz;
039        private Field field;
040        private Method readMethod;
041        private Method writeMethod;
042        
043        /**
044         * Constructor for ReflectedClazzProperty.
045         */
046        public ReflectedAccessorPairProperty(Clazz declaringClazz, String name) {
047            super(declaringClazz);
048            this.name = name;
049        }
050    
051        /**
052         * @see org.apache.commons.clazz.ClazzProperty#getName()
053         */
054        public String getName() {
055            return name;
056        }
057        
058        /**
059         * Returns the aliases.
060         * @return String[]
061         */
062        public String[] getAliases() {
063            return aliases;
064        }
065    
066        /**
067         * Sets the aliases.
068         * @param aliases The aliases to set
069         */
070        public void setAliases(String[] aliases) {
071            this.aliases = aliases;
072        }
073        
074        /**
075         * @see org.apache.commons.clazz.ClazzProperty#getClazz()
076         */
077        public Clazz getClazz() {
078            if (clazz == null) {
079                clazz = getDeclaringClazz().
080                    getClazzLoader().getClazzForName(type.getName());
081            }
082            return clazz;
083        }
084        
085        /**
086         * @see org.apache.commons.clazz.ClazzProperty#getContentClazz()
087         */
088        public Clazz getContentClazz() {
089            return null;
090        }
091    
092        /**
093         * @see org.apache.commons.clazz.ClazzProperty#getKeyClazz()
094         */
095        public Clazz getKeyClazz() {
096            return null;
097        }
098    
099        /**
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    }