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