Giter VIP home page Giter VIP logo

Comments (10)

marcphilipp avatar marcphilipp commented on July 20, 2024

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

from c4j.

BenRomberg avatar BenRomberg commented on July 20, 2024

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.

from c4j.

BenRomberg avatar BenRomberg commented on July 20, 2024

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.

from c4j.

marcphilipp avatar marcphilipp commented on July 20, 2024

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

from c4j.

BenRomberg avatar BenRomberg commented on July 20, 2024

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.

from c4j.

marcphilipp avatar marcphilipp commented on July 20, 2024

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?

from c4j.

BenRomberg avatar BenRomberg commented on July 20, 2024

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.

from c4j.

BenRomberg avatar BenRomberg commented on July 20, 2024

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.

from c4j.

marcphilipp avatar marcphilipp commented on July 20, 2024

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

from c4j.

BenRomberg avatar BenRomberg commented on July 20, 2024

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

from c4j.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.