Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
MICHAIL MOUDATSOS
Type parameter bound as Service method return type
March 1, 2013 5:22 AM
Answer

MICHAIL MOUDATSOS

Rank: Regular Member

Posts: 110

Join Date: October 4, 2011

Recent Posts

Hello all.

Assuming I have defined an entity in service.xml, let's say 'MyEntity'

It seems that in MyEntityLocalServiceImpl I can define a method with the following contract

1public List<?> getMyEntities(Class<?> clazz, int myEntityId)
or even
1public List<? extends MyEntity> getMyEntities(Class<? extends MyEntity> clazz, int myEntityId)
but I can't define
1public <T extends ChargingScheme> List<T> getMyEntities(Class<T> clazz, int myEntityId)
and of course not even
1public <T extends ChargingScheme> T getMyEntity(Class<T> clazz, int myEntityId)

From what i know, you can only return Collections, Objects that relate to primitive data types and Entities created by service-builder. The wildcard bound <?> is allowed in a Collection or Class declaration but not the type parameter bound <T>. Can I have, even a short, explanation for this? Or a reference to one.

Thank you all in advance!
David H Nebinger
RE: Type parameter bound as Service method return type
March 1, 2013 6:32 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11102

Join Date: September 1, 2006

Recent Posts

Because the initial SB stuff was done before these things got into Java. SB has undergone some minor changes/enhancements since it's initial launch, and there has been little if any effort put towards integrating these 'new' language features.

Note that SB's principle concept has always been to keep things simple. It does not support entity inheritance, limited support for one to many & many to many relationships, etc. It only works on primitives (and their object counterparts), collections, and entities. It has no shared visibility on any other class/interface (unless loaded from the global class loader), so ChargingScheme may not be visible in all necessary contexts.
MICHAIL MOUDATSOS
RE: Type parameter bound as Service method return type
March 1, 2013 7:04 AM
Answer

MICHAIL MOUDATSOS

Rank: Regular Member

Posts: 110

Join Date: October 4, 2011

Recent Posts

David H Nebinger:
It has no shared visibility on any other class/interface (unless loaded from the global class loader), so ChargingScheme may not be visible in all necessary contexts.
Woops! my bad there, I should have written MyEntity instead! (guess I didn't manage to anonymize my entity after all emoticon ). So it is what you said about the whole advancement of java language, etc.
Additionally in order to correct something I said:
1List<? extends MyEntity> ..../*and*/...Class<? extends MyEntity>...
does not produce any error while the ant build-service task runs, but in generated code it is converted to
1List<?> ..../*and*/...Class<?>...
,which in turn produces compilation errors. The reason might again lie in your answer so only bare wildcard is supported. The wildcard bound as well as the type parameter bound is not supported.

Thanks a lot!