Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10189

File.withWriter and Path.withWriter are inconsistent

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.8
    • 4.0.0-beta-1, 3.0.9
    • None
    • None

    Description

      Compare the following:

      1. ResourceGroovyMethods.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);
      }
       

      2. NioExtensions.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);
      } 

      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:

      Paths.get('/tmp/myfile').withWriter { writer ->
          writer.writeLine 'hello world'
      }

      Attachments

        Activity

          People

            paulk Paul King
            rkrisztian rkrisztian
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 20m
                20m