Skip to content

Commit

Permalink
small edits
Browse files Browse the repository at this point in the history
  • Loading branch information
Magnus Smith committed Dec 18, 2024
1 parent 423b99a commit 5ee65f2
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions _posts/2024-12-18-taming-nullness-in-java-with-jspecify.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private String address;
}
~~~

This User class has a glaring potential NPE in `getFormattedAddress()`. Let's use JSpecify to address this.
This User class has a glaringly obvious NullPointerException in `getFormattedAddress()`. Let's use JSpecify to address this.

## Step 1: Add JSpecify Dependency

Expand Down Expand Up @@ -102,6 +102,10 @@ public class User {
}

public @NonNull String getName() { return name; }

public void setName(@NonNull String name) {
this.name = name;
}

public String getFormattedAddress() {
String address = getAddress();
Expand Down Expand Up @@ -139,8 +143,6 @@ Now, all unannotated types within the User class are treated as `@NonNull`, unle

We can also apply `@NullMarked` and `@NullUnmarked` at the Package and Module Levels. If you needed to exempt a class from the null marked package you would use `@NullUnmarked` on the class you need to exempt.

You would place `@NullMarked` or `@NullUnmarked` in a `package-info.java` file to affect the entire package.

### At the Package Level

You can place `@NullMarked` or `@NullUnmarked` in a package-info.java file to affect the entire package.
Expand Down Expand Up @@ -201,7 +203,7 @@ If you want to use jspecify notifications in generated code then you can set tha
- `Integrates with the compiler`: Works seamlessly with the Java compiler, so you don't need a separate tool or process.
- `Extensible`: Allows you to write custom checks to enforce project-specific coding standards.

Error Prone can be used in many useful ways, even fixing some issues automatically. I shall cover that in more depth in a future post.
Error Prone can be used in many useful ways, even fixing some issues automatically. This will be covered in more depth in a future post.

### NullAway
[NullAway](https://github.com/uber/NullAway) is a static analysis tool built on top of Error Prone specifically designed to detect null pointer dereferences. It leverages annotations (such as JSpecify's `@Nullable` and @NonNull) to understand the nullness constraints of your code and identify potential NPEs.
Expand Down

0 comments on commit 5ee65f2

Please sign in to comment.