Announcement Announcement Module
Collapse
No announcement yet.
Problem with FactoryBean and generics in 3.0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with FactoryBean and generics in 3.0

    An issue was filed in JIRA which seems to have been lost in the noise, converning the use of generics in the 3.0 FactoryBean interface.

    http://issues.springframework.org/browse/SPR-5982

    The method signature for getObjectType on factoryBean is:
    java.lang.Class<? extends T> getObjectType()

    The problem with this is if you want to peg FactoryBean to a Generic class such as FactoryBean<List<String>> additional restrictions come on the implemtation of getObjectType. An example being:

    Class<? extends List<String>> getObjectType()

    { return ?????; }
    return List.class; //compilation error
    return (Class<? extends List<String>>) List.class; // compiles with eclipse compiler, compilation error with sun javac

    List<String> myList = new ArrayList<String>();
    return (Class<? extends List<String>>)myList.getClass(); //compiles on both javac + eclipse

    What this means is to actually return something that can make it through compilation you have to have a instantiated object ahead of time to call getClass on. This is not the most ideal situation as per the javadoc "In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance."

    There is of course a work around (which is why I put this as minor) you can either not use FactoryBean generics or be sure to not peg a internal generic ex: FactoryBean<List>. I understand that changing such a important interface at this point is unrealistic but I would ask if the implementation is left as is, a note be added to the javadoc about how to work around this particular wart in the java generics implementation.
    This is quite a tricky one, and it's a shame it wasn't addressed during the milestone builds, when the issue was first raised.

    What are people's thought's on this?

  • #2
    related to JDK7 vs 6 I noticed

    Comment

    Working...
    X