It's all about the answers!

Ask a question

Why can't we cast org.eclipse.lyo.client.oslc.resources.Requirement to a subclass extending Requirements class?


Anuraj Kurup (244) | asked Jul 26 '16, 4:43 a.m.
Hi All,

I am trying to downcast Requirements class, using the below code :

Requirement otherRequirement = new MyRequirements();
 otherRequirement = getResponse.getEntity(Requirement.class);   // Load response in a Requirement object
       ((MyRequirements)otherRequirement).addCRSSatisfiedBySysRS(new Link(new URI("Some Url")));

But at the time of downcasting using the below line :
((MyRequirements)otherRequirement).addCRSSatisfiedBySysRS(new Link(new URI("Some Url")));

I get org.eclipse.lyo.client.oslc.resources.Requirement cannot be cast to org.XXX.samples.MyRequirements,

MyRequirements here is my sub class that extends the Requirement class.
I am pretty sure the downcasting works with other codes, but it gives an error here.
Is there anything in particular with the Requirement class?

Accepted answer


permanent link
Andrew Berezovskyi (762) | answered Jul 17 '19, 9:39 a.m.

 Anuraj,


I know this is an old topic, but maybe this will help someone else.

First, in Java you can only cast MyRequirements to Requirement, not the other way around. Google for Covariance and Contravariance In Java to read more about this.

Second, support for unmarshalling subclasses was added recently to Lyo: https://github.com/eclipse/lyo.core/pull/29

Cheers,
Andrew
(Eclipse Lyo project lead)

David Lafreniere selected this answer as the correct answer

One other answer



permanent link
Ralph Schoon (56.0k23642) | answered Jul 26 '16, 7:46 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
 You might want to ask in the Oslc forum http://open-services.net/forums/ as this question seems to be around Lyo and not doors next.

Your answer


Register or to post your answer.