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