View Javadoc

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.Method;
19  
20  import org.apache.commons.clazz.Clazz;
21  
22  /**
23   * 
24   * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
25   * @version $Id: ReflectedListProperty.java 155436 2005-02-26 13:17:48Z dirkv $
26   */
27  public class ReflectedListProperty extends ReflectedAccessorPairProperty {
28  
29      private Class contentType;
30      private Method sizeMethod;
31      private Method getMethod;
32      private Method addMethod;
33      private Method addIndexedMethod;
34      private Method setMethod;
35      private Method removeMethod;
36      private Method removeIndexedMethod;
37      
38      /**
39       * Constructor for ReflectedListProperty.
40       * @param declaringClazz
41       * @param name
42       */
43      public ReflectedListProperty(Clazz declaringClazz, String name) {
44          super(declaringClazz, name);
45      }
46      
47      /**
48       * @see org.apache.commons.clazz.ClazzProperty#isCollection()
49       */
50      public boolean isCollection() {
51          return true;
52      }
53  
54      /**
55       * Returns the contentType.
56       * @return Class
57       */
58      public Class getContentType() {
59          return contentType;
60      }
61  
62      /**
63       * Sets the contentType.
64       * @param contentType The contentType to set
65       */
66      public void setContentType(Class valueType) {
67          this.contentType = valueType;
68      }
69      
70      /**
71       * Returns the getMethod.
72       * @return Method
73       */
74      public Method getGetMethod() {
75          return getMethod;
76      }
77  
78      /**
79       * Sets the getMethod.
80       * @param getMethod The getMethod to set
81       */
82      public void setGetMethod(Method getMethod) {
83          this.getMethod = getMethod;
84      }
85      
86     /**
87       * Returns the setMethod.
88       * @return Method
89       */
90      public Method getSetMethod() {
91          return setMethod;
92      }
93  
94      /**
95       * Sets the setMethod.
96       * @param setMethod The setMethod to set
97       */
98      public void setSetMethod(Method setMethod) {
99          this.setMethod = setMethod;
100     }
101 
102     /**
103      * Returns the addMethod.
104      * @return Method
105      */
106     public Method getAddMethod() {
107         return addMethod;
108     }
109 
110     /**
111      * Sets the addMethod.
112      * @param addMethod The addMethod to set
113      */
114     public void setAddMethod(Method addMethod) {
115         this.addMethod = addMethod;
116     }
117 
118     /**
119      * Returns the addIndexedMethod.
120      * @return Method
121      */
122     public Method getAddIndexedMethod() {
123         return addIndexedMethod;
124     }
125 
126     /**
127      * Sets the addIndexedMethod.
128      * @param addIndexedMethod The addIndexedMethod to set
129      */
130     public void setAddIndexedMethod(Method addIndexedMethod) {
131         this.addIndexedMethod = addIndexedMethod;
132     }
133 
134     /**
135      * Returns the removeMethod.
136      * @return Method
137      */
138     public Method getRemoveMethod() {
139         return removeMethod;
140     }
141 
142     /**
143      * Sets the removeMethod.
144      * @param removeMethod The removeMethod to set
145      */
146     public void setRemoveMethod(Method removeMethod) {
147         this.removeMethod = removeMethod;
148     }
149 
150     /**
151      * Returns the removeIndexedMethod.
152      * @return Method
153      */
154     public Method getRemoveIndexedMethod() {
155         return removeIndexedMethod;
156     }
157 
158     /**
159      * Sets the removeIndexedMethod.
160      * @param removeIndexedMethod The removeIndexedMethod to set
161      */
162     public void setRemoveIndexedMethod(Method removeIndexedMethod) {
163         this.removeIndexedMethod = removeIndexedMethod;
164     }
165 
166     /**
167      * Returns the sizeMethod.
168      * @return Method
169      */
170     public Method getSizeMethod() {
171         return sizeMethod;
172     }
173 
174     /**
175      * Sets the sizeMethod.
176      * @param sizeMethod The sizeMethod to set
177      */
178     public void setSizeMethod(Method sizeMethod) {
179         this.sizeMethod = sizeMethod;
180     }
181 
182     public String toString() {
183         StringBuffer buffer = new StringBuffer("[ReflectedListProperty ");
184         if (getType() != null) {
185             buffer.append(getType().getName());
186             buffer.append(" ");
187         }
188         if (getContentType() != null) {
189             buffer.append("[content: ");
190             buffer.append(getContentType().getName());
191             buffer.append("] ");
192         }
193         buffer.append(getName());
194         if (getReadMethod() != null) {
195             buffer.append("\n   [read method]      ");
196             buffer.append(getReadMethod());            
197         }
198         if (getWriteMethod() != null) {
199             buffer.append("\n   [write method]     ");
200             buffer.append(getWriteMethod());            
201         }
202         if (getGetMethod() != null) {
203             buffer.append("\n   [get method]       ");
204             buffer.append(getGetMethod());            
205         }
206         if (getSetMethod() != null) {
207             buffer.append("\n   [set method]       ");
208             buffer.append(getSetMethod());            
209         }
210         if (getAddMethod() != null) {
211             buffer.append("\n   [add method]       ");
212             buffer.append(getAddMethod());            
213         }
214         if (getAddIndexedMethod() != null) {
215             buffer.append("\n   [add(i) method]    ");
216             buffer.append(getAddIndexedMethod());            
217         }
218         if (getRemoveMethod() != null) {
219             buffer.append("\n   [remove method]    ");
220             buffer.append(getRemoveMethod());            
221         }
222         if (getRemoveIndexedMethod() != null) {
223             buffer.append("\n   [remove(i) method] ");
224             buffer.append(getRemoveIndexedMethod());
225         }
226         if (getSizeMethod() != null) {
227             buffer.append("\n   [size method]      ");
228             buffer.append(getSizeMethod());            
229         }
230         buffer.append("]");
231         return buffer.toString();
232     }
233     /**
234      * @see org.apache.commons.clazz.ClazzProperty#get(java.lang.Object)
235      */
236     public Object get(Object instance) {
237         return new ReflectedList(instance, this);
238     }
239     
240     public Object getList(Object instance) {
241         return super.get(instance);
242     }
243 
244     /**
245      * @see org.apache.commons.clazz.ClazzProperty#set(Object, Object)
246      */
247     public void set(Object instance, Object value) {
248         super.set(instance, value);
249     }
250 }