I have a Port that is stereotyped with RtFeature, this stereotype has a property named "specification" that is a list.
I'd like to add comments that annotate that port, but when I try with this code it doesn't work:

I think you'll get on better by adding comments to ...getOwnedComment().

Regards

Ed Willink

On 03/12/2010 10:11, 123star wrote:
> Hi,
>
> I have a Port that is stereotyped with RtFeature, this stereotype has
> a property named "specification" that is a list.
> I'd like to add comments that annotate that port, but when I try with
> this code it doesn't work:
>
> Object tmp = thePort.getValue(stereoRtFeature, "specification");
>
> if(tmp instanceof List){
>
> List specList = (List)tmp;
> specList.add(theComment);
> }
>
>
> How can I add a value to a property that is a list?
> Thanks in advance, 123star

1 & 2) When I get the value the list is not null, so I add the element to it

I tried to create a new list ( by the way, what kind of list I can use? i tried arraylist with no success ), add the element to it and then set the list with setValue, but an IllegalArgumentException is thrown for doing this.

Why I can't add the element directly to the list that I get with getValue?

Why I can't add the element directly to the list that I get with getValue?

First, because the collection you get is not the actual collection used internally. Second, because it may not be modifiable. Third, UML2 API aside, unless a method documentation states it returns a modifiable collection and that you should modify it at will, that is the sensible thing to do.

Quote:

I tried to create a new list ( by the way, what kind of list I can use? i tried arraylist with no success ), add the element to it and then set the list with setValue, but an IllegalArgumentException is thrown for doing this.

There can be many reasons for an IAE in setValue (for instance, is the type of "theComment" variable compatible with the stereotype property type?). Your best bet is to ensure you have the source for UML2 (for instance, you have the UML2 Extender SDK, not only the binary libraries), and step through the call to setValue and see exactly where it is failing.