11/*
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.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
3535 * parameter when creating a relation, and when trying to set several roles in
3636 * a relation (via 'setRoles()' method). It is returned as part of a
3737 * 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>
3842 *
3943 * @since 1.5
4044 */
@@ -55,9 +59,6 @@ the asList() method so you can write
5559*/
5660public class RoleList extends ArrayList <Object > {
5761
58- private transient boolean typeSafe ;
59- private transient boolean tainted ;
60-
6162 /* Serial version */
6263 private static final long serialVersionUID = 5568344346499649313L ;
6364
@@ -121,25 +122,13 @@ public RoleList(List<Role> list) throws IllegalArgumentException {
121122 * @return a {@code List<Role>} whose contents
122123 * reflect the contents of this {@code RoleList}.
123124 *
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- *
132125 * @throws IllegalArgumentException if this {@code RoleList} contains
133126 * an element that is not a {@code Role}.
134127 *
135128 * @since 1.6
136129 */
137130 public List <Role > asList () {
138- if (!typeSafe ) {
139- if (tainted )
140- checkTypeSafe (this );
141- typeSafe = true ;
142- }
131+ checkTypeSafe (this );
143132 return Util .cast (this );
144133 }
145134
@@ -158,8 +147,7 @@ public void add(Role role)
158147 throws IllegalArgumentException {
159148
160149 if (role == null ) {
161- String excMsg = "Invalid parameter" ;
162- throw new IllegalArgumentException (excMsg );
150+ throw new IllegalArgumentException ("Invalid parameter" );
163151 }
164152 super .add (role );
165153 }
@@ -183,10 +171,8 @@ public void add(int index,
183171 IndexOutOfBoundsException {
184172
185173 if (role == null ) {
186- String excMsg = "Invalid parameter" ;
187- throw new IllegalArgumentException (excMsg );
174+ throw new IllegalArgumentException ("Invalid parameter" );
188175 }
189-
190176 super .add (index , role );
191177 }
192178
@@ -208,11 +194,8 @@ public void set(int index,
208194 IndexOutOfBoundsException {
209195
210196 if (role == null ) {
211- // Revisit [cebro] Localize message
212- String excMsg = "Invalid parameter." ;
213- throw new IllegalArgumentException (excMsg );
197+ throw new IllegalArgumentException ("Invalid parameter" );
214198 }
215-
216199 super .set (index , role );
217200 }
218201
@@ -236,7 +219,6 @@ public boolean addAll(RoleList roleList)
236219 if (roleList == null ) {
237220 return true ;
238221 }
239-
240222 return (super .addAll (roleList ));
241223 }
242224
@@ -263,9 +245,7 @@ public boolean addAll(int index,
263245 IndexOutOfBoundsException {
264246
265247 if (roleList == null ) {
266- // Revisit [cebro] Localize message
267- String excMsg = "Invalid parameter." ;
268- throw new IllegalArgumentException (excMsg );
248+ throw new IllegalArgumentException ("Invalid parameter" );
269249 }
270250
271251 return (super .addAll (index , roleList ));
@@ -277,48 +257,53 @@ public boolean addAll(int index,
277257 * been called on this instance.
278258 */
279259
260+ /**
261+ * {@inheritDoc}
262+ * @throws IllegalArgumentException if {@code o} is not a {@code Role}.
263+ */
280264 @ Override
281265 public boolean add (Object o ) {
282- if (!tainted )
283- tainted = isTainted (o );
284- if (typeSafe )
285- checkTypeSafe (o );
266+ checkTypeSafe (o );
286267 return super .add (o );
287268 }
288269
270+ /**
271+ * {@inheritDoc}
272+ * @throws IllegalArgumentException if {@code element} is not a {@code Role}.
273+ */
289274 @ Override
290275 public void add (int index , Object element ) {
291- if (!tainted )
292- tainted = isTainted (element );
293- if (typeSafe )
294- checkTypeSafe (element );
276+ checkTypeSafe (element );
295277 super .add (index , element );
296278 }
297279
280+ /**
281+ * {@inheritDoc}
282+ * @throws IllegalArgumentException if {@code c} contains a member that is not a {@code Role}.
283+ */
298284 @ Override
299285 public boolean addAll (Collection <?> c ) {
300- if (!tainted )
301- tainted = isTainted (c );
302- if (typeSafe )
303- checkTypeSafe (c );
286+ checkTypeSafe (c );
304287 return super .addAll (c );
305288 }
306289
290+ /**
291+ * {@inheritDoc}
292+ * @throws IllegalArgumentException if {@code c} contains a member that is not a {@code Role}.
293+ */
307294 @ Override
308295 public boolean addAll (int index , Collection <?> c ) {
309- if (!tainted )
310- tainted = isTainted (c );
311- if (typeSafe )
312- checkTypeSafe (c );
296+ checkTypeSafe (c );
313297 return super .addAll (index , c );
314298 }
315299
300+ /**
301+ * {@inheritDoc}
302+ * @throws IllegalArgumentException if {@code element} is not a {@code Role}.
303+ */
316304 @ Override
317305 public Object set (int index , Object element ) {
318- if (!tainted )
319- tainted = isTainted (element );
320- if (typeSafe )
321- checkTypeSafe (element );
306+ checkTypeSafe (element );
322307 return super .set (index , element );
323308 }
324309
@@ -345,28 +330,4 @@ private static void checkTypeSafe(Collection<?> c) {
345330 throw new IllegalArgumentException (e );
346331 }
347332 }
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- }
372333}
0 commit comments