1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
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
121
122 protected abstract ReflectedAccessorPairProperty createProperty(
123 ReflectedClazz clazz,
124 ReflectedPropertyParseResults parseResults);
125
126
127
128
129
130
131
132
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 }