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 | 0 | public abstract class ReflectedPropertyIntrospectorSupport |
35 | |
implements ReflectedPropertyIntrospector |
36 | |
{ |
37 | |
|
38 | |
|
39 | |
public List introspectProperties(ReflectedClazz clazz, Class javaClass) { |
40 | 0 | HashMap parseResultMap = new HashMap(); |
41 | 0 | introspectProperties(clazz, javaClass, parseResultMap); |
42 | |
|
43 | 0 | boolean loggingEnabled = clazz.isLoggingEnabled(); |
44 | |
|
45 | 0 | ArrayList list = new ArrayList(); |
46 | 0 | Iterator iter = parseResultMap.values().iterator(); |
47 | 0 | while (iter.hasNext()) { |
48 | 0 | ReflectedPropertyParseResults parseResults = |
49 | |
(ReflectedPropertyParseResults) iter.next(); |
50 | 0 | if (clazz.getProperty(parseResults.getPropertyName()) != null) { |
51 | 0 | continue; |
52 | |
} |
53 | |
|
54 | 0 | boolean aliasExists = false; |
55 | 0 | String aliases[] = parseResults.getAliases(); |
56 | 0 | for (int i = 0; i < aliases.length; i++) { |
57 | 0 | if (clazz.getProperty(aliases[i]) != null) { |
58 | 0 | aliasExists = true; |
59 | 0 | break; |
60 | |
} |
61 | |
} |
62 | 0 | if (aliasExists) { |
63 | 0 | continue; |
64 | |
} |
65 | |
|
66 | 0 | boolean consistent = parseResults.checkConsistency(); |
67 | 0 | if (consistent) { |
68 | 0 | list.add(createProperty(clazz, parseResults)); |
69 | |
} |
70 | 0 | if (loggingEnabled) { |
71 | 0 | clazz.logPropertyParseResults(parseResults); |
72 | |
} |
73 | 0 | } |
74 | 0 | 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 | 0 | Class instanceClass = clazz.getInstanceClass(); |
87 | 0 | List declaredPropertyList = null; |
88 | 0 | if (instanceClass.getSuperclass() == null) { |
89 | 0 | declaredPropertyList = clazz.getProperties(); |
90 | |
} |
91 | |
else { |
92 | 0 | List superProperties = clazz.getSuperclazz().getProperties(); |
93 | 0 | if (superProperties.size() == 0) { |
94 | 0 | declaredPropertyList = clazz.getProperties(); |
95 | |
} |
96 | |
else { |
97 | 0 | HashSet superNames = new HashSet(); |
98 | 0 | for (int i = 0; i < superProperties.size(); i++) { |
99 | 0 | ClazzProperty property = |
100 | |
(ClazzProperty) superProperties.get(i); |
101 | 0 | superNames.add(property.getName()); |
102 | |
} |
103 | |
|
104 | 0 | List properties = clazz.getProperties(); |
105 | 0 | declaredPropertyList = new ArrayList(); |
106 | 0 | for (int i = 0; i < properties.size(); i++) { |
107 | 0 | ClazzProperty property = |
108 | |
(ClazzProperty) properties.get(i); |
109 | 0 | String name = property.getName(); |
110 | 0 | if (!superNames.contains(name)) { |
111 | 0 | declaredPropertyList.add(property); |
112 | |
} |
113 | |
} |
114 | |
} |
115 | |
} |
116 | 0 | 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 | 0 | if (plural.startsWith(singular)) { |
136 | 0 | return isCorrectPluralSuffix( |
137 | |
singular, |
138 | |
plural.substring(singular.length())); |
139 | |
} |
140 | 0 | 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 | 0 | return true; |
146 | |
} |
147 | 0 | return false; |
148 | |
} |
149 | |
|
150 | |
protected boolean isCorrectPluralSuffix(String singular, String suffix) { |
151 | 0 | return suffix.equals("s"); |
152 | |
} |
153 | |
} |