Commit 392ab62c3d72b6f144e6d12b4491782a2f9c9331
1 parent
62dde646
Exists in
master
Ajustes para carregamento de arrays a partir de arquivo do
tipo properties
Showing
1 changed file
with
15 additions
and
15 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
@@ -52,6 +52,7 @@ import org.apache.commons.configuration.FileConfiguration; | @@ -52,6 +52,7 @@ import org.apache.commons.configuration.FileConfiguration; | ||
52 | import org.apache.commons.configuration.PropertiesConfiguration; | 52 | import org.apache.commons.configuration.PropertiesConfiguration; |
53 | import org.apache.commons.configuration.SystemConfiguration; | 53 | import org.apache.commons.configuration.SystemConfiguration; |
54 | import org.apache.commons.configuration.XMLConfiguration; | 54 | import org.apache.commons.configuration.XMLConfiguration; |
55 | +import org.apache.commons.lang.ClassUtils; | ||
55 | 56 | ||
56 | import br.gov.frameworkdemoiselle.annotation.Ignore; | 57 | import br.gov.frameworkdemoiselle.annotation.Ignore; |
57 | import br.gov.frameworkdemoiselle.annotation.Name; | 58 | import br.gov.frameworkdemoiselle.annotation.Name; |
@@ -178,37 +179,36 @@ public class ConfigurationLoader implements Serializable { | @@ -178,37 +179,36 @@ public class ConfigurationLoader implements Serializable { | ||
178 | } | 179 | } |
179 | 180 | ||
180 | String key = getKey(field); | 181 | String key = getKey(field); |
182 | + Object value; | ||
181 | 183 | ||
182 | if (field.getType().isArray()) { | 184 | if (field.getType().isArray()) { |
183 | - loadArrayField(field, key); | ||
184 | - } else if (field.getType() == String.class) { | ||
185 | - loadStringField(field, key); | 185 | + value = getArrayValue(field, key); |
186 | } else { | 186 | } else { |
187 | - loadBasicField(field, key); | 187 | + value = getPrimitiveOrWrappedValue(field, key); |
188 | } | 188 | } |
189 | - } | ||
190 | 189 | ||
191 | - private void loadArrayField(Field field, String key) { | ||
192 | - Object value = this.configuration.getArray(field.getType().getComponentType(), key, | ||
193 | - Reflections.getFieldValue(field, this.object)); | ||
194 | Reflections.setFieldValue(field, this.object, value); | 190 | Reflections.setFieldValue(field, this.object, value); |
195 | } | 191 | } |
196 | 192 | ||
197 | - private void loadStringField(Field field, String key) { | ||
198 | - Object value = this.configuration.getString(key, (String) Reflections.getFieldValue(field, this.object)); | ||
199 | - Reflections.setFieldValue(field, this.object, value); | 193 | + private Object getArrayValue(Field field, String key) { |
194 | + return this.configuration.getArray(field.getType().getComponentType(), key, | ||
195 | + Reflections.getFieldValue(field, this.object)); | ||
200 | } | 196 | } |
201 | 197 | ||
202 | - private void loadBasicField(Field field, String key) { | 198 | + @SuppressWarnings("unchecked") |
199 | + private Object getPrimitiveOrWrappedValue(Field field, String key) { | ||
203 | Object value; | 200 | Object value; |
201 | + Object defaultValue = Reflections.getFieldValue(field, this.object); | ||
204 | 202 | ||
205 | try { | 203 | try { |
206 | - value = this.configuration.get(field.getType(), key, Reflections.getFieldValue(field, this.object)); | 204 | + Class<Object> type = ClassUtils.primitiveToWrapper(field.getType()); |
205 | + value = this.configuration.get(type, key, defaultValue); | ||
206 | + | ||
207 | } catch (ConversionException cause) { | 207 | } catch (ConversionException cause) { |
208 | - value = null; | 208 | + value = defaultValue; |
209 | } | 209 | } |
210 | 210 | ||
211 | - Reflections.setFieldValue(field, this.object, value); | 211 | + return value; |
212 | } | 212 | } |
213 | 213 | ||
214 | private String getKey(Field field) { | 214 | private String getKey(Field field) { |