Skip to content

Generic return type on FactoryBean.getObjectType causes problems [SPR-6692] #11358

@spring-projects-issues

Description

@spring-projects-issues

Kenny MacLeod opened SPR-6692 and commented

I'm reluctant to classify this as a bug, but considering the lack of an acceptable workaround, it seems appropriate. This is related to #10650, but that issue was filed as an improvement, and I think the case is stronger than that.

Since Spring 3.0 supported Java5 generics, the return type of the *getObject()* method of *FactoryBean<T>* has been narrowed to *Class<? extends T>*. This is fine as long as *T* is something like *MyClass*, you just return *MyClass.class* and everyone's happy.

The problems start when T is itself a generic type, for example *List<T>*. This gives a return type for *getObjectType()* as *Class<? extends List<T>>*, which is legal, but meaningless. Worse still, there is no easy way to generate an expression that has the type *Class<? extends List<T>>*. The class literal syntax cannot do it, and the only way I've found is a hack like *return (Class<List<T>>) new ArrayList<T>().getClass();*, which is ugly, and requires a warning suppression.

The worst case is when the return type cannot be instantiated in any way (e.g. because it's an interface, and no implementations are available or suitable), so even the above hack will not work. In these situations, just getting it to compile can be a struggle.

I suggest that the return type of *getObjectType()* be changed to *Class<?>*. This should not break any existing code compiled against Spring 3, since the type is being widened. This would then allow the raw class literal to be used (e.g. *return List.class;*.

Sorry for not raising this earlier, but the problem is quite chronic now.


Affects: 3.0 GA

Issue Links:

Referenced from: commits b53ccb3

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions