| 1 | package com.reallifedeveloper.common.domain; | |
| 2 | ||
| 3 | import java.util.Arrays; | |
| 4 | ||
| 5 | import org.checkerframework.checker.nullness.qual.EnsuresNonNull; | |
| 6 | import org.checkerframework.checker.nullness.qual.Nullable; | |
| 7 | ||
| 8 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | |
| 9 | import lombok.experimental.UtilityClass; | |
| 10 | ||
| 11 | /** | |
| 12 | * Utility class for simplifying error handling. | |
| 13 | * | |
| 14 | * @author RealLifeDeveloper | |
| 15 | */ | |
| 16 | @UtilityClass | |
| 17 | @SuppressWarnings({ "NullAway", "PMD.AvoidDuplicateLiterals" }) | |
| 18 | public class ErrorHandling { | |
| 19 | ||
| 20 | /** | |
| 21 | * Fails if {@code arg1} is {@code null}, throwing an {@code IllegalArgumentException} with a formatted message based on | |
| 22 | * {@code messageTemplate}. | |
| 23 | * | |
| 24 | * @param messageTemplate a {@code String.format()} format string that will be formatted using {@code arg1} | |
| 25 | * @param arg1 the argument to check for nullness | |
| 26 | * | |
| 27 | * @throws IllegalArgumentException if {@code arg1} is {@code null} | |
| 28 | * @throws IllegalStateException if {@code messageTemplate} is {@code null} | |
| 29 | */ | |
| 30 | @EnsuresNonNull({ "#2" }) | |
| 31 | @SuppressWarnings("nullness") | |
| 32 | public static void checkNull(String messageTemplate, @Nullable Object arg1) { | |
| 33 |
1
1. checkNull : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNullInternal → KILLED |
checkNullInternal(messageTemplate, arg1); |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * Fails if {@code arg1} or {@code arg2} is {@code null}, throwing an {@code IllegalArgumentException} with a formatted message based on | |
| 38 | * {@code messageTemplate}. | |
| 39 | * | |
| 40 | * @param messageTemplate a {@code String.format()} format string that will be formatted using {@code arg1} and {@code arg2} | |
| 41 | * @param arg1 the first argument to check for nullness | |
| 42 | * @param arg2 the second argument to check for nullness | |
| 43 | * | |
| 44 | * @throws IllegalArgumentException if {@code arg1} or {@code arg2} is {@code null} | |
| 45 | * @throws IllegalStateException if {@code messageTemplate} is {@code null} | |
| 46 | */ | |
| 47 | @EnsuresNonNull({ "#2", "#3" }) | |
| 48 | @SuppressWarnings("nullness") | |
| 49 | public static void checkNull(String messageTemplate, @Nullable Object arg1, @Nullable Object arg2) { | |
| 50 |
1
1. checkNull : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNullInternal → KILLED |
checkNullInternal(messageTemplate, arg1, arg2); |
| 51 | } | |
| 52 | ||
| 53 | /** | |
| 54 | * Fails if {@code arg1}, {@code arg2} or {@code arg3} is {@code null}, throwing an {@code IllegalArgumentException} with a formatted | |
| 55 | * message based on {@code messageTemplate}. | |
| 56 | * | |
| 57 | * @param messageTemplate a {@code String.format()} format string that will be formatted using {@code arg1}, {@code arg2} and | |
| 58 | * {@code arg3} | |
| 59 | * @param arg1 the first argument to check for nullness | |
| 60 | * @param arg2 the second argument to check for nullness | |
| 61 | * @param arg3 the third argument to check for nullness | |
| 62 | * | |
| 63 | * @throws IllegalArgumentException if {@code arg1}, {@code arg2} or {@code arg3} is {@code null} | |
| 64 | * @throws IllegalStateException if {@code messageTemplate} is {@code null} | |
| 65 | */ | |
| 66 | @EnsuresNonNull({ "#2", "#3", "#4" }) | |
| 67 | @SuppressWarnings("nullness") | |
| 68 | public static void checkNull(String messageTemplate, @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3) { | |
| 69 |
1
1. checkNull : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNullInternal → KILLED |
checkNullInternal(messageTemplate, arg1, arg2, arg3); |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Fails if {@code arg1}, {@code arg2}, {@code arg3} or {@code arg4} is {@code null}, throwing an {@code IllegalArgumentException} with | |
| 74 | * a formatted message based on {@code messageTemplate}. | |
| 75 | * | |
| 76 | * @param messageTemplate a {@code String.format()} format string that will be formatted using {@code arg1}, {@code arg2}, {@code arg3} | |
| 77 | * and {@code arg4} | |
| 78 | * @param arg1 the first argument to check for nullness | |
| 79 | * @param arg2 the second argument to check for nullness | |
| 80 | * @param arg3 the third argument to check for nullness | |
| 81 | * @param arg4 the third argument to check for nullness | |
| 82 | * | |
| 83 | * @throws IllegalArgumentException if {@code arg1}, {@code arg2}, {@code arg3} or {@code arg4} is {@code null} | |
| 84 | * @throws IllegalStateException if {@code messageTemplate} is {@code null} | |
| 85 | */ | |
| 86 | @EnsuresNonNull({ "#2", "#3", "#4", "#5" }) | |
| 87 | @SuppressWarnings({ "nullness" }) | |
| 88 | public static void checkNull(String messageTemplate, @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3, | |
| 89 | @Nullable Object arg4) { | |
| 90 |
1
1. checkNull : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNullInternal → KILLED |
checkNullInternal(messageTemplate, arg1, arg2, arg3, arg4); |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * Fails if any of {@code arg1} through {@code arg5} is {@code null}, throwing an {@code IllegalArgumentException} with a formatted | |
| 95 | * message based on {@code messageTemplate}. | |
| 96 | * | |
| 97 | * @param messageTemplate a {@code String.format()} format string that will be formatted using {@code arg1} through {@code arg5}. | |
| 98 | * @param arg1 the first argument to check for nullness | |
| 99 | * @param arg2 the second argument to check for nullness | |
| 100 | * @param arg3 the third argument to check for nullness | |
| 101 | * @param arg4 the fourth argument to check for nullness | |
| 102 | * @param arg5 the fifth argument to check for nullness | |
| 103 | * | |
| 104 | * @throws IllegalArgumentException if any of {@code arg1} through {@code arg5} is {@code null} | |
| 105 | * @throws IllegalStateException if {@code messageTemplate} is {@code null} | |
| 106 | */ | |
| 107 | @EnsuresNonNull({ "#2", "#3", "#4", "#5", "#6" }) | |
| 108 | @SuppressWarnings({ "nullness" }) | |
| 109 | public static void checkNull(String messageTemplate, @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3, | |
| 110 | @Nullable Object arg4, @Nullable Object arg5) { | |
| 111 |
1
1. checkNull : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNullInternal → KILLED |
checkNullInternal(messageTemplate, arg1, arg2, arg3, arg4, arg5); |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * Fails if any of {@code arg1} through {@code arg6} is {@code null}, throwing an {@code IllegalArgumentException} with a formatted | |
| 116 | * message based on {@code messageTemplate}. | |
| 117 | * | |
| 118 | * @param messageTemplate a {@code String.format()} format string that will be formatted using {@code arg1} through {@code arg6}. | |
| 119 | * @param arg1 the first argument to check for nullness | |
| 120 | * @param arg2 the second argument to check for nullness | |
| 121 | * @param arg3 the third argument to check for nullness | |
| 122 | * @param arg4 the fourth argument to check for nullness | |
| 123 | * @param arg5 the fifth argument to check for nullness | |
| 124 | * @param arg6 the sixth argument to check for nullness | |
| 125 | * | |
| 126 | * @throws IllegalArgumentException if any of {@code arg1} through {@code arg6} is {@code null} | |
| 127 | * @throws IllegalStateException if {@code messageTemplate} is {@code null} | |
| 128 | */ | |
| 129 | @EnsuresNonNull({ "#2", "#3", "#4", "#5", "#6", "#7" }) | |
| 130 | @SuppressWarnings({ "nullness" }) | |
| 131 | public static void checkNull(String messageTemplate, @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3, | |
| 132 | @Nullable Object arg4, @Nullable Object arg5, @Nullable Object arg6) { | |
| 133 |
1
1. checkNull : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNullInternal → KILLED |
checkNullInternal(messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6); |
| 134 | } | |
| 135 | ||
| 136 | /** | |
| 137 | * Fails if any of {@code arg1} through {@code arg7} is {@code null}, throwing an {@code IllegalArgumentException} with a formatted | |
| 138 | * message based on {@code messageTemplate}. | |
| 139 | * | |
| 140 | * @param messageTemplate a {@code String.format()} format string that will be formatted using {@code arg1} through {@code arg7}. | |
| 141 | * @param arg1 the first argument to check for nullness | |
| 142 | * @param arg2 the second argument to check for nullness | |
| 143 | * @param arg3 the third argument to check for nullness | |
| 144 | * @param arg4 the fourth argument to check for nullness | |
| 145 | * @param arg5 the fifth argument to check for nullness | |
| 146 | * @param arg6 the sixth argument to check for nullness | |
| 147 | * @param arg7 the seventh argument to check for nullness | |
| 148 | * | |
| 149 | * @throws IllegalArgumentException if any of {@code arg1} through {@code arg7} is {@code null} | |
| 150 | * @throws IllegalStateException if {@code messageTemplate} is {@code null} | |
| 151 | */ | |
| 152 | @EnsuresNonNull({ "#2", "#3", "#4", "#5", "#6", "#7", "#8" }) | |
| 153 | @SuppressWarnings({ "nullness" }) | |
| 154 | public static void checkNull(String messageTemplate, @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3, | |
| 155 | @Nullable Object arg4, @Nullable Object arg5, @Nullable Object arg6, @Nullable Object arg7) { | |
| 156 |
1
1. checkNull : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNullInternal → KILLED |
checkNullInternal(messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7); |
| 157 | } | |
| 158 | ||
| 159 | /** | |
| 160 | * Fails if any of {@code arg1} through {@code arg8} is {@code null}, throwing an {@code IllegalArgumentException} with a formatted | |
| 161 | * message based on {@code messageTemplate}. | |
| 162 | * | |
| 163 | * @param messageTemplate a {@code String.format()} format string that will be formatted using {@code arg1} through {@code arg8}. | |
| 164 | * @param arg1 the first argument to check for nullness | |
| 165 | * @param arg2 the second argument to check for nullness | |
| 166 | * @param arg3 the third argument to check for nullness | |
| 167 | * @param arg4 the fourth argument to check for nullness | |
| 168 | * @param arg5 the fifth argument to check for nullness | |
| 169 | * @param arg6 the sixth argument to check for nullness | |
| 170 | * @param arg7 the seventh argument to check for nullness | |
| 171 | * @param arg8 the eigth argument to check for nullness | |
| 172 | * | |
| 173 | * @throws IllegalArgumentException if any of {@code arg1} through {@code arg8} is {@code null} | |
| 174 | * @throws IllegalStateException if {@code messageTemplate} is {@code null} | |
| 175 | */ | |
| 176 | @EnsuresNonNull({ "#2", "#3", "#4", "#5", "#6", "#7", "#8", "#9" }) | |
| 177 | @SuppressWarnings({ "nullness" }) | |
| 178 | public static void checkNull(String messageTemplate, @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3, | |
| 179 | @Nullable Object arg4, @Nullable Object arg5, @Nullable Object arg6, @Nullable Object arg7, @Nullable Object arg8) { | |
| 180 |
1
1. checkNull : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNullInternal → KILLED |
checkNullInternal(messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); |
| 181 | } | |
| 182 | ||
| 183 | @SuppressWarnings("AnnotateFormatMethod") | |
| 184 | @SuppressFBWarnings(value = "FORMAT_STRING_MANIPULATION", justification = "The format string is provided by the programmer, not user") | |
| 185 | private static void checkNullInternal(String messageTemplate, @Nullable Object... arguments) { | |
| 186 | // We know that arguments will never be null, it will always be an array, since | |
| 187 | // this method is only called using varargs | |
| 188 |
1
1. checkNullInternal : negated conditional → KILLED |
if (messageTemplate == null) { |
| 189 | throw new IllegalStateException( | |
| 190 | String.format("checkNull called incorrectly: messageTemplate=null, arguments=%s", Arrays.asList(arguments))); | |
| 191 | } | |
| 192 | for (Object argument : arguments) { | |
| 193 |
1
1. checkNullInternal : negated conditional → KILLED |
if (argument == null) { |
| 194 | String errorMessage = String.format(messageTemplate, arguments); | |
| 195 | throw new IllegalArgumentException(errorMessage); | |
| 196 | } | |
| 197 | } | |
| 198 | } | |
| 199 | ||
| 200 | /** | |
| 201 | * Fails if the string {@code s} is {@code null} or blank, throwing an {@code IllegalArgumentException} with a formatted message based | |
| 202 | * on {@code messageTemplate}. | |
| 203 | * | |
| 204 | * @param messageTemplate a {@code String.format()} format string that will be formatted using {@code s} | |
| 205 | * @param s the string to check for nullness or blankness | |
| 206 | * | |
| 207 | * @throws IllegalArgumentException if {@code s} is {@code null} or blank, i.e., empty or only containing whitespace | |
| 208 | * @throws IllegalStateException if {@code messageTemplate} is {@code null} | |
| 209 | */ | |
| 210 | @EnsuresNonNull({ "#2" }) | |
| 211 | @SuppressFBWarnings(value = "FORMAT_STRING_MANIPULATION", justification = "The format string is provided by the programmer, not user") | |
| 212 | public static void checkNullOrBlank(String messageTemplate, @Nullable String s) { | |
| 213 |
1
1. checkNullOrBlank : negated conditional → KILLED |
if (messageTemplate == null) { |
| 214 | throw new IllegalStateException(String.format("checkNullOrBlank called incorrectly: messageTemplate=null, s='%s'", s)); | |
| 215 | } | |
| 216 |
2
1. checkNullOrBlank : negated conditional → KILLED 2. checkNullOrBlank : negated conditional → KILLED |
if (s == null || s.isBlank()) { |
| 217 | throw new IllegalArgumentException(String.format(messageTemplate, s)); | |
| 218 | } | |
| 219 | } | |
| 220 | } | |
Mutations | ||
| 33 |
1.1 |
|
| 50 |
1.1 |
|
| 69 |
1.1 |
|
| 90 |
1.1 |
|
| 111 |
1.1 |
|
| 133 |
1.1 |
|
| 156 |
1.1 |
|
| 180 |
1.1 |
|
| 188 |
1.1 |
|
| 193 |
1.1 |
|
| 213 |
1.1 |
|
| 216 |
1.1 2.2 |