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 (2417) | 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 (764) | 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 (63.6k33646) | 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.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.