JADEx (Java Advanced Development Extension) is a safety layer that runs on top of Java.
It currently supports up to Java 25 syntax and extends it with additional Null-Safety and Immutability features.

In the previous article, I introduced the Null-Safety features.
For more details, please refer to:


Introducing the New Immutability Feature

If Null-Safety eliminates runtime crashes caused by null,
Immutability reduces bugs caused by unintended state changes.

With v0.41 release, JADEx introduces Immutable by Default Mode


Core Concepts

The Immutability feature revolves around two simple additions:

apply immutability;
mutable

apply immutability;

  • When you declare this at the top of your source file:

    • All fields
    • All local variables (excluding method parameters)
    • are treated as immutable by default.
  • When the JADEx compiler generates Java code:

    • They are automatically declared as final.

mutable keyword

  • Only variables declared with mutable remain changeable.
  • Everything else (excluding method parameters) is immutable by default.

JADEx Source Code


package jadex.example;

apply immutability;

public class Immutability {

    private int capacity = 2; // immutable
    private String msg = "immutable"; // immutable

    private int uninitializedCapacity; // uninitialaized immutable
    private String uninitializedMsg; // uninitialaized immutable

    private mutable String mutableMsg = "mutable";  // mutable

    public static void main(String[] args) {
        var immutable = new Immutability();

         immutable.capacity = 10; //error
         immutable.msg = "new immutable"; //error

         immutable.mutableMsg = "changed mutable";

        System.out.println("mutableMsg: " + immutable.mutableMsg);
        System.out.println("capacity: " + immutable.capacity);
        System.out.println("msg: " + immutable.msg);
    }
}

Generated Java Code

package jadex.example;

//apply immutability;

public class Immutability {

    private final int capacity = 2; // immutable
    private final String msg = "immutable"; // immutable

    private final int uninitializedCapacity; // uninitialaized immutable
    private final String uninitializedMsg; // uninitialaized immutable

    private String mutableMsg = "mutable";  // mutable

    public static void main(String[] args) {
        final var immutable = new Immutability();

         immutable.capacity = 10; //error
         immutable.msg = "new immutable"; //error

         immutable.mutableMsg = "changed mutable";

        System.out.println("mutableMsg: " + immutable.mutableMsg);
        System.out.println("capacity: " + immutable.capacity);
        System.out.println("msg: " + immutable.msg);
    }
}

This feature is available starting from JADEx v0.41. Since the IntelliJ Plugin for JADEx v0.41 has not yet been published on the JetBrains Marketplace, if you wish to try it, please download the JADEx IntelliJ Plugin from the link below and install it manually.

JADEx v0.41 IntelliJ Plugin

We highly welcome your feedback on the newly added Immutability feature.

Finally, your support is a great help in keeping this project alive and thriving.

Thank you.

  • JADExOP
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    11 hours ago

    JADEx is a solution designed to enhance null-safety in Java. Its key advantage is that existing Java developers can gain null-safety and immutability without any learning curve. As a result, compared to migrating to Kotlin, JADEx offers a much more cost-effective way to significantly improve the stability of the legacy Java codebases that many companies continue to operate.

    In addition, JADEx provides immutability, allowing developers to strengthen immutability without the verbose use of the final keyword.