1
1
/*
2
- * Copyright (c) 2000, 2024 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2000, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
35
35
* parameter when creating a relation, and when trying to set several roles in
36
36
* a relation (via 'setRoles()' method). It is returned as part of a
37
37
* RoleResult, to provide roles successfully retrieved.
38
+
39
+ * <p>It is not permitted to add objects to a {@code RoleList} that are
40
+ * not instances of {@code Role}. This will produce an {@code IllegalArgumentException}
41
+ * when calling methods in this class, or when using {@code listIterator} and {@code add} or {@code set}.</p>
38
42
*
39
43
* @since 1.5
40
44
*/
@@ -55,9 +59,6 @@ the asList() method so you can write
55
59
*/
56
60
public class RoleList extends ArrayList <Object > {
57
61
58
- private transient boolean typeSafe ;
59
- private transient boolean tainted ;
60
-
61
62
/* Serial version */
62
63
private static final long serialVersionUID = 5568344346499649313L ;
63
64
@@ -121,25 +122,13 @@ public RoleList(List<Role> list) throws IllegalArgumentException {
121
122
* @return a {@code List<Role>} whose contents
122
123
* reflect the contents of this {@code RoleList}.
123
124
*
124
- * <p>If this method has ever been called on a given
125
- * {@code RoleList} instance, a subsequent attempt to add
126
- * an object to that instance which is not a {@code Role}
127
- * will fail with an {@code IllegalArgumentException}. For compatibility
128
- * reasons, a {@code RoleList} on which this method has never
129
- * been called does allow objects other than {@code Role}s to
130
- * be added.</p>
131
- *
132
125
* @throws IllegalArgumentException if this {@code RoleList} contains
133
126
* an element that is not a {@code Role}.
134
127
*
135
128
* @since 1.6
136
129
*/
137
130
public List <Role > asList () {
138
- if (!typeSafe ) {
139
- if (tainted )
140
- checkTypeSafe (this );
141
- typeSafe = true ;
142
- }
131
+ checkTypeSafe (this );
143
132
return Util .cast (this );
144
133
}
145
134
@@ -158,8 +147,7 @@ public void add(Role role)
158
147
throws IllegalArgumentException {
159
148
160
149
if (role == null ) {
161
- String excMsg = "Invalid parameter" ;
162
- throw new IllegalArgumentException (excMsg );
150
+ throw new IllegalArgumentException ("Invalid parameter" );
163
151
}
164
152
super .add (role );
165
153
}
@@ -183,10 +171,8 @@ public void add(int index,
183
171
IndexOutOfBoundsException {
184
172
185
173
if (role == null ) {
186
- String excMsg = "Invalid parameter" ;
187
- throw new IllegalArgumentException (excMsg );
174
+ throw new IllegalArgumentException ("Invalid parameter" );
188
175
}
189
-
190
176
super .add (index , role );
191
177
}
192
178
@@ -208,11 +194,8 @@ public void set(int index,
208
194
IndexOutOfBoundsException {
209
195
210
196
if (role == null ) {
211
- // Revisit [cebro] Localize message
212
- String excMsg = "Invalid parameter." ;
213
- throw new IllegalArgumentException (excMsg );
197
+ throw new IllegalArgumentException ("Invalid parameter" );
214
198
}
215
-
216
199
super .set (index , role );
217
200
}
218
201
@@ -236,7 +219,6 @@ public boolean addAll(RoleList roleList)
236
219
if (roleList == null ) {
237
220
return true ;
238
221
}
239
-
240
222
return (super .addAll (roleList ));
241
223
}
242
224
@@ -263,9 +245,7 @@ public boolean addAll(int index,
263
245
IndexOutOfBoundsException {
264
246
265
247
if (roleList == null ) {
266
- // Revisit [cebro] Localize message
267
- String excMsg = "Invalid parameter." ;
268
- throw new IllegalArgumentException (excMsg );
248
+ throw new IllegalArgumentException ("Invalid parameter" );
269
249
}
270
250
271
251
return (super .addAll (index , roleList ));
@@ -277,48 +257,53 @@ public boolean addAll(int index,
277
257
* been called on this instance.
278
258
*/
279
259
260
+ /**
261
+ * {@inheritDoc}
262
+ * @throws IllegalArgumentException if {@code o} is not a {@code Role}.
263
+ */
280
264
@ Override
281
265
public boolean add (Object o ) {
282
- if (!tainted )
283
- tainted = isTainted (o );
284
- if (typeSafe )
285
- checkTypeSafe (o );
266
+ checkTypeSafe (o );
286
267
return super .add (o );
287
268
}
288
269
270
+ /**
271
+ * {@inheritDoc}
272
+ * @throws IllegalArgumentException if {@code element} is not a {@code Role}.
273
+ */
289
274
@ Override
290
275
public void add (int index , Object element ) {
291
- if (!tainted )
292
- tainted = isTainted (element );
293
- if (typeSafe )
294
- checkTypeSafe (element );
276
+ checkTypeSafe (element );
295
277
super .add (index , element );
296
278
}
297
279
280
+ /**
281
+ * {@inheritDoc}
282
+ * @throws IllegalArgumentException if {@code c} contains a member that is not a {@code Role}.
283
+ */
298
284
@ Override
299
285
public boolean addAll (Collection <?> c ) {
300
- if (!tainted )
301
- tainted = isTainted (c );
302
- if (typeSafe )
303
- checkTypeSafe (c );
286
+ checkTypeSafe (c );
304
287
return super .addAll (c );
305
288
}
306
289
290
+ /**
291
+ * {@inheritDoc}
292
+ * @throws IllegalArgumentException if {@code c} contains a member that is not a {@code Role}.
293
+ */
307
294
@ Override
308
295
public boolean addAll (int index , Collection <?> c ) {
309
- if (!tainted )
310
- tainted = isTainted (c );
311
- if (typeSafe )
312
- checkTypeSafe (c );
296
+ checkTypeSafe (c );
313
297
return super .addAll (index , c );
314
298
}
315
299
300
+ /**
301
+ * {@inheritDoc}
302
+ * @throws IllegalArgumentException if {@code element} is not a {@code Role}.
303
+ */
316
304
@ Override
317
305
public Object set (int index , Object element ) {
318
- if (!tainted )
319
- tainted = isTainted (element );
320
- if (typeSafe )
321
- checkTypeSafe (element );
306
+ checkTypeSafe (element );
322
307
return super .set (index , element );
323
308
}
324
309
@@ -345,28 +330,4 @@ private static void checkTypeSafe(Collection<?> c) {
345
330
throw new IllegalArgumentException (e );
346
331
}
347
332
}
348
-
349
- /**
350
- * Returns true if o is a non-Role object.
351
- */
352
- private static boolean isTainted (Object o ) {
353
- try {
354
- checkTypeSafe (o );
355
- } catch (IllegalArgumentException e ) {
356
- return true ;
357
- }
358
- return false ;
359
- }
360
-
361
- /**
362
- * Returns true if c contains any non-Role objects.
363
- */
364
- private static boolean isTainted (Collection <?> c ) {
365
- try {
366
- checkTypeSafe (c );
367
- } catch (IllegalArgumentException e ) {
368
- return true ;
369
- }
370
- return false ;
371
- }
372
333
}
0 commit comments