groovy

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

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.7.0
  • Fix Version/s: 1.7.2, 1.8-beta-1
  • Component/s: None
  • Labels:
    None
  • Environment:
    Any, I guess.
    I'm using Sun's Java6 on Linux.
  • Testcase included:
    yes
  • Number of attachments :
    1

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;
}

Activity

Hide
Uli Heller added a comment -

A groovy script with some assertions which fail due to missing MissingPropertyException.

Show
Uli Heller added a comment - A groovy script with some assertions which fail due to missing MissingPropertyException.
Hide
Uli Heller added a comment -

Unfortunately, the source code included above is screwed up. I've created an attachment containing the test script. Hopefully, this is easier to read.

Show
Uli Heller added a comment - Unfortunately, the source code included above is screwed up. I've created an attachment containing the test script. Hopefully, this is easier to read.
Hide
Paul King added a comment -

Added. Thanks for the suggestion.

Show
Paul King added a comment - Added. Thanks for the suggestion.
Hide
Uli Heller added a comment -

Great! I verified the fix on groovy-1.7.2. Thanks a lot, I'll upgrade my mini project to 1.7.2 right away.

Best regards, Uli

Show
Uli Heller added a comment - Great! I verified the fix on groovy-1.7.2. Thanks a lot, I'll upgrade my mini project to 1.7.2 right away. Best regards, Uli

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: