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

Change all withXXX methods to return the value returned by their closure instead of void

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 1.5.2
    • groovy-jdk
    • None
    • Patch

    Description

      As it now stands, the withXXX methods like withReader all return void. It would be better if they returned the value yielded by their closure, if any. This would allow code like the following JCE code:

      def f = new File("cert.der")
      def cf = CertificateFactory.getInstance("X.509")
      def cert = f.withInputStream { cf.generateCertificate(it) }
      

      I have attached a patch that changes all of the withXXX method to return Object instead of void. The only effect this should have on existing code will be if someone has compiled Java classes that are calling these DGM methods directly; in that case, they will get a NoSuchMethodError. It shouldn't effect existing Groovy code unless it was relying on these methods returning null.

      The patch includes the new methods URL.newInputStream and URL.withInputStream that are proposed in GROOVY-2471. If this ticket is accepted, it may be best to just apply this patch and ignore the other one.

      The patch also includes very basic test cases for File.withReader, File.withInputStream, URL.withReader, and URL.withInputStream.

      Attachments

        1. return-from-with.diff
          18 kB
          Mike Dillon

        Issue Links

          Activity

            People

              paulk Paul King
              md5 Mike Dillon
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: