Announcement Announcement Module
Collapse
No announcement yet.
setting a property of type Java 5 enum Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • setting a property of type Java 5 enum

    Hello,

    I have a class Foo with a property "level" of type LevelEnum (a Java 5 enum). How can one set the property in a spring xml configuration file? For example the following does not work, failing with a TypeMismatchException because Spring doesn't know how to convert the String to a LevelEnum. So what to do?

    Brian


    <bean id="foo" class="Foo">
    <property name="level"><value>high</value></property>
    </bean>

    Code:
    public class Foo &#123;
        private LevelEnum level;
     
        public LevelEnum getLevel&#40;&#41; &#123;
            return level;
        &#125;
    
        public void setLevel&#40;LevelEnum level&#41; &#123;
            this.level = level;
        &#125;
    
        enum LevelEnum&#123; low, medium, high &#125;
        ...
    &#125;

  • #2
    Brian,

    You can create your own PropertyEditor to convert between String and your enum class. You can then register this PropertyEditor using CustomEditorConfigurer in your ApplicationContext configuration.

    The Spring source has some good examples of creating custom editors - check out the org.springframework.beans.propertyeditors package.

    Rob

    Comment


    • #3
      Thanks, Rob.

      For those interested my solution is posted below.

      Hmmm... As written, an entry to CustomEditorConfigurer is required for each enum type. How would one enhance this solution so that when registered EnumPropertyEditor would handle any enum type automatically? Suggestions?

      Brian

      <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.Cu stomEditorConfigurer">
      <property name="customEditors">
      <map>
      <entry key="Foo$LevelEnum">
      <bean class="EnumPropertyEditor">
      <property name="enumType">
      <value>Foo$LevelEnum</value>
      </property>
      </bean>
      </entry>
      </map>
      </property>
      </bean>

      Code:
      /**
       * Should work with any Java 5 enum.
       * Configure the enumType property before use.
       */
      public class EnumPropertyEditor extends PropertyEditorSupport &#123;
          private Class enumType;
      
          public EnumPropertyEditor&#40;&#41; &#123;
          &#125;
      
          public EnumPropertyEditor&#40;Class enumType&#41; &#123;
              setEnumType&#40;enumType&#41;;
          &#125;
      
          public Class getEnumType&#40;&#41; &#123;
              return enumType;
          &#125;
      
          public void setEnumType&#40;Class enumType&#41; &#123;
              if&#40;!enumType.isEnum&#40;&#41;&#41;&#123;
                  throw new IllegalArgumentException&#40;"Class must be an enum"&#41;;
              &#125;
              this.enumType = enumType;
          &#125;
      
          public void setAsText&#40;String text&#41; throws IllegalArgumentException &#123;
              for&#40;Object e &#58; getEnumType&#40;&#41;.getEnumConstants&#40;&#41;&#41;&#123;
                  if&#40;e.toString&#40;&#41;.equals&#40;text&#41;&#41;&#123;
                      setValue&#40;e&#41;;
                      return;
                  &#125;
              &#125;
              throw new IllegalArgumentException&#40;"Invalid text for enum of type '" + enumType.getName&#40;&#41; + "&#58; '" + text + "'."&#41;;
          &#125;
      &#125;

      Comment


      • #4
        Brian,

        You might be able to get a more generic solution by creating a property editor that works with instances of java.lang.Enum.

        Rob

        Comment


        • #5
          Hi,

          Using some code and ideas I found on the Spring Jira
          I have implemented a generic EnumEditor. It works
          fine for values and the only caveat is that for every
          Enum you need to register an editor.

          I was wondering. I would like to use Enums as keys in a Map.
          Will Spring use my EnumEditor for key values too?

          Thanks.
          Alwyn

          Comment


          • #6
            While I was writing my EnumEditor I was trying to duplicate in a test program
            what Spring will be doing.

            I came across something odd.

            Using j2se 5.0, when you do a Class.forName, you get an object back which
            is of type Class<?>.

            So when you do something like:

            MyEnum mine = Enum.valueOf(Class.forname("MyEnum"),"X");

            It will not compile because it wants the first parameter to be of type
            Class<MyEnum>.

            So the only fix would be to have something like:

            Class<MyEnum> clazz = Class.forname("MyEnum");

            I expect this won't work well for Spring.

            So WHY is it working at the moment? Do the fact that my spring
            library is compiled with 1.4.2 have anything to do with it?

            Will it be impossible to do once Spring is build with j2se 5.0?

            Comment


            • #7
              This seems a little over the top when you could just make your set method take a string and do the conversion itself..

              public void setMessageSource(String messageSource) {
              this.messageSource = MessageSrc.valueOf(messageSource);
              }

              Comment


              • #8
                These posts are from over a year ago. With the latest 1.2.X and 2.0 versions of Spring, they are no longer applicable. Enums are handled "out of the box"; execept within collections (last time I checked).

                FWIW, the point of having an Enum is so one does not have to know the associated String; passing in a String and converting to an Enum seems to defeat the purpose of using the Enum to begin with.

                Comment


                • #9
                  great - can you post an example of how this is down "out-of-the-box" so other users who come across this post know the answer

                  Comment


                  • #10
                    Well, searching this forum, or using Google would probably give you what you are looking for. However, since I'm in a good mood today, here you go: http://www.jroller.com/page/kdonald. :-)

                    Comment


                    • #11
                      I had this problem recently and found a pretty good solution at http://deepthoughts.orsomethingliket...2006/10/19/13/

                      Hope that helps

                      Comment

                      Working...
                      X