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({ "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 arg7}. | |
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} throuch {@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 arg7}. | |
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} throuch {@code arg7} 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} throuch {@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 arg7}. | |
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} throuch {@code arg7} 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 | @SuppressFBWarnings(value = "FORMAT_STRING_MANIPULATION", justification = "The format string is provided by the programmer, not user") | |
184 | private static void checkNullInternal(String messageTemplate, @Nullable Object... arguments) { | |
185 | // We know that arguments will never be null, it will always be an array, since | |
186 | // this method is only called using varargs | |
187 |
1
1. checkNullInternal : negated conditional → KILLED |
if (messageTemplate == null) { |
188 | throw new IllegalStateException( | |
189 | String.format("checkNull called incorrectly: messageTemplate=null, arguments=%s", Arrays.asList(arguments))); | |
190 | } | |
191 | for (Object argument : arguments) { | |
192 |
1
1. checkNullInternal : negated conditional → KILLED |
if (argument == null) { |
193 | String errorMessage = String.format(messageTemplate, arguments); | |
194 | throw new IllegalArgumentException(errorMessage); | |
195 | } | |
196 | } | |
197 | } | |
198 | ||
199 | /** | |
200 | * Fails if the string {@code s} is {@code null} or blank, throwing an {@code IllegalArgumentException} with a formatted message based | |
201 | * on {@code messageTemplate}. | |
202 | * | |
203 | * @param messageTemplate a {@code String.format()} format string that will be formatted using {@code s} | |
204 | * @param s the string to check for nullness or blankness | |
205 | * | |
206 | * @throws IllegalArgumentException if {@code s} is {@code null} or blank, i.e., empty or only containing whitespace | |
207 | * @throws IllegalStateException if {@code messageTemplate} is {@code null} | |
208 | */ | |
209 | @EnsuresNonNull({ "#2" }) | |
210 | @SuppressFBWarnings(value = "FORMAT_STRING_MANIPULATION", justification = "The format string is provided by the programmer, not user") | |
211 | public static void checkNullOrBlank(String messageTemplate, @Nullable String s) { | |
212 |
1
1. checkNullOrBlank : negated conditional → KILLED |
if (messageTemplate == null) { |
213 | throw new IllegalStateException(String.format("checkNullOrBlank called incorrectly: messageTemplate=null, s='%s'", s)); | |
214 | } | |
215 |
2
1. checkNullOrBlank : negated conditional → KILLED 2. checkNullOrBlank : negated conditional → KILLED |
if (s == null || s.isBlank()) { |
216 | throw new IllegalArgumentException(String.format(messageTemplate, s)); | |
217 | } | |
218 | } | |
219 | } | |
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 |
|
187 |
1.1 |
|
192 |
1.1 |
|
212 |
1.1 |
|
215 |
1.1 2.2 |