From 34d156f082845f78a8115bdf9b0e5e62910aba5e Mon Sep 17 00:00:00 2001 From: Magnus Smith Date: Wed, 18 Dec 2024 16:36:17 +0000 Subject: [PATCH] small edits --- ...18-taming-nullness-in-java-with-jspecify.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/_posts/2024-12-18-taming-nullness-in-java-with-jspecify.md b/_posts/2024-12-18-taming-nullness-in-java-with-jspecify.md index 2e33b1612..4ef791255 100644 --- a/_posts/2024-12-18-taming-nullness-in-java-with-jspecify.md +++ b/_posts/2024-12-18-taming-nullness-in-java-with-jspecify.md @@ -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; } @@ -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 @@ -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; @@ -102,7 +110,7 @@ public class User { } public @NonNull String getName() { return name; } - + public String getFormattedAddress() { String address = getAddress(); if (address != null) { @@ -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. @@ -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.