Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent invariant from running before construction of an object is finished #22

Closed
BenRomberg opened this issue May 28, 2012 · 1 comment
Assignees
Milestone

Comments

@BenRomberg
Copy link
Member

Discovered here: http://c2.com/cgi/wiki?TestWhetherInConstructionPhase

@BenRomberg
Copy link
Member Author

"Temporary breaking of class invariance between private method calls is possible, although not encouraged."

This has been fixed.

The other issue with class-invariants being checked during construction of an object can thus only happen after a publicly available method (meaning public, protected or package-private in Java) is being called during construction of an object. As they could also be called during any other moment during the lifecycle of the object, they must be able to assume that the class-invariant holds before being called themselves. With this assumption in mind, it should not be an issue to call the class-invariant after a publicly available method during construction of an object.

Also, the example on c2.com is using another assumption: That the assertValid() method is being overridden in the subclass. The call order will differ between the example and the C4J implementation as follows:

c2.com Example:
Derived();
Base();
{ body of Base() }
Derived.assertValid(); // <-- problem: Invariant called before body of Derived() is being evaluated
Base.assertValid();
{ body of Derived() }
Derived.assertValid();
Base.assertValid();

C4J equivalent:
Derived();
Base();
{ body of Base() }
Base.classInvariant();
{ body of Derived() }
Base.classInvariant();
Derive.classInvariant();

This also shows why doing Contracts in Java without using a framework is quite complicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant