From 3ec96a0722642126409a1adb314cd66b802cd3d0 Mon Sep 17 00:00:00 2001 From: Richard Tingstad Date: Sat, 28 Nov 2015 13:34:06 +0100 Subject: [PATCH] Proposed feature: Property key from String field value Currently, field name is used as key when @Configuration annotation has no value. A benefit of this is that the key is clearly visible everywhere the field is accessed. It cannot work, however, if the key consists of periods, like my.example.property=X Which is why I propose support for using field value as key expression. In most (?) IDEs, the key expression can then be viewed with Ctrl+mouseover or similar. This also means less duplication of key expressions. There are some disadvantages, like only working for String, which is why I won't be too sad if this feature request is not accepted :-) --- .../DefaultConstrettoConfiguration.java | 25 ++++++++++++++++++- .../ConstrettoConfigurationTest.java | 2 +- .../ConfigurationAnnotationsTest.java | 2 ++ .../helper/DataSourceConfiguration.java | 2 ++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/constretto-core/src/main/java/org/constretto/internal/DefaultConstrettoConfiguration.java b/constretto-core/src/main/java/org/constretto/internal/DefaultConstrettoConfiguration.java index 6b41d5f9..ceee232c 100644 --- a/constretto-core/src/main/java/org/constretto/internal/DefaultConstrettoConfiguration.java +++ b/constretto-core/src/main/java/org/constretto/internal/DefaultConstrettoConfiguration.java @@ -475,7 +475,7 @@ private void injectFields(T objectToConfigure) { try { if (field.isAnnotationPresent(Configuration.class)) { Configuration configurationAnnotation = field.getAnnotation(Configuration.class); - String expression = "".equals(configurationAnnotation.value()) ? field.getName() : configurationAnnotation.value(); + String expression = getExpression(objectToConfigure, field, configurationAnnotation); field.setAccessible(true); Class fieldType = field.getType(); if (hasValue(expression)) { @@ -514,6 +514,29 @@ private void injectFields(T objectToConfigure) { } while ((objectToConfigureClass = objectToConfigureClass.getSuperclass()) != null); } + /** + * If @Configuration() has value, this is used; + * if field is non-empty String, its value is used; + * otherwise, field name is used. + * + * @return Property key + */ + private static String getExpression(T objectToConfigure, Field field, Configuration configurationAnnotation) { + String expression = configurationAnnotation.value(); + if ("".equals(configurationAnnotation.value())) { + expression = field.getName(); + if (String.class.equals(field.getType())) { + try { + String value = (String) field.get(objectToConfigure); + if (value != null && value.length() > 0) { + expression = value; + } + } catch (IllegalAccessException ile) { } + } + } + return expression; + } + private boolean hasAnnotationDefaults(Configuration configurationAnnotation) { return !("N/A".equals(configurationAnnotation.defaultValue()) && configurationAnnotation.defaultValueFactory().equals(Configuration.EmptyValueFactory.class)); } diff --git a/constretto-core/src/test/java/org/constretto/ConstrettoConfigurationTest.java b/constretto-core/src/test/java/org/constretto/ConstrettoConfigurationTest.java index 564cdfc1..5f2665c8 100644 --- a/constretto-core/src/test/java/org/constretto/ConstrettoConfigurationTest.java +++ b/constretto-core/src/test/java/org/constretto/ConstrettoConfigurationTest.java @@ -25,7 +25,7 @@ public void constrettoShouldWorkWithNonExistingResources() { assertNotNull(configuration); } - @Test(expected = ConstrettoExpressionException.class) //TODO perhaps a more specific expeption + @Test(expected = ConstrettoExpressionException.class) //TODO perhaps a more specific exception public void shouldThrowExceptionIfTryingToMapWithoutNeededAllTagsProvided() throws Exception { new ConstrettoBuilder(false) .createPropertiesStore() diff --git a/constretto-core/src/test/java/org/constretto/internal/provider/ConfigurationAnnotationsTest.java b/constretto-core/src/test/java/org/constretto/internal/provider/ConfigurationAnnotationsTest.java index e069eb0f..d2dd851e 100644 --- a/constretto-core/src/test/java/org/constretto/internal/provider/ConfigurationAnnotationsTest.java +++ b/constretto-core/src/test/java/org/constretto/internal/provider/ConfigurationAnnotationsTest.java @@ -45,6 +45,7 @@ public void prepareTests() { setProperty("password", "password"); setProperty("vendor", "derby"); setProperty("version", "10"); + setProperty("derby.system.home", "C:\\home\\Derby\\"); setProperty("array", "[\"one\",\"two\",\"three\"]"); setProperty("map", "{\"1\":\"10\",\"2\":\"20\"}"); configuration = new ConstrettoBuilder().createSystemPropertiesStore().getConfiguration(); @@ -69,6 +70,7 @@ public void applyConfigrationToAnnotatedConfigurationObject() { assertEquals("username", customerDataSource.getUsername()); assertEquals("jdbc://url", customerDataSource.getUrl()); assertEquals("password", customerDataSource.getPassword()); + assertEquals("C:\\home\\Derby\\", customerDataSource.homeDir); assertEquals(new Integer(10), customerDataSource.getVersion()); } diff --git a/constretto-core/src/test/java/org/constretto/internal/provider/helper/DataSourceConfiguration.java b/constretto-core/src/test/java/org/constretto/internal/provider/helper/DataSourceConfiguration.java index 34e83a62..73fe521c 100644 --- a/constretto-core/src/test/java/org/constretto/internal/provider/helper/DataSourceConfiguration.java +++ b/constretto-core/src/test/java/org/constretto/internal/provider/helper/DataSourceConfiguration.java @@ -33,6 +33,8 @@ public class DataSourceConfiguration { @Configuration("username") private String myUsername; + @Configuration + public String homeDir = "derby.system.home"; @Configure public void configureMe(@Configuration String url, @Configuration("password") String secret) {