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