Skip to content

Generic getObjectType on FactoryBean should not be generic [SPR-5982] #10650

@spring-projects-issues

Description

@spring-projects-issues

Stephen Solka opened SPR-5982 and commented

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.


Affects: 3.0 GA

Issue Links:

2 votes, 3 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions