Uploaded image for project: 'Commons Configuration'
  1. Commons Configuration
  2. CONFIGURATION-508

Add generic get method to convert config property to any object

    XMLWordPrintableJSON

    Details

      Description

      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:

      public <T> T get(String key, Class<T> clazz);
      public <T> T get(String key, Class<T> clazz, T defaultValue);
      

      The method would perform a five step process:

      1. First it would get the string value, post interpolation.
      2. 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.
      3. 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.
      4. If no constructor or static method is found, it will check if a default value was given, returning it if true.
      5. 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.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mbertolini Matt Bertolini
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: