1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.util; |
18 | |
|
19 | |
import java.lang.reflect.Field; |
20 | |
import java.lang.reflect.Modifier; |
21 | |
import java.util.Collection; |
22 | |
import java.util.Collections; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Map; |
25 | |
import java.util.Set; |
26 | |
|
27 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
28 | |
import org.springframework.beans.factory.InitializingBean; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class ConstantsMap implements InitializingBean, Map<String, Object> { |
36 | |
|
37 | |
private Map<String, Object> map; |
38 | |
private Class<?> constantClass; |
39 | |
|
40 | |
@Override |
41 | |
public void afterPropertiesSet() throws Exception { |
42 | 0 | final Map<String, Object> m = new HashMap<String, Object>(); |
43 | |
|
44 | 0 | publishFields(m, constantClass); |
45 | 0 | map = Collections.unmodifiableMap(m); |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
protected void publishFields(Map<String, Object> constantMap, Class<?> c) { |
55 | 0 | final Field[] fields = c.getDeclaredFields(); |
56 | 0 | for (final Field field : fields) { |
57 | 0 | final int modifier = field.getModifiers(); |
58 | |
|
59 | |
|
60 | 0 | if (Modifier.isStatic(modifier) && Modifier.isFinal(modifier) && !Modifier.isPrivate(modifier)) { |
61 | |
try { |
62 | 0 | final String fieldName = field.getName(); |
63 | |
|
64 | 0 | constantMap.put(fieldName, field.get(null)); |
65 | 0 | } catch (IllegalAccessException e) { |
66 | 0 | throw new JSTLConstantExporterException(e); |
67 | 0 | } |
68 | |
} |
69 | |
} |
70 | |
|
71 | |
|
72 | 0 | publishMemberClassFields(constantMap, c); |
73 | 0 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
protected void publishMemberClassFields(Map<String, Object> constantMap, Class<?> c) { |
83 | 0 | final Class<?>[] memberClasses = c.getClasses(); |
84 | |
|
85 | 0 | for (final Class<?> memberClass : memberClasses) { |
86 | 0 | if (!memberClass.isAnonymousClass()) { |
87 | 0 | final String memberPrefix = memberClass.getSimpleName(); |
88 | |
|
89 | 0 | final Map<String, Object> subclassMap = new HashMap<String, Object>(); |
90 | 0 | publishFields(subclassMap, memberClass); |
91 | 0 | constantMap.put(memberPrefix, Collections.unmodifiableMap(subclassMap)); |
92 | |
} |
93 | |
} |
94 | 0 | } |
95 | |
|
96 | |
public Class<?> getConstantClass() { |
97 | 0 | return this.constantClass; |
98 | |
} |
99 | |
|
100 | |
public void setConstantClass(Class<?> constantClass) { |
101 | 0 | this.constantClass = constantClass; |
102 | 0 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
@Override |
107 | |
public int size() { |
108 | 0 | return this.map.size(); |
109 | |
} |
110 | |
|
111 | |
@Override |
112 | |
public boolean isEmpty() { |
113 | 0 | return this.map.isEmpty(); |
114 | |
} |
115 | |
|
116 | |
@Override |
117 | |
public boolean containsKey(Object key) { |
118 | 0 | return this.map.containsKey(key); |
119 | |
} |
120 | |
|
121 | |
@Override |
122 | |
public boolean containsValue(Object value) { |
123 | 0 | return this.map.containsValue(value); |
124 | |
} |
125 | |
|
126 | |
@Override |
127 | |
public Object get(Object key) { |
128 | 0 | return this.map.get(key); |
129 | |
} |
130 | |
|
131 | |
@Override |
132 | |
public Object put(String key, Object value) { |
133 | 0 | return this.map.put(key, value); |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public Object remove(Object key) { |
138 | 0 | return this.map.remove(key); |
139 | |
} |
140 | |
|
141 | |
@Override |
142 | |
public void putAll(Map<? extends String, ? extends Object> m) { |
143 | 0 | this.map.putAll(m); |
144 | 0 | } |
145 | |
|
146 | |
@Override |
147 | |
public void clear() { |
148 | 0 | this.map.clear(); |
149 | 0 | } |
150 | |
|
151 | |
@Override |
152 | |
public Set<String> keySet() { |
153 | 0 | return this.map.keySet(); |
154 | |
} |
155 | |
|
156 | |
@Override |
157 | |
public Collection<Object> values() { |
158 | 0 | return this.map.values(); |
159 | |
} |
160 | |
|
161 | |
@Override |
162 | |
public Set<java.util.Map.Entry<String, Object>> entrySet() { |
163 | 0 | return this.map.entrySet(); |
164 | |
} |
165 | |
|
166 | |
@Override |
167 | |
public boolean equals(Object o) { |
168 | 0 | return this.map.equals(o); |
169 | |
} |
170 | |
|
171 | |
@Override |
172 | |
public int hashCode() { |
173 | 0 | return this.map.hashCode(); |
174 | |
} |
175 | |
|
176 | 0 | private static class JSTLConstantExporterException extends RiceRuntimeException { |
177 | |
|
178 | |
public JSTLConstantExporterException(Throwable t) { |
179 | 0 | super(t); |
180 | 0 | } |
181 | |
} |
182 | |
} |