Announcement Announcement Module
Collapse
No announcement yet.
Why would Spring think was supposed to be an ENUM? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why would Spring think was supposed to be an ENUM?

    I have a situation where a parent class has a Date field. I want to be able to set this field dynamically and allow things to vary by what I set up in an XML file. For example, in my XML file I want to be able to say things like TODAY and NEXT_WEEK and have these calculated and then injected into the parent class.

    What I did was extend the parent and then created a method that took a String instead of a Date, and then in that method did the calculations and then called the parent setDate(Date) with the results.

    For example:
    Code:
       public void setDate(String date)
       {
          //The helper method looks for TODAY, NEXT_WEEK, etc.
          setDate(DateTestHelper.generateDate(date));
       }
    The DateTestHelper looks like this:
    Code:
       public static Date generateDate(String dateType)
       {
          Date requestDate = null;
          
          if(dateType.equals("TODAY"))
          {
             requestDate = new Date();
          }
    ...
       return requestDate;
    }
    The XML for that looks like this:

    Code:
      <property name="date">
    	<value>TODAY</value>
    		<!--Date object in code, YYYYMMDD here -->
      </property>
    When I run the application I get this back:

    12:15:16,975 BeanWrapperImpl DEBUG - Field [TODAY] isn't an enum value
    java.lang.NoSuchFieldException: TODAY
    at java.lang.Class.getField(Class.java:919)
    at org.springframework.beans.BeanWrapperImpl.doTypeCo nversionIfNecessary(BeanWrapperImpl.java:940)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:692)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:572)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:737)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValues(BeanWrapperImpl.java:764)

    Why won't Spring simply call my derived class setDate(String) method? Why does it think that TODAY is supposed to be an enum?

    Note that I have done this sort of thing using a mapper class that did not extend another class and that works, but this, in my mind, should work fine.

    BTW, this is on Spring 1.2.8

  • #2
    Have you tried the same thing in Spring 2.0 to see if its fixed?

    Comment


    • #3
      Originally posted by karldmoore View Post
      Have you tried the same thing in Spring 2.0 to see if its fixed?
      No...I'm afraid that we are locked into this version for a while.

      Note that I know that I can simply alter the name in the derived class, calculate the date, then call the correct setting in the parent, but I just didn't think you should have to do this.

      Comment


      • #4
        Can you attach the relevent code and I'll run it in 2.0 if you want? Doesn't fix your problem but interesting to see if its still not working as you expected.

        Comment


        • #5
          Originally posted by karldmoore View Post
          Can you attach the relevent code and I'll run it in 2.0 if you want? Doesn't fix your problem but interesting to see if its still not working as you expected.
          I just shot you an email.

          Comment


          • #6
            Isn't it because of the java Bean specs? If you have a getter which returns a Date object it looks for the setter with the same type. I believe it is stated so in the spec.

            You could rename your method from setDate to setDateString and see if that works.

            Comment


            • #7
              Originally posted by mdeinum View Post
              Isn't it because of the java Bean specs? If you have a getter which returns a Date object it looks for the setter with the same type. I believe it is stated so in the spec.

              You could rename your method from setDate to setDateString and see if that works.
              I agree with Marten, hopefully the problem should be easily visible from the code. Hotmail seems to playing up though, can you try resending?

              Comment


              • #8
                Originally posted by mdeinum View Post
                Isn't it because of the java Bean specs? If you have a getter which returns a Date object it looks for the setter with the same type. I believe it is stated so in the spec.

                You could rename your method from setDate to setDateString and see if that works.
                Good point!

                In my derived class I didn't code another get method to use a String, so maybe it was confused.

                I'll add that and see what happens.

                Comment


                • #9
                  How did you fix this?

                  Comment


                  • #10
                    type attribute

                    I had a similar problem that originated from a constructor arg. I fixed this by specifying the type using the type attribute:

                    Code:
                    <bean id="exampleBean" class="examples.ExampleBean">
                      <constructor-arg type="int" value="7500000"/>
                      <constructor-arg type="java.lang.String" value="42"/>
                    </bean>
                    resource: http://static.springsource.org/sprin...ctor-injection

                    Comment

                    Working...
                    X