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

Contracts for static methods #17

Closed
BenRomberg opened this issue May 14, 2012 · 10 comments
Closed

Contracts for static methods #17

BenRomberg opened this issue May 14, 2012 · 10 comments
Assignees
Milestone

Comments

@BenRomberg
Copy link
Member

The only way this seems possible while keeping the ability to refactor is with an annotation on the static method, e.g.:

public class SomeClass {
  @StaticContractReference(StaticMethodContract.class)
  public static void staticMethod() {
    // ...
  }
}

public class StaticMethodContract {
  @StaticContract
  public void contractMethod() {
    if (preCondition()) {
      assert ...;
    }
    // ...
  }
}

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.

@marcphilipp
Copy link
Contributor

I wouldn't consider this feature necessary for 4.0 (if at all).

@BenRomberg
Copy link
Member Author

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.

BenRomberg pushed a commit that referenced this issue May 27, 2012
todo: check return value works correctly + contracts for static
constructors
@ghost ghost assigned BenRomberg May 28, 2012
@BenRomberg
Copy link
Member Author

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.

@marcphilipp
Copy link
Contributor

I would consider enabling assert without the -ea VM argument magic (which I rather dislike when it comes to programming)…

@BenRomberg
Copy link
Member Author

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.

@marcphilipp
Copy link
Contributor

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?

@BenRomberg
Copy link
Member Author

Very good point!

  • Yes, you can have multiple static initializers.
  • javac combines them into one static initializer at compile-time.
  • I'm pretty sure that multiple static initializers aren't possible in byte-code (as they're named <clinit> and you cannot have multiple methods with the same name), but I'll double-check to be safe.

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 <init>) in the bytecode. So there's no way we can write contracts for those initializer-blocks alone - they'll always have to be included with the constructor.

@BenRomberg
Copy link
Member Author

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.

@marcphilipp
Copy link
Contributor

My opinion is not really important, but I also think that this is not a must-have feature for 4.0.

@BenRomberg
Copy link
Member Author

Btw, I double-checked: There can at most be one static initializer <clinit> in bytecode.

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

2 participants