Skip to content

Commit cbc7090

Browse files
committed
8359809: AttributeList, RoleList and UnresolvedRoleList should never accept other types of Object
Reviewed-by: sspitsyn
1 parent f735275 commit cbc7090

File tree

5 files changed

+129
-232
lines changed

5 files changed

+129
-232
lines changed

src/java.management/share/classes/javax/management/AttributeList.java

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 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
@@ -37,13 +37,9 @@
3737
* {@link MBeanServerConnection#setAttributes setAttributes} methods of
3838
* {@link MBeanServer} and {@link MBeanServerConnection}.</p>
3939
*
40-
* <p id="type-safe">For compatibility reasons, it is possible, though
41-
* highly discouraged, to add objects to an {@code AttributeList} that are
42-
* not instances of {@code Attribute}. However, an {@code AttributeList}
43-
* can be made <em>type-safe</em>, which means that an attempt to add
44-
* an object that is not an {@code Attribute} will produce an {@code
45-
* IllegalArgumentException}. An {@code AttributeList} becomes type-safe
46-
* when the method {@link #asList()} is called on it.</p>
40+
* <p>It is not permitted to add objects to an {@code AttributeList} that are
41+
* not instances of {@code Attribute}. This will produce an {@code IllegalArgumentException}
42+
* when calling methods in this class, or when using {@code listIterator} and {@code add} or {@code set}.</p>
4743
*
4844
* @since 1.5
4945
*/
@@ -64,9 +60,6 @@ the asList() method so you can write
6460
*/
6561
public class AttributeList extends ArrayList<Object> {
6662

67-
private transient volatile boolean typeSafe;
68-
private transient volatile boolean tainted;
69-
7063
/* Serial version */
7164
private static final long serialVersionUID = -4077085769279709076L;
7265

@@ -145,24 +138,14 @@ public AttributeList(List<Attribute> list) {
145138
* @return a {@code List<Attribute>} whose contents
146139
* reflect the contents of this {@code AttributeList}.
147140
*
148-
* <p>If this method has ever been called on a given
149-
* {@code AttributeList} instance, a subsequent attempt to add
150-
* an object to that instance which is not an {@code Attribute}
151-
* will fail with an {@code IllegalArgumentException}. For compatibility
152-
* reasons, an {@code AttributeList} on which this method has never
153-
* been called does allow objects other than {@code Attribute}s to
154-
* be added.</p>
155-
*
156141
* @throws IllegalArgumentException if this {@code AttributeList} contains
157142
* an element that is not an {@code Attribute}.
158143
*
159144
* @since 1.6
160145
*/
161146
@SuppressWarnings("unchecked")
162147
public List<Attribute> asList() {
163-
typeSafe = true;
164-
if (tainted)
165-
adding((Collection<?>) this); // will throw IllegalArgumentException
148+
adding((Collection<?>) this);
166149
return (List<Attribute>) (List<?>) this;
167150
}
168151

@@ -257,15 +240,12 @@ public boolean addAll(int index, AttributeList list) {
257240

258241
/*
259242
* Override all of the methods from ArrayList<Object> that might add
260-
* a non-Attribute to the List, and disallow that if asList has ever
261-
* been called on this instance.
243+
* a non-Attribute to the List, and disallow.
262244
*/
263245

264246
/**
265247
* {@inheritDoc}
266-
* @throws IllegalArgumentException if this {@code AttributeList} is
267-
* <a href="#type-safe">type-safe</a> and {@code element} is not an
268-
* {@code Attribute}.
248+
* @throws IllegalArgumentException if {@code element} is not an {@code Attribute}.
269249
*/
270250
@Override
271251
public boolean add(Object element) {
@@ -275,9 +255,7 @@ public boolean add(Object element) {
275255

276256
/**
277257
* {@inheritDoc}
278-
* @throws IllegalArgumentException if this {@code AttributeList} is
279-
* <a href="#type-safe">type-safe</a> and {@code element} is not an
280-
* {@code Attribute}.
258+
* @throws IllegalArgumentException if {@code element} is not an {@code Attribute}.
281259
*/
282260
@Override
283261
public void add(int index, Object element) {
@@ -287,9 +265,7 @@ public void add(int index, Object element) {
287265

288266
/**
289267
* {@inheritDoc}
290-
* @throws IllegalArgumentException if this {@code AttributeList} is
291-
* <a href="#type-safe">type-safe</a> and {@code c} contains an
292-
* element that is not an {@code Attribute}.
268+
* @throws IllegalArgumentException if {@code c} contains an element that is not an {@code Attribute}.
293269
*/
294270
@Override
295271
public boolean addAll(Collection<?> c) {
@@ -299,9 +275,7 @@ public boolean addAll(Collection<?> c) {
299275

300276
/**
301277
* {@inheritDoc}
302-
* @throws IllegalArgumentException if this {@code AttributeList} is
303-
* <a href="#type-safe">type-safe</a> and {@code c} contains an
304-
* element that is not an {@code Attribute}.
278+
* @throws IllegalArgumentException if {@code c} contains an element that is not an {@code Attribute}.
305279
*/
306280
@Override
307281
public boolean addAll(int index, Collection<?> c) {
@@ -311,9 +285,7 @@ public boolean addAll(int index, Collection<?> c) {
311285

312286
/**
313287
* {@inheritDoc}
314-
* @throws IllegalArgumentException if this {@code AttributeList} is
315-
* <a href="#type-safe">type-safe</a> and {@code element} is not an
316-
* {@code Attribute}.
288+
* @throws IllegalArgumentException if {@code element} is not an {@code Attribute}.
317289
*/
318290
@Override
319291
public Object set(int index, Object element) {
@@ -324,10 +296,7 @@ public Object set(int index, Object element) {
324296
private void adding(Object x) {
325297
if (x == null || x instanceof Attribute)
326298
return;
327-
if (typeSafe)
328-
throw new IllegalArgumentException("Not an Attribute: " + x);
329-
else
330-
tainted = true;
299+
throw new IllegalArgumentException("Not an Attribute: " + x);
331300
}
332301

333302
private void adding(Collection<?> c) {

src/java.management/share/classes/javax/management/relation/RoleList.java

Lines changed: 35 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
@@ -35,6 +35,10 @@
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
*/
5660
public 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

Comments
 (0)