4
4
*/
5
5
package org .hibernate .processor .util ;
6
6
7
- import org .checkerframework .checker .nullness .qual .EnsuresNonNull ;
8
- import org .checkerframework .checker .nullness .qual .NonNull ;
9
- import org .checkerframework .checker .nullness .qual .Nullable ;
10
- import org .checkerframework .framework .qual .AnnotatedFor ;
7
+ import org .jspecify .annotations .Nullable ;
8
+
9
+ import static java .util .Objects .requireNonNull ;
11
10
12
11
/**
13
12
* Utility class for the Nullness Checker.
23
22
* <p><b>Runtime Dependency</b>: If you use this class, you must distribute (or link to) {@code
24
23
* checker-qual.jar}, along with your binaries. Or, you can copy this class into your own project.
25
24
*/
26
- @ SuppressWarnings ({
27
- "nullness" , // Nullness utilities are trusted regarding nullness.
28
- "cast" // Casts look redundant if Nullness Checker is not run.
29
- })
30
- @ AnnotatedFor ("nullness" )
31
25
public final class NullnessUtil {
32
26
33
27
private NullnessUtil () {
@@ -70,10 +64,8 @@ private NullnessUtil() {
70
64
*
71
65
* @return the argument, cast to have the type qualifier @NonNull
72
66
*/
73
- @ EnsuresNonNull ("#1" )
74
- public static <T extends @ Nullable Object > @ NonNull T castNonNull (@ Nullable T ref ) {
75
- assert ref != null : "Misuse of castNonNull: called with a null argument" ;
76
- return ref ;
67
+ public static <T > T castNonNull (@ Nullable T ref ) {
68
+ return requireNonNull ( ref , "Misuse of castNonNull: called with a null argument" );
77
69
}
78
70
79
71
/**
@@ -88,254 +80,9 @@ private NullnessUtil() {
88
80
*
89
81
* @see #castNonNull(Object)
90
82
*/
91
- public static @ EnsuresNonNull ("#1" ) <T extends @ Nullable Object > @ NonNull T castNonNull (
92
- @ Nullable T ref , String message ) {
93
- assert ref != null : "Misuse of castNonNull: called with a null argument: " + message ;
94
- return ref ;
95
- }
96
-
97
- /**
98
- * Like castNonNull, but whereas that method only checks and casts the reference itself, this
99
- * traverses all levels of the argument array. The array is recursively checked to ensure that all
100
- * elements at every array level are non-null.
101
- *
102
- * @param <T> the component type of the array
103
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
104
- * time
105
- *
106
- * @return the argument, cast to have the type qualifier @NonNull at all levels
107
- *
108
- * @see #castNonNull(Object)
109
- */
110
- @ EnsuresNonNull ("#1" )
111
- public static <T extends @ Nullable Object > @ NonNull T @ NonNull [] castNonNullDeep (
112
- T @ Nullable [] arr ) {
113
- return castNonNullArray ( arr , null );
114
- }
115
-
116
- /**
117
- * Like castNonNull, but whereas that method only checks and casts the reference itself, this
118
- * traverses all levels of the argument array. The array is recursively checked to ensure that all
119
- * elements at every array level are non-null.
120
- *
121
- * @param <T> the component type of the array
122
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
123
- * time
124
- * @param message text to include if this method is misused
125
- *
126
- * @return the argument, cast to have the type qualifier @NonNull at all levels
127
- *
128
- * @see #castNonNull(Object)
129
- */
130
- @ EnsuresNonNull ("#1" )
131
- public static <T extends @ Nullable Object > @ NonNull T @ NonNull [] castNonNullDeep (
132
- T @ Nullable [] arr , String message ) {
133
- return castNonNullArray ( arr , message );
134
- }
135
-
136
- /**
137
- * Like castNonNull, but whereas that method only checks and casts the reference itself, this
138
- * traverses all levels of the argument array. The array is recursively checked to ensure that all
139
- * elements at every array level are non-null.
140
- *
141
- * @param <T> the component type of the component type of the array
142
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
143
- * time
144
- *
145
- * @return the argument, cast to have the type qualifier @NonNull at all levels
146
- *
147
- * @see #castNonNull(Object)
148
- */
149
- @ EnsuresNonNull ("#1" )
150
- public static <T extends @ Nullable Object > @ NonNull T @ NonNull [][] castNonNullDeep (
151
- T @ Nullable [] @ Nullable [] arr ) {
152
- return castNonNullArray ( arr , null );
153
- }
154
-
155
- /**
156
- * Like castNonNull, but whereas that method only checks and casts the reference itself, this
157
- * traverses all levels of the argument array. The array is recursively checked to ensure that all
158
- * elements at every array level are non-null.
159
- *
160
- * @param <T> the component type of the component type of the array
161
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
162
- * time
163
- * @param message text to include if this method is misused
164
- *
165
- * @return the argument, cast to have the type qualifier @NonNull at all levels
166
- *
167
- * @see #castNonNull(Object)
168
- */
169
- @ EnsuresNonNull ("#1" )
170
- public static <T extends @ Nullable Object > @ NonNull T @ NonNull [][] castNonNullDeep (
171
- T @ Nullable [] @ Nullable [] arr , String message ) {
172
- return castNonNullArray ( arr , message );
173
- }
174
-
175
- /**
176
- * Like castNonNull, but whereas that method only checks and casts the reference itself, this
177
- * traverses all levels of the argument array. The array is recursively checked to ensure that all
178
- * elements at every array level are non-null.
179
- *
180
- * @param <T> the component type (three levels in) of the array
181
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
182
- * time
183
- *
184
- * @return the argument, cast to have the type qualifier @NonNull at all levels
185
- *
186
- * @see #castNonNull(Object)
187
- */
188
- @ EnsuresNonNull ("#1" )
189
- public static <T extends @ Nullable Object > @ NonNull T @ NonNull [][][] castNonNullDeep (
190
- T @ Nullable [] @ Nullable [] @ Nullable [] arr ) {
191
- return castNonNullArray ( arr , null );
192
- }
193
-
194
- /**
195
- * Like castNonNull, but whereas that method only checks and casts the reference itself, this
196
- * traverses all levels of the argument array. The array is recursively checked to ensure that all
197
- * elements at every array level are non-null.
198
- *
199
- * @param <T> the component type (three levels in) of the array
200
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
201
- * time
202
- * @param message text to include if this method is misused
203
- *
204
- * @return the argument, cast to have the type qualifier @NonNull at all levels
205
- *
206
- * @see #castNonNull(Object)
207
- */
208
- @ EnsuresNonNull ("#1" )
209
- public static <T extends @ Nullable Object > @ NonNull T @ NonNull [][][] castNonNullDeep (
210
- T @ Nullable [] @ Nullable [] @ Nullable [] arr , String message ) {
211
- return castNonNullArray ( arr , message );
212
- }
213
-
214
- /**
215
- * Like castNonNull, but whereas that method only checks and casts the reference itself, this
216
- * traverses all levels of the argument array. The array is recursively checked to ensure that all
217
- * elements at every array level are non-null.
218
- *
219
- * @param <T> the component type of the array
220
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
221
- * time
222
- *
223
- * @return the argument, cast to have the type qualifier @NonNull at all levels
224
- *
225
- * @see #castNonNull(Object)
226
- */
227
- @ EnsuresNonNull ("#1" )
228
- public static <T extends @ Nullable Object > @ NonNull T @ NonNull [][][][] castNonNullDeep (
229
- T @ Nullable [] @ Nullable [] @ Nullable [] @ Nullable [] arr ) {
230
- return castNonNullArray ( arr , null );
231
- }
232
-
233
- /**
234
- * Like castNonNull, but whereas that method only checks and casts the reference itself, this
235
- * traverses all levels of the argument array. The array is recursively checked to ensure that all
236
- * elements at every array level are non-null.
237
- *
238
- * @param <T> the component type (four levels in) of the array
239
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
240
- * time
241
- * @param message text to include if this method is misused
242
- *
243
- * @return the argument, cast to have the type qualifier @NonNull at all levels
244
- *
245
- * @see #castNonNull(Object)
246
- */
247
- @ EnsuresNonNull ("#1" )
248
- public static <T extends @ Nullable Object > @ NonNull T @ NonNull [][][][] castNonNullDeep (
249
- T @ Nullable [] @ Nullable [] @ Nullable [] @ Nullable [] arr , String message ) {
250
- return castNonNullArray ( arr , message );
251
- }
252
-
253
- /**
254
- * Like castNonNull, but whereas that method only checks and casts the reference itself, this
255
- * traverses all levels of the argument array. The array is recursively checked to ensure that all
256
- * elements at every array level are non-null.
257
- *
258
- * @param <T> the component type (four levels in) of the array
259
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
260
- * time
261
- *
262
- * @return the argument, cast to have the type qualifier @NonNull at all levels
263
- *
264
- * @see #castNonNull(Object)
265
- */
266
- @ EnsuresNonNull ("#1" )
267
- public static <T extends @ Nullable Object > @ NonNull T @ NonNull [][][][][] castNonNullDeep (
268
- T @ Nullable [] @ Nullable [] @ Nullable [] @ Nullable [] @ Nullable [] arr ) {
269
- return castNonNullArray ( arr , null );
270
- }
271
-
272
- /**
273
- * Like castNonNull, but whereas that method only checks and casts the reference itself, this
274
- * traverses all levels of the argument array. The array is recursively checked to ensure that all
275
- * elements at every array level are non-null.
276
- *
277
- * @param <T> the component type (five levels in) of the array
278
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
279
- * time
280
- * @param message text to include if this method is misused
281
- *
282
- * @return the argument, cast to have the type qualifier @NonNull at all levels
283
- *
284
- * @see #castNonNull(Object)
285
- */
286
- @ EnsuresNonNull ("#1" )
287
- public static <T extends @ Nullable Object > @ NonNull T @ NonNull [][][][][] castNonNullDeep (
288
- T @ Nullable [] @ Nullable [] @ Nullable [] @ Nullable [] @ Nullable [] arr , String message ) {
289
- return castNonNullArray ( arr , message );
290
- }
291
-
292
- /**
293
- * The implementation of castNonNullDeep.
294
- *
295
- * @param <T> the component type (five levels in) of the array
296
- * @param arr an array all of whose elements, and their elements recursively, are non-null at run
297
- * time
298
- * @param message text to include if there is a non-null value, or null to use uncustomized
299
- * message
300
- *
301
- * @return the argument, cast to have the type qualifier @NonNull at all levels
302
- */
303
- private static <T extends @ Nullable Object > @ NonNull T @ NonNull [] castNonNullArray (
304
- T @ Nullable [] arr , @ Nullable String message ) {
305
- assert arr != null
306
- : "Misuse of castNonNullArray: called with a null array argument"
307
- + ( message == null ? "" : ": " + message );
308
- for ( int i = 0 ; i < arr .length ; ++i ) {
309
- assert arr [i ] != null
310
- : "Misuse of castNonNull: called with a null array element"
311
- + ( message == null ? "" : ": " + message );
312
- checkIfArray ( arr [i ], message );
313
- }
314
- return arr ;
83
+ public static <T > T castNonNull (@ Nullable T ref , String message ) {
84
+ return requireNonNull ( ref , "Misuse of castNonNull: called with a null argument: " + message );
315
85
}
316
86
317
- /**
318
- * If the argument is an array, requires it to be non-null at all levels.
319
- *
320
- * @param ref a value; if an array, all of its elements, and their elements recursively, are
321
- * non-null at run time
322
- * @param message text to include if there is a non-null value, or null to use uncustomized
323
- * message
324
- */
325
- private static void checkIfArray (@ NonNull Object ref , @ Nullable String message ) {
326
- assert ref != null
327
- : "Misuse of checkIfArray: called with a null argument"
328
- + ( ( message == null ) ? "" : ( ": " + message ) );
329
- Class <?> comp = ref .getClass ().getComponentType ();
330
- if ( comp != null ) {
331
- // comp is non-null for arrays, otherwise null.
332
- if ( comp .isPrimitive () ) {
333
- // Nothing to do for arrays of primitive type: primitives are
334
- // never null.
335
- }
336
- else {
337
- castNonNullArray ( (Object []) ref , message );
338
- }
339
- }
340
- }
87
+ // removed unused code and very deeply arrays
341
88
}
0 commit comments