-
Notifications
You must be signed in to change notification settings - Fork 7
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
Contracts for static methods #17
Comments
I wouldn't consider this feature necessary for 4.0 (if at all). |
Well, it should be pretty easy to implement and can come in handy for legacy support. The main effort will be writing tests for almost every feature for static methods as well. |
todo: check return value works correctly + contracts for static constructors
Static methods was easy, what's missing is static initializers. Not so easy, as the compiler is initializing the synthetic field $assertionsDisabled using Class.desiredAssertionStatus in the static initializer for every class using an assert statement :-( Will have to find out if this is compiler specific. If it's not, we can safely remove this compiler magic and also make the needed -ea VM argument obsolete. |
I would consider enabling |
You're right, we shouldn't change the assert mechianism as specified. Also, who knows how other compilers handle the -ea argument - maybe there's no such field that's being initialized in the static initializer. However, we can probably assume that if we run the static initializer code in the contract class (being copied to a separate method) as the pre- and post-condition of the static initializer of the target class, that all static initializations are taking place as needed. This is because the static initializer of the target class is being run just before the target class is first being used (in a static or non-static way) and therefore the contract class can't be used earlier than that, making the pre-condition of the static initializer the earliest possible use of the contract class. |
If I remember correctly, you can have multiple static initializers in a single class. How would you write a contract for this? A separate one for each initializer or one preconfition that must hold before the first and a postcondition that must hold after the last? |
Very good point!
So, you'd always write pre- and post-conditions for all static initializers combined, because that's all we know when looking at the bytecode. Another example of bytecode-differences are initializers like this: public class DummyClass {
{
// this will be executed when an object of the class is instantiated
}
} Code in (non-static) initializers like that simply gets inserted at the beginning of every constructor (named |
We could also postpone the ability to write contracts for static initializers to post-4.0-GA. Although it could come in handy, e.g. if you have a static Map being initialized in the static initializer and you want to ensure that there are at least a certain amount of elements, or at least as many as there are elements in an enum, etc. |
My opinion is not really important, but I also think that this is not a must-have feature for 4.0. |
Btw, I double-checked: There can at most be one static initializer |
The only way this seems possible while keeping the ability to refactor is with an annotation on the static method, e.g.:
This would not be possible when using external contracts, where a naming-convention is the only thing I can come up with. On the other hand, static methods should be rarely used. Maybe the Annotation-hassle isn't worth the effort at all.
The text was updated successfully, but these errors were encountered: