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

Having a setter with a return value causes the stub generator to generate multiple setters and then fail to compile

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.7.6, 1.8-beta-3
    • Fix Version/s: 1.8.1, 1.7.11
    • Component/s: Compiler
    • Labels:
      None

      Description

      Having a class with a property and then explicitly defining the setter but giving it a return value (common for Builder classes) will compile fine with groovyc without joint compiling. When using joint compiling the java stubs generated are invalid. They end up with multiple setters - one normal void setter and one with the return value. Obviously this is not valid code. See the following example:

      class SetterWithReturn {
        String foo
        def String bar
      
        SetterWithReturn setFoo(String foo) { this.foo = foo; return this; }
        SetterWithReturn setBar(String bar) { this.bar = bar; return this; }
      }
      
      public class SetterWithReturnClient {
          { new SetterWithReturn(); }
      }
      

      Here is the error the compiler gives:

      C:\IdeaProjects\groovy-bugs>groovyc -j src\main\groovy\SetterWithReturn.groovy src\main\java\SetterWithReturnC
      lient.java
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Compile error during compilation with javac.
      C:\DOCUME~1\schnee13\LOCALS~1\Temp\groovy-generated-1842144177968937199-java-source\SetterWithReturn.java:22:
      setFoo(java.lang.String) is already defined in SetterWithReturn
      public  SetterWithReturn setFoo(java.lang.String foo) { return (SetterWithReturn)null;}
                               ^
      C:\DOCUME~1\schnee13\LOCALS~1\Temp\groovy-generated-1842144177968937199-java-source\SetterWithReturn.java:23:
      setBar(java.lang.String) is already defined in SetterWithReturn
      public  SetterWithReturn setBar(java.lang.String bar) { return (SetterWithReturn)null;}
                               ^
      2 errors
      
      
      1 error
      
      

        Attachments

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              leo.herbie Travis Schneeberger
            • Votes:
              2 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: