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

CompileStatic: string concatenation in method parameter default value results in NoSuchMethodError

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 4.0.9, 4.0.11
    • 4.0.12
    • None

    Description

      If a method parameter's default value is defined by concatenating a String literal to a String variable, calling the method throws java.lang.NoSuchMethodError.

      import groovy.transform.CompileStatic
      import spock.lang.Specification
      
      @CompileStatic  // works without CompileStatic
      class MyClass {
          String makePlural(String word, String plural = word + "s") {  // java.lang.NoSuchMethodError: 'java.lang.String java.lang.String.plus(java.lang.CharSequence)'
              return plural
          }
      }
      
      class Test extends Specification {
          def test() {
              setup:
                  MyClass myObj = new MyClass()
              expect:
                  myObj.makePlural("word") == "words"
          }
      }    

      Live demo.

      A workaround is to do the concatenation in a GString:

      String makePlural(String word, String plural = "${word}s") { 

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              jasongarrett Jason Garrett
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: