Community
Participate
Working Groups
Example ---- class foo { def bar() { try { newArrayList("file1.ext").map(f| new File(f).canonicalFile) } catch(IOException o) { Collections::<File>emptyList } } } ----- this compiles to ---- public class foo { public List<File> bar() { List<File> _xtrycatchfinallyexpression = null; try { ArrayList<String> _newArrayList = CollectionLiterals.<String>newArrayList("file1.ext"); final Function1<String,File> _function = new Function1<String,File>() { public File apply(final String f) { File _file = new File(f); File _canonicalFile = _file.getCanonicalFile(); return _canonicalFile; } }; List<File> _map = ListExtensions.<String, File>map(_newArrayList, _function); _xtrycatchfinallyexpression = _map; } catch (final IOException o) { List<File> _emptyList = Collections.<File>emptyList(); _xtrycatchfinallyexpression = _emptyList; } return _xtrycatchfinallyexpression; } } ----- the Java code doesn't compile because of two errors - "Unhandled exception type IOException" under "File _canonicalFile = _file.getCanonicalFile();" - "Unreachable catch block for IOException. This exception is never thrown from the try statement body" under "catch (final IOException o) ". Valid Java code could look like this: ----- public List<File> bar() { List<File> _xtrycatchfinallyexpression = null; try { final Function1<String, File> _function = new Function1<String, File>() { public File apply(final String f) { File _canonicalFile; try { _canonicalFile = new File(f).getCanonicalFile(); } catch (IOException e) { Exceptions.sneakyThrow(e); return null; } return _canonicalFile; } }; _xtrycatchfinallyexpression = ListExtensions.<String, File> map( CollectionLiterals.<String> newArrayList("file1.ext"), _function); } catch (final Throwable o) { if (o instanceof IOException) { List<File> _emptyList = Collections.<File> emptyList(); _xtrycatchfinallyexpression = _emptyList; } } return _xtrycatchfinallyexpression; } --- This code catches the IOException and sneaks it out and later it fishes for THwables and checks via instancoef if it's an IOException. This code however has the conceptual problem that there is no guarantee that the closure is executed within the declared try-catch-block. It could be stored in a variable and executed later - after the control flow has left the try-catch-block.
The main problem is solved. However, the outlined scenario still doesn't work, because the caching mechanism remembers wrong results during linking. We'll have to fix/improve the caching strategy. I've added an uncommented test case to Xtend2CompilerTest
*** Bug 365320 has been marked as a duplicate of this bug. ***
fixed in the meantime.
Requested via bug 522520. -M.