JADEx v0.41: Introducing Immutability for Safer Java Code (github.com)
from justicecoder@programming.dev to programming@programming.dev on 26 Feb 16:20
https://programming.dev/post/46376374

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;

mutable keyword

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.

#programming

threaded - newest

FrostyPolicy@suppo.fi on 26 Feb 17:57 collapse

Based on the readme this basically brings some Kotlin null handling and immutability as an add-on to Java projects. For a fresh project I recommend Kotlin. For an existing project this might add some value.

As for immutability Java already has options for that like the final keyword or immutable collections.

Overall an interesting idea. NPEs are way too common in Java code. Even the original creator of null feels it was a mistake to begin with.

justicecoder@programming.dev on 26 Feb 21:01 collapse

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.