Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

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

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?

0 votes


Accepted answer

Permanent link

 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

0 votes


One other answer

Permanent link
 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.

0 votes

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 1,220
× 516
× 25
× 24

Question asked: Jul 26 '16, 4:43 a.m.

Question was seen: 3,047 times

Last updated: Jul 17 '19, 9:39 a.m.

Confirmation Cancel Confirm