You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"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.
Discovered here: http://c2.com/cgi/wiki?TestWhetherInConstructionPhase
The text was updated successfully, but these errors were encountered: