mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-25 11:45:49 +01:00
[config] Use field from calling method to check superclass hierarchy for writing fields in 'ConfigMapper' (#1474)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
parent
859da2e1c7
commit
054ad6c48e
@ -75,7 +75,6 @@ public class ConfigMapper {
|
||||
Class<?> type = field.getType();
|
||||
|
||||
Object value = properties.get(configKey);
|
||||
|
||||
// Consider RequiredField annotations
|
||||
if (value == null) {
|
||||
LOGGER.trace("Skipping field '{}', because config has no entry for {}", fieldName, configKey);
|
||||
@ -99,22 +98,15 @@ public class ConfigMapper {
|
||||
value = objectConvert(value, type);
|
||||
LOGGER.trace("Setting value ({}) {} to field '{}' in configuration class {}", type.getSimpleName(),
|
||||
value, fieldName, configurationClass.getName());
|
||||
writeField(configuration, fieldName, value, true);
|
||||
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
|
||||
field.setAccessible(true);
|
||||
field.set(configuration, value);
|
||||
} catch (SecurityException | IllegalArgumentException | IllegalAccessException ex) {
|
||||
LOGGER.warn("Could not set field value for field '{}': {}", fieldName, ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
return configuration;
|
||||
}
|
||||
|
||||
private static void writeField(Object target, String fieldName, Object value, boolean forceAccess)
|
||||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
|
||||
Field field = target.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(forceAccess);
|
||||
field.set(target, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return fields of the given class as well as all super classes.
|
||||
*
|
||||
@ -123,13 +115,9 @@ public class ConfigMapper {
|
||||
*/
|
||||
private static List<Field> getAllFields(Class<?> clazz) {
|
||||
List<Field> fields = new ArrayList<>();
|
||||
|
||||
Class<?> currentClass = clazz;
|
||||
while (currentClass != null) {
|
||||
fields.addAll(Arrays.asList(currentClass.getDeclaredFields()));
|
||||
currentClass = currentClass.getSuperclass();
|
||||
for (Class<?> superclazz = clazz; superclazz != null; superclazz = superclazz.getSuperclass()) {
|
||||
fields.addAll(Arrays.asList(superclazz.getDeclaredFields()));
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ import org.junit.Test;
|
||||
*/
|
||||
public class ConfigurationTest {
|
||||
|
||||
public static final class ConfigClass {
|
||||
public static class ConfigClass {
|
||||
public enum MyEnum {
|
||||
ON,
|
||||
OFF,
|
||||
@ -50,6 +50,11 @@ public class ConfigurationTest {
|
||||
private static final String CONSTANT = "SOME_CONSTANT";
|
||||
}
|
||||
|
||||
public static class ExtendedConfigClass extends ConfigClass {
|
||||
public int additionalIntField;
|
||||
public String listField;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertGetConfigAsWorks() {
|
||||
Configuration configuration = new Configuration();
|
||||
@ -69,6 +74,21 @@ public class ConfigurationTest {
|
||||
assertThat(configClass.listField, is(hasItems("one", "two", "three")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertGetConfigAsWorksWithSuperclass() {
|
||||
Configuration configuration = new Configuration();
|
||||
configuration.put("intField", 1);
|
||||
configuration.put("additionalIntField", 5);
|
||||
configuration.put("listField", "one, two, three");
|
||||
|
||||
ExtendedConfigClass extendedConfigClass = configuration.as(ExtendedConfigClass.class);
|
||||
|
||||
assertThat(extendedConfigClass.intField, is(1));
|
||||
assertThat(extendedConfigClass.stringField, is("somedefault"));
|
||||
assertThat(extendedConfigClass.additionalIntField, is(5));
|
||||
assertThat(extendedConfigClass.listField, is("one, two, three"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertGetConfigAsEnumWrongValue() {
|
||||
Configuration configuration = new Configuration();
|
||||
|
Loading…
Reference in New Issue
Block a user