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.standard;
17  
18  import java.lang.reflect.Method;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import org.apache.commons.clazz.reflect.ReflectedClazz;
24  import org.apache.commons.clazz.reflect.common.*;
25  
26  /**
27   * A ReflectedPropertyIntrospector that discovers list (aka indexed) properties.
28   * 
29   * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
30   * @version $Id: StandardReflectedListPropertyIntrospector.java 155436 2005-02-26 13:17:48Z dirkv $
31   */
32  public class StandardReflectedListPropertyIntrospector
33      extends ReflectedListPropertyIntrospectorSupport 
34  {
35      public void introspectProperties(
36              ReflectedClazz clazz,
37              Class javaClass,
38              Map parseResultMap)
39      {
40          HashMap parseResultMapSingular = new HashMap();
41          Method methods[] = javaClass.getMethods();
42          ReflectedListPropertyParseResults parseResults;
43          AccessorMethodParseResults results;
44          for (int i = 0; i < methods.length; i++) {
45              Method method = methods[i];
46              
47              results = getReadAccessMethodParser().parse(method);
48              if (results != null) {
49                  parseResults =
50                      getParseResults(
51                          clazz,
52                          parseResultMap,
53                          results.getPropertyName());
54                  parseResults.setReadMethodParseResults(results);
55                  continue;
56              }
57  
58              results = getWriteAccessMethodParser().parse(method);
59              if (results != null) {
60                  parseResults =
61                      getParseResults(
62                          clazz,
63                          parseResultMap,
64                          results.getPropertyName());
65                  parseResults.setWriteMethodParseResults(results);
66                  continue;
67              }
68  
69              results = getGetAccessMethodParser().parse(method);
70              if (results != null) {
71                  parseResults =
72                      getParseResults(
73                          clazz,
74                          parseResultMapSingular,
75                          results.getPropertyName());
76                  parseResults.setGetMethodParseResults(results);
77                  continue;
78              }
79              
80              results = getSetAccessMethodParser().parse(method);
81              if (results != null) {
82                  parseResults =
83                      getParseResults(
84                          clazz,
85                          parseResultMapSingular,
86                          results.getPropertyName());
87                  parseResults.setSetMethodParseResults(results);
88                  continue;
89              }            
90          }
91          
92          Iterator iter = parseResultMap.entrySet().iterator();
93          while (iter.hasNext()) {
94              Map.Entry entry = (Map.Entry) iter.next();
95              ReflectedListPropertyParseResults result = 
96                  (ReflectedListPropertyParseResults) entry.getValue();
97                  
98              Class propertyType = result.getPropertyType();
99              Class componentType = result.getContentType();
100 
101             if (propertyType != null
102                 && (!propertyType.isArray()
103                     || (componentType != null
104                         && propertyType.getComponentType() != componentType))) {
105                 iter.remove(); // @todo verify that this actually works
106             }
107         }
108         
109         mergeSingularMethods(parseResultMap, parseResultMapSingular);
110     }
111 
112     /**
113      * With the standard JavaBean indexed properties the plural form is the same
114      * as the singular form.
115      */
116     protected boolean isCorrectPluralForm(String singular, String plural) {
117         return singular.equals(plural);
118     }    
119 }