Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 425170

Summary: ClassCastException in CommandContext when an expression returns an empty set
Product: [Modeling] Sirius Reporter: Stephane Begaudeau <stephane.begaudeau>
Component: CoreAssignee: Pierre-Charles David <pierre-charles.david>
Status: CLOSED FIXED QA Contact: Julien Dupont <julien.dupont>
Severity: major    
Priority: P3 CC: alex.lagarde, florian.barbin, julien.dupont, pierre-charles.david
Version: 1.0.0Keywords: triaged
Target Milestone: 1.0.0M7   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Use case to reproduce Class Cast Exception none

Description Stephane Begaudeau CLA 2014-01-09 05:43:02 EST
In org.eclipse.sirius.tools.api.command.CommandContext#getContextTargets(...)

http://git.eclipse.org/c/sirius/org.eclipse.sirius.git/tree/plugins/org.eclipse.sirius/src/org/eclipse/sirius/tools/api/command/CommandContext.java

Line 170/171
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
} else if (result instanceof Collection) {
   contextTargets = (List<Object>) result; // BUG!
} else {
   contextTargets = Lists.newArrayList(result);
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

There are other kinds of collections in Java :)

The whole "if" part could be replaced by something like this:
if (result == null) {
   contextTargets = Lists.newArrayList();
} else {
   contextTargets = Lists.newArrayList(result);
}

Since if it's a collection, Guava can create a new ArrayList from it anyway.
Comment 1 Alex Lagarde CLA 2014-01-09 07:49:47 EST
Hi Stephane, could you please join steps to reproduce the issue?
Comment 2 Stephane Begaudeau CLA 2014-01-09 08:09:57 EST
(In reply to Alex Lagarde from comment #1)
> Hi Stephane, could you please join steps to reproduce the issue?

Ask Julien :)

It was an Acceleo expression involving eInverse() which must have returned an empty set.
Comment 3 Julien Dupont CLA 2014-01-13 05:35:01 EST
Created attachment 238906 [details]
Use case to reproduce Class Cast Exception

To reproduce:
Open the use case representation Diagram and select node named Class1. Try to remove. Does Nothing, open the view "Error Log". The Class Cast Exeption appears.
This problem is in the VSM on remove tool, with this expression: "[oclAsType(ecore::EReference).eInverse()/]" that return an empty list.
Comment 4 Pierre-Charles David CLA 2014-01-15 10:10:09 EST
I renamed the ticket for more clarity, so here is the actual exception (which was used as a title previously):

Caused by: java.lang.ClassCastException: java.util.Collections$EmptySet cannot be cast to java.util.List
Comment 5 Pierre-Charles David CLA 2014-04-22 08:20:25 EDT
See https://git.eclipse.org/r/#/c/25343/.

Note that the suggestion for further simplification in #c1 would not work: result is statically known as a raw Object, so even if it is actually a collection, Lists.newArrayList(result) would call the Lists.newArrayList(E... elements) variant, which would result in a list with a single element (which would happen to be a collection).
Comment 6 Pierre-Charles David CLA 2014-04-25 11:09:17 EDT
Fixed by 3d05b28b007ea955c7ad566a6a13d1577c00e364.
Comment 7 Pierre-Charles David CLA 2014-05-12 03:45:31 EDT
Available in Sirius 1.0.0M7 (see https://wiki.eclipse.org/Sirius/1.0.0M7 & http://git.eclipse.org/c/sirius/org.eclipse.sirius.git/tag/?id=v1.0.0M7).