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.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.clazz.ClazzProperty;
26  import org.apache.commons.clazz.reflect.ReflectedClazz;
27  import org.apache.commons.clazz.reflect.ReflectedPropertyIntrospector;
28  
29  /**
30   * 
31   * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
32   * @version $Id: ReflectedPropertyIntrospectorSupport.java 155436 2005-02-26 13:17:48Z dirkv $
33   */
34  public abstract class ReflectedPropertyIntrospectorSupport
35      implements ReflectedPropertyIntrospector 
36  {
37      /**
38       */
39      public List introspectProperties(ReflectedClazz clazz, Class javaClass) {
40          HashMap parseResultMap = new HashMap();
41          introspectProperties(clazz, javaClass, parseResultMap);
42  
43          boolean loggingEnabled = clazz.isLoggingEnabled();
44  
45          ArrayList list = new ArrayList();
46          Iterator iter = parseResultMap.values().iterator();
47          while (iter.hasNext()) {
48              ReflectedPropertyParseResults parseResults =
49                  (ReflectedPropertyParseResults) iter.next();
50              if (clazz.getProperty(parseResults.getPropertyName()) != null) {
51                  continue;
52              }
53  
54              boolean aliasExists = false;
55              String aliases[] = parseResults.getAliases();
56              for (int i = 0; i < aliases.length; i++) {
57                  if (clazz.getProperty(aliases[i]) != null) {
58                      aliasExists = true;
59                      break;
60                  }
61              }
62              if (aliasExists) {
63                  continue;
64              }
65  
66              boolean consistent = parseResults.checkConsistency();
67              if (consistent) {
68                  list.add(createProperty(clazz, parseResults));
69              }
70              if (loggingEnabled) {
71                  clazz.logPropertyParseResults(parseResults);
72              }
73          }
74          return list;
75      }
76      
77      public abstract void introspectProperties(
78          ReflectedClazz clazz,
79          Class javaClass,
80          Map parseResultMap);
81  
82      public List introspectDeclaredProperties(
83              ReflectedClazz clazz,
84              Class javaClass)
85      {
86          Class instanceClass = clazz.getInstanceClass();
87          List declaredPropertyList = null;
88          if (instanceClass.getSuperclass() == null) {
89              declaredPropertyList = clazz.getProperties();
90          }
91          else {
92              List superProperties = clazz.getSuperclazz().getProperties();
93              if (superProperties.size() == 0) {
94                  declaredPropertyList = clazz.getProperties();
95              }
96              else {
97                  HashSet superNames = new HashSet();
98                  for (int i = 0; i < superProperties.size(); i++) {
99                      ClazzProperty property =
100                             (ClazzProperty) superProperties.get(i);
101                     superNames.add(property.getName());
102                 }
103 
104                 List properties = clazz.getProperties();
105                 declaredPropertyList = new ArrayList();
106                 for (int i = 0; i < properties.size(); i++) {
107                     ClazzProperty property =
108                         (ClazzProperty) properties.get(i);
109                     String name = property.getName();
110                     if (!superNames.contains(name)) {
111                         declaredPropertyList.add(property);
112                     }
113                 }
114             }
115         }
116         return declaredPropertyList;
117     }
118                 
119     /**
120      * Creates a new ReflectedAccessorPairProperty based on parse results. 
121      */
122     protected abstract ReflectedAccessorPairProperty createProperty(
123         ReflectedClazz clazz,
124         ReflectedPropertyParseResults parseResults);
125     
126     
127     /**
128      * Returns true if the <code>plural</code> parameter is
129      * a correct plural form for the property names <code>singular</code>.
130      * If the plural.startsWith(singular), calls isCorrectPluralSuffix
131      * with the singular form and the remaining part of the plural form.
132      * Separately handles the "~y" - "~ies" substitution for English.
133      */
134     protected boolean isCorrectPluralForm(String singular, String plural) {
135         if (plural.startsWith(singular)) {
136             return isCorrectPluralSuffix(
137                 singular,
138                 plural.substring(singular.length()));
139         }
140         else if (
141             singular.endsWith("y")
142                 && plural.endsWith("ies")
143                 && plural.substring(0, plural.length() - 3).equals(
144                     singular.substring(singular.length() - 1))) {
145             return true;
146         }
147         return false;
148     }
149     
150     protected boolean isCorrectPluralSuffix(String singular, String suffix) {
151         return suffix.equals("s");
152     }
153 }