From 5ee65f2ee4706f72024914da07aeedb748c584bd Mon Sep 17 00:00:00 2001 From: Magnus Smith Date: Wed, 18 Dec 2024 16:36:17 +0000 Subject: [PATCH] small edits --- ...2024-12-18-taming-nullness-in-java-with-jspecify.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 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..17bd21268 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 @@ -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 @@ -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(); @@ -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. @@ -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.