You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2022/02/03 22:35:01 UTC
[jira] [Closed] (GROOVY-10189) File.withWriter and Path.withWriter are inconsistent
[ https://issues.apache.org/jira/browse/GROOVY-10189?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Paul King closed GROOVY-10189.
------------------------------
> File.withWriter and Path.withWriter are inconsistent
> ----------------------------------------------------
>
> Key: GROOVY-10189
> URL: https://issues.apache.org/jira/browse/GROOVY-10189
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 3.0.8
> Reporter: rkrisztian
> Assignee: Paul King
> Priority: Major
> Fix For: 4.0.0-beta-1, 3.0.9
>
> Time Spent: 20m
> Remaining Estimate: 0h
>
> Compare the following:
> 1. {{ResourceGroovyMethods.java}}:
> {code:java}
> /**
> * Creates a new BufferedWriter for this file, passes it to the closure, and
> * ensures the stream is flushed and closed after the closure returns.
> *
> * @param file a File
> * @param closure a closure
> * @return the value returned by the closure
> * @throws IOException if an IOException occurs.
> * @since 1.5.2
> */
> public static <T> T withWriter(File file, @ClosureParams(value = SimpleType.class, options = "java.io.BufferedWriter") Closure<T> closure) throws IOException {
> return IOGroovyMethods.withWriter(newWriter(file), closure);
> }
> {code}
> 2. {{NioExtensions.java}}:
> {code:java}
> /**
> * Creates a new BufferedWriter for this file, passes it to the closure, and
> * ensures the stream is flushed and closed after the closure returns.
> * The writer will not write a BOM.
> *
> * @param self a Path
> * @param closure a closure
> * @return the value returned by the closure
> * @throws java.io.IOException if an IOException occurs.
> * @since 2.3.0
> */
> public static <T> T withWriter(Path self, @ClosureParams(value = SimpleType.class, options = "java.io.Writer") Closure<T> closure) throws IOException {
> return withWriter(self, Charset.defaultCharset().name(), closure);
> } {code}
> The closure parameter should be a {{BufferedWriter}} in the 2nd case, not just a {{Writer}}. Because of this, IntelliJ IDEA reports a syntax error for this code:
> {code:groovy}
> Paths.get('/tmp/myfile').withWriter { writer ->
> writer.writeLine 'hello world'
> }{code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)