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

Enhance the named argument constructors related to @Immutable to throw MissingPropertyException

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.7.0
    • 1.7.2, 1.8-beta-1
    • None
    • None
    • Any, I guess.
      I'm using Sun's Java6 on Linux.

    Description

      I just discovered the @Immutable annotation and migrated some of my classes to it. Unfortunately, I realized that the behavior of the constructor changes slightly: For ordinary groovy beans, I used to get a MissingPropertyException when specifying a named parameter for a non-existing property. For @Immutable groovy beans, the parameter is silently ignored.
      Here an example which shows the error:

      @Immutable class ImmutablePerson {
          String name
      }
      
      class Person {
      	String name
      }
      
      Person p = new Person(name: "uli"); // works fine
      assert "uli" == p.name;
      ImmutablePerson ip = new ImmutablePerson(name: "uli"); // works fine, too
      assert "uli" == ip.name;
      
      try {
        Person p2 = new Person(name: "uli", lastname: "heller"); // throws an exception, fine
        assert false;
      } catch (MissingPropertyException mpe) {
        assert true;
      }
      
      try {
        ImmutablePerson ip2 = new ImmutablePerson(name: "uli", lastname: "heller"); // does not throw an exception
        assert false; // ... so it fails within this assertion
      } catch (MissingPropertyException mpe) {
        assert true;
      }
      

      Attachments

        1. Immutable.groovy
          0.6 kB
          Uli Heller

        Activity

          People

            paulk Paul King
            uli42 Uli Heller
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: