Skip to content

Plugin for Java 21 to create some shortcuts for guardian clauses.

Notifications You must be signed in to change notification settings

inglettronald/guard

Repository files navigation

Guard

This is the home of a java plugin I want to work on. Currently, this is still in early concept stages, but I wrote out this README because I thought it would be fun. The current state of the project is only as far as having the toolchain set up (ty @lea89 for the help there!) and writing a basic proof-of-concept annotation parser.

RoadMap:

  • Implement a basic @Guard.Null annotation for variable declaration that null checks the resolved value of that line.
  • Expand that system to check each step along the way (see usages, this is psuedo optional chaining)
  • Implement safe returns out of Expression statements with null checks at each step along the way
  • Implement error handling for improper annotation usage
  • See if It's possible to supply a consumer as an annotation argument?!? Would be cool to modify the early exit code.
  • Implement some other types of annotations, such as Guard.Empty, Guard.Exception, etc. A lot of this is still pretty loose as I'll need to know how much I can abuse the parameters.

Example Developer Facing Usages

Variable nullability:

private void foo() {
    @Guard.Null String idString = this.id;
    ...
}
// Maps to...
private void foo() {
    String idString = this.id;
    if (idString == null) {
        return;
    }
}

Chaining, and functions:

private void foo() {
    @Guard.Null char firstIdChar = this.id.charAt(0);
    ...
}
// Maps to...
private void foo() {
    String idString = this.id; // likely some other generated, less helpful name
    if (idString == null) {
        return;
    }
    char firstIdChar = idString.charAt(0);
    if (firstIdChar == null) {
        return;
    }
    ...
}

Chaining, functions, and emptiness:

private void foo() {
    @Guard.Empty
    @Guard.Null 
    char firstIdChar = this.id.charAt(0);
    ...
}
// Maps to...
private void foo() {
    String idString = this.id; // likely some other generated, less helpful name
    if (idString == null) {
        return;
    }
    if (idString.isEmpty()) {
        return;
    }
    char firstIdChar = idString.charAt(0);
    if (firstIdChar == null) {
        return;
    }
    ...
}

And hopefully more! This project will probably take some time, considering I'm new to compiler plugins, and I'm only able to do this when I have free time outside of work. However, I think it would be a novel (and useful) idea to bring optional chaining out of kotlin and into java. Considering that projects like Lombok have gotten so popular as well, I feel like there might be some people who also would appreciate the boiler-plate reduction that this would bring?

There is probably some valid criticism to this idea in that it's a bit silly to use a verbose language if you don't want to be verbose. However, I counter this with the super thought out "choice is cool" argument.

About

Plugin for Java 21 to create some shortcuts for guardian clauses.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages