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 19, 2024
1 parent 423b99a commit 34d156f
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions _posts/2024-12-18-taming-nullness-in-java-with-jspecify.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ private String name;
private String address;

public User(String name) {
if (name == null) {
throw new IllegalArgumentException("Name cannot be null");
}
this.name = name;
}

Expand All @@ -64,7 +67,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 All @@ -91,7 +94,12 @@ import org.jspecify.nullness.Nullable;
import org.jspecify.nullness.NonNull;

public class User {
// ... other code
public User(String name) {
if (name == null) {
throw new IllegalArgumentException("Name cannot be null");
}
this.name = name;
}

public @Nullable String getAddress() {
return address;
Expand All @@ -102,7 +110,7 @@ public class User {
}

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

public String getFormattedAddress() {
String address = getAddress();
if (address != null) {
Expand Down Expand Up @@ -139,8 +147,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 +207,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 34d156f

Please sign in to comment.