You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Matt Bertolini (JIRA)" <ji...@apache.org> on 2012/09/24 17:22:07 UTC

[jira] [Created] (CONFIGURATION-508) Add generic get method to convert config property to any object

Matt Bertolini created CONFIGURATION-508:
--------------------------------------------

             Summary: Add generic get method to convert config property to any object
                 Key: CONFIGURATION-508
                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-508
             Project: Commons Configuration
          Issue Type: Improvement
          Components: Type conversion
    Affects Versions: 1.9
            Reporter: Matt Bertolini


Now that commons configuration requires java 5.0+, I think we should consider adding a couple of new generic getter methods that will enable developers to automatically convert the configuration property to the object of their choice. I envision the API looking like the below:

{code:java}
public <T> T get(String key, Class<T> clazz);
public <T> T get(String key, Class<T> clazz, T defaultValue);
{code}

The method would perform a five step process:
# First it would get the string value, post interpolation.
# Then it would check the given class for the existence of a static "valueOf" method with one String argument. If found, it would invoke that method with the interpolated string value.
# If no static "valueOf" method is found, it would search for a constructor with one String argument. If that is found, it would construct a new instance of the class passing the interpolated string value.
# If no constructor or static method is found, it will check if a default value was given, returning it if true.
# Finally, if no default value is given, it checks to see if throwExceptionOnMissing is set to true. If so, it will throw the exception. If false, it will return null.

This behavior is similar to how the Spring conversion service works. I believe that this feature would be a great help to developers as many of the values contained in configuration properties often times map over to various model objects.

I am looking for some feedback on the idea. Let me know your thoughts. Thanks.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (CONFIGURATION-508) Add generic get method to convert config property to any object

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CONFIGURATION-508?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13463160#comment-13463160 ] 

Oliver Heger commented on CONFIGURATION-508:
--------------------------------------------

We already have similar functionality in {{DataConfiguration}} which can be used as a wrapper around a standard {{Configuration}} object providing enhanced access methods. The methods even try to do some simple data type conversions. However, the whole concept could certainly be extended by a customizable converter framework. Please have a look whether these methods fit your needs.

I like your detailed description of the process for creating an object of a given target class. If this is a standard algorithm for creating an instance of class A based on string input (or maybe even on an arbitrary type B), maybe it is a candidate for being included into Commons Lang, for instance in {{ObjectUtils}}?
                
> Add generic get method to convert config property to any object
> ---------------------------------------------------------------
>
>                 Key: CONFIGURATION-508
>                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-508
>             Project: Commons Configuration
>          Issue Type: Improvement
>          Components: Type conversion
>    Affects Versions: 1.9
>            Reporter: Matt Bertolini
>              Labels: configuration, constructor, conversion, convert, generic, property, static, type, valueof
>
> Now that commons configuration requires java 5.0+, I think we should consider adding a couple of new generic getter methods that will enable developers to automatically convert the configuration property to the object of their choice. I envision the API looking like the below:
> {code:java}
> public <T> T get(String key, Class<T> clazz);
> public <T> T get(String key, Class<T> clazz, T defaultValue);
> {code}
> The method would perform a five step process:
> # First it would get the string value, post interpolation.
> # Then it would check the given class for the existence of a static "valueOf" method with one String argument. If found, it would invoke that method with the interpolated string value.
> # If no static "valueOf" method is found, it would search for a constructor with one String argument. If that is found, it would construct a new instance of the class passing the interpolated string value.
> # If no constructor or static method is found, it will check if a default value was given, returning it if true.
> # Finally, if no default value is given, it checks to see if throwExceptionOnMissing is set to true. If so, it will throw the exception. If false, it will return null.
> This behavior is similar to how the Spring conversion service works. I believe that this feature would be a great help to developers as many of the values contained in configuration properties often times map over to various model objects.
> I am looking for some feedback on the idea. Let me know your thoughts. Thanks.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (CONFIGURATION-508) Add generic get method to convert config property to any object

Posted by "Matt Bertolini (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CONFIGURATION-508?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13464325#comment-13464325 ] 

Matt Bertolini commented on CONFIGURATION-508:
----------------------------------------------

I was unaware of the DataConfiguration decorator class. It solves some of my problems (for things like URLs) but I also have lots of custom classes that I would like to use as well.

I could see making an addition to the PropertyConverter class in the to() method. Currently, it doesn't have an else condition in its long if/else block. Instead it throws an exception if it can't find a way of converting the string. We could add the else condition and use the steps above to have a one last try before throwing an exception.

Of course I do believe that it would be better to have the get() method more at the top level Configuration class rather than in a decorator. I inject the Configuration object into various locations of my application and it is better than passing around the decorator or constantly re-creating the decorator where it is needed.
                
> Add generic get method to convert config property to any object
> ---------------------------------------------------------------
>
>                 Key: CONFIGURATION-508
>                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-508
>             Project: Commons Configuration
>          Issue Type: Improvement
>          Components: Type conversion
>    Affects Versions: 1.9
>            Reporter: Matt Bertolini
>              Labels: configuration, constructor, conversion, convert, generic, property, static, type, valueof
>
> Now that commons configuration requires java 5.0+, I think we should consider adding a couple of new generic getter methods that will enable developers to automatically convert the configuration property to the object of their choice. I envision the API looking like the below:
> {code:java}
> public <T> T get(String key, Class<T> clazz);
> public <T> T get(String key, Class<T> clazz, T defaultValue);
> {code}
> The method would perform a five step process:
> # First it would get the string value, post interpolation.
> # Then it would check the given class for the existence of a static "valueOf" method with one String argument. If found, it would invoke that method with the interpolated string value.
> # If no static "valueOf" method is found, it would search for a constructor with one String argument. If that is found, it would construct a new instance of the class passing the interpolated string value.
> # If no constructor or static method is found, it will check if a default value was given, returning it if true.
> # Finally, if no default value is given, it checks to see if throwExceptionOnMissing is set to true. If so, it will throw the exception. If false, it will return null.
> This behavior is similar to how the Spring conversion service works. I believe that this feature would be a great help to developers as many of the values contained in configuration properties often times map over to various model objects.
> I am looking for some feedback on the idea. Let me know your thoughts. Thanks.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira