Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 345955 - [xtend] problem with auto-converting lists to arrays
Summary: [xtend] problem with auto-converting lists to arrays
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: unspecified   Edit
Hardware: PC Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: SR2   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-16 10:21 EDT by Moritz Eysholdt CLA
Modified: 2017-09-19 18:06 EDT (History)
2 users (show)

See Also:
sebastian.zarnekow: indigo+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Moritz Eysholdt CLA 2011-05-16 10:21:46 EDT
when overriding the Java Method

---
public String[] getExportedPackagesRt(Grammar grammar)
---

then 

---
override getExportedPackagesRt(Grammar grammar) {
  return newArrayList(semanticSequencer.packageName)
}
---

raises the error "Incompatible types. Expected java.lang.String[] or java.util.List<java.lang.String> but was java.util.ArrayList<java.lang.String>"

workaround: type-cast to List<String>
---
override getExportedPackagesRt(Grammar grammar) {
  return newArrayList(semanticSequencer.packageName) as List<String>
}
---

Expected behavior: flawlessly convert all sub-types of List<String>
Comment 1 Moritz Eysholdt CLA 2011-05-16 10:34:28 EDT
false positive for the workaround:

---
override getExportedPackagesRt(Grammar grammar) {
  return newArrayList(semanticSequencer.packageName) as List<String>
}
---

compiles to

---
public String[] getExportedPackagesRt(final Grammar grammar) {
  String _packageName = this.semanticSequencer.getPackageName();
  ArrayList<String> _newArrayList = CollectionLiterals.<String>newArrayList(_packageName);
  return ((String[])Conversions.unwrapArray(((List<String>) _newArrayList)));
}
---

which throws the following exception at runtime:

---
java.lang.ClassCastException: java.util.ArrayList
	at org.eclipse.xtext.generator.serializer.SerializerFragment.getExportedPackagesRt(SerializerFragment.java:122)
---

line 122 is:
---
return ((String[])Conversions.unwrapArray(((List<String>) _newArrayList)));
---
Comment 2 Sebastian Zarnekow CLA 2011-05-17 16:59:43 EDT
We know the element type of the list and the expected array type statically. That's why we can pass this information to the conversion method (which could take a second parameter of type class which could be used to instantiate the array).
Comment 3 Sebastian Zarnekow CLA 2011-10-17 05:58:57 EDT
Pushed to master.
Comment 4 Karsten Thoms CLA 2017-09-19 17:55:12 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 5 Karsten Thoms CLA 2017-09-19 18:06:07 EDT
Closing all bugs that were set to RESOLVED before Neon.0