author | emcmanus |
Thu, 07 May 2009 10:44:45 +0200 | |
changeset 3446 | 4a07252d2e92 |
parent 1510 | e747d3193ef2 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2000-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
||
27 |
package javax.management.openmbean; |
|
28 |
||
29 |
||
30 |
// java import |
|
31 |
// |
|
32 |
import java.lang.reflect.Array; |
|
33 |
import java.lang.reflect.Constructor; |
|
34 |
import java.lang.reflect.Method; |
|
35 |
import java.lang.reflect.Modifier; |
|
36 |
import java.util.Arrays; |
|
37 |
import java.util.Collection; |
|
38 |
import java.util.Collections; |
|
39 |
import java.util.HashMap; |
|
40 |
import java.util.HashSet; |
|
41 |
import java.util.Map; |
|
42 |
import java.util.Set; |
|
43 |
import javax.management.Descriptor; |
|
44 |
import javax.management.DescriptorRead; |
|
45 |
import javax.management.ImmutableDescriptor; |
|
46 |
import javax.management.MBeanAttributeInfo; |
|
47 |
import com.sun.jmx.remote.util.EnvHelp; |
|
48 |
||
49 |
/** |
|
50 |
* Describes an attribute of an open MBean. |
|
51 |
* |
|
52 |
* |
|
53 |
* @since 1.5 |
|
54 |
*/ |
|
55 |
public class OpenMBeanAttributeInfoSupport |
|
56 |
extends MBeanAttributeInfo |
|
57 |
implements OpenMBeanAttributeInfo { |
|
58 |
||
59 |
/* Serial version */ |
|
60 |
static final long serialVersionUID = -4867215622149721849L; |
|
61 |
||
62 |
/** |
|
63 |
* @serial The open mbean attribute's <i>open type</i> |
|
64 |
*/ |
|
65 |
private OpenType<?> openType; |
|
66 |
||
67 |
/** |
|
68 |
* @serial The open mbean attribute's default value |
|
69 |
*/ |
|
70 |
private final Object defaultValue; |
|
71 |
||
72 |
/** |
|
73 |
* @serial The open mbean attribute's legal values. This {@link |
|
74 |
* Set} is unmodifiable |
|
75 |
*/ |
|
76 |
private final Set<?> legalValues; // to be constructed unmodifiable |
|
77 |
||
78 |
/** |
|
79 |
* @serial The open mbean attribute's min value |
|
80 |
*/ |
|
1510 | 81 |
private final Comparable<?> minValue; |
2 | 82 |
|
83 |
/** |
|
84 |
* @serial The open mbean attribute's max value |
|
85 |
*/ |
|
1510 | 86 |
private final Comparable<?> maxValue; |
2 | 87 |
|
88 |
||
89 |
// As this instance is immutable, these two values need only |
|
90 |
// be calculated once. |
|
91 |
private transient Integer myHashCode = null; |
|
92 |
private transient String myToString = null; |
|
93 |
||
94 |
||
95 |
/** |
|
96 |
* Constructs an {@code OpenMBeanAttributeInfoSupport} instance, |
|
97 |
* which describes the attribute of an open MBean with the |
|
98 |
* specified {@code name}, {@code openType} and {@code |
|
99 |
* description}, and the specified read/write access properties. |
|
100 |
* |
|
101 |
* @param name cannot be a null or empty string. |
|
102 |
* |
|
103 |
* @param description cannot be a null or empty string. |
|
104 |
* |
|
105 |
* @param openType cannot be null. |
|
106 |
* |
|
107 |
* @param isReadable {@code true} if the attribute has a getter |
|
108 |
* exposed for management. |
|
109 |
* |
|
110 |
* @param isWritable {@code true} if the attribute has a setter |
|
111 |
* exposed for management. |
|
112 |
* |
|
113 |
* @param isIs {@code true} if the attribute's getter is of the |
|
114 |
* form <tt>is<i>XXX</i></tt>. |
|
115 |
* |
|
116 |
* @throws IllegalArgumentException if {@code name} or {@code |
|
117 |
* description} are null or empty string, or {@code openType} is |
|
118 |
* null. |
|
119 |
*/ |
|
120 |
public OpenMBeanAttributeInfoSupport(String name, |
|
121 |
String description, |
|
122 |
OpenType<?> openType, |
|
123 |
boolean isReadable, |
|
124 |
boolean isWritable, |
|
125 |
boolean isIs) { |
|
126 |
this(name, description, openType, isReadable, isWritable, isIs, |
|
127 |
(Descriptor) null); |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* <p>Constructs an {@code OpenMBeanAttributeInfoSupport} instance, |
|
132 |
* which describes the attribute of an open MBean with the |
|
133 |
* specified {@code name}, {@code openType}, {@code |
|
134 |
* description}, read/write access properties, and {@code Descriptor}.</p> |
|
135 |
* |
|
136 |
* <p>The {@code descriptor} can contain entries that will define |
|
137 |
* the values returned by certain methods of this class, as |
|
138 |
* explained in the {@link <a href="package-summary.html#constraints"> |
|
139 |
* package description</a>}. |
|
140 |
* |
|
141 |
* @param name cannot be a null or empty string. |
|
142 |
* |
|
143 |
* @param description cannot be a null or empty string. |
|
144 |
* |
|
145 |
* @param openType cannot be null. |
|
146 |
* |
|
147 |
* @param isReadable {@code true} if the attribute has a getter |
|
148 |
* exposed for management. |
|
149 |
* |
|
150 |
* @param isWritable {@code true} if the attribute has a setter |
|
151 |
* exposed for management. |
|
152 |
* |
|
153 |
* @param isIs {@code true} if the attribute's getter is of the |
|
154 |
* form <tt>is<i>XXX</i></tt>. |
|
155 |
* |
|
156 |
* @param descriptor The descriptor for the attribute. This may be null |
|
157 |
* which is equivalent to an empty descriptor. |
|
158 |
* |
|
159 |
* @throws IllegalArgumentException if {@code name} or {@code |
|
160 |
* description} are null or empty string, or {@code openType} is |
|
161 |
* null, or the descriptor entries are invalid as described in the |
|
162 |
* {@link <a href="package-summary.html#constraints">package |
|
163 |
* description</a>}. |
|
164 |
* |
|
165 |
* @since 1.6 |
|
166 |
*/ |
|
167 |
public OpenMBeanAttributeInfoSupport(String name, |
|
168 |
String description, |
|
169 |
OpenType<?> openType, |
|
170 |
boolean isReadable, |
|
171 |
boolean isWritable, |
|
172 |
boolean isIs, |
|
173 |
Descriptor descriptor) { |
|
174 |
// Construct parent's state |
|
175 |
// |
|
176 |
super(name, |
|
177 |
(openType==null) ? null : openType.getClassName(), |
|
178 |
description, |
|
179 |
isReadable, |
|
180 |
isWritable, |
|
181 |
isIs, |
|
182 |
ImmutableDescriptor.union(descriptor, (openType==null)?null: |
|
183 |
openType.getDescriptor())); |
|
184 |
||
185 |
// Initialize this instance's specific state |
|
186 |
// |
|
187 |
this.openType = openType; |
|
188 |
||
189 |
descriptor = getDescriptor(); // replace null by empty |
|
190 |
this.defaultValue = valueFrom(descriptor, "defaultValue", openType); |
|
191 |
this.legalValues = valuesFrom(descriptor, "legalValues", openType); |
|
192 |
this.minValue = comparableValueFrom(descriptor, "minValue", openType); |
|
193 |
this.maxValue = comparableValueFrom(descriptor, "maxValue", openType); |
|
194 |
||
195 |
try { |
|
196 |
check(this); |
|
197 |
} catch (OpenDataException e) { |
|
198 |
throw new IllegalArgumentException(e.getMessage(), e); |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Constructs an {@code OpenMBeanAttributeInfoSupport} instance, |
|
204 |
* which describes the attribute of an open MBean with the |
|
205 |
* specified {@code name}, {@code openType}, {@code description} |
|
206 |
* and {@code defaultValue}, and the specified read/write access |
|
207 |
* properties. |
|
208 |
* |
|
209 |
* @param name cannot be a null or empty string. |
|
210 |
* |
|
211 |
* @param description cannot be a null or empty string. |
|
212 |
* |
|
213 |
* @param openType cannot be null. |
|
214 |
* |
|
215 |
* @param isReadable {@code true} if the attribute has a getter |
|
216 |
* exposed for management. |
|
217 |
* |
|
218 |
* @param isWritable {@code true} if the attribute has a setter |
|
219 |
* exposed for management. |
|
220 |
* |
|
221 |
* @param isIs {@code true} if the attribute's getter is of the |
|
222 |
* form <tt>is<i>XXX</i></tt>. |
|
223 |
* |
|
224 |
* @param defaultValue must be a valid value for the {@code |
|
225 |
* openType} specified for this attribute; default value not |
|
226 |
* supported for {@code ArrayType} and {@code TabularType}; can |
|
227 |
* be null, in which case it means that no default value is set. |
|
228 |
* |
|
229 |
* @param <T> allows the compiler to check that the {@code defaultValue}, |
|
230 |
* if non-null, has the correct Java type for the given {@code openType}. |
|
231 |
* |
|
232 |
* @throws IllegalArgumentException if {@code name} or {@code |
|
233 |
* description} are null or empty string, or {@code openType} is |
|
234 |
* null. |
|
235 |
* |
|
236 |
* @throws OpenDataException if {@code defaultValue} is not a |
|
237 |
* valid value for the specified {@code openType}, or {@code |
|
238 |
* defaultValue} is non null and {@code openType} is an {@code |
|
239 |
* ArrayType} or a {@code TabularType}. |
|
240 |
*/ |
|
241 |
public <T> OpenMBeanAttributeInfoSupport(String name, |
|
242 |
String description, |
|
243 |
OpenType<T> openType, |
|
244 |
boolean isReadable, |
|
245 |
boolean isWritable, |
|
246 |
boolean isIs, |
|
247 |
T defaultValue) |
|
248 |
throws OpenDataException { |
|
249 |
this(name, description, openType, isReadable, isWritable, isIs, |
|
250 |
defaultValue, (T[]) null); |
|
251 |
} |
|
252 |
||
253 |
||
254 |
/** |
|
255 |
* <p>Constructs an {@code OpenMBeanAttributeInfoSupport} instance, |
|
256 |
* which describes the attribute of an open MBean with the |
|
257 |
* specified {@code name}, {@code openType}, {@code description}, |
|
258 |
* {@code defaultValue} and {@code legalValues}, and the specified |
|
259 |
* read/write access properties.</p> |
|
260 |
* |
|
261 |
* <p>The contents of {@code legalValues} are copied, so subsequent |
|
262 |
* modifications of the array referenced by {@code legalValues} |
|
263 |
* have no impact on this {@code OpenMBeanAttributeInfoSupport} |
|
264 |
* instance.</p> |
|
265 |
* |
|
266 |
* @param name cannot be a null or empty string. |
|
267 |
* |
|
268 |
* @param description cannot be a null or empty string. |
|
269 |
* |
|
270 |
* @param openType cannot be null. |
|
271 |
* |
|
272 |
* @param isReadable {@code true} if the attribute has a getter |
|
273 |
* exposed for management. |
|
274 |
* |
|
275 |
* @param isWritable {@code true} if the attribute has a setter |
|
276 |
* exposed for management. |
|
277 |
* |
|
278 |
* @param isIs {@code true} if the attribute's getter is of the |
|
279 |
* form <tt>is<i>XXX</i></tt>. |
|
280 |
* |
|
281 |
* @param defaultValue must be a valid value |
|
282 |
* for the {@code |
|
283 |
* openType} specified for this attribute; default value not |
|
284 |
* supported for {@code ArrayType} and {@code TabularType}; can |
|
285 |
* be null, in which case it means that no default value is set. |
|
286 |
* |
|
287 |
* @param legalValues each contained value must be valid for the |
|
288 |
* {@code openType} specified for this attribute; legal values |
|
289 |
* not supported for {@code ArrayType} and {@code TabularType}; |
|
290 |
* can be null or empty. |
|
291 |
* |
|
292 |
* @param <T> allows the compiler to check that the {@code |
|
293 |
* defaultValue} and {@code legalValues}, if non-null, have the |
|
294 |
* correct Java type for the given {@code openType}. |
|
295 |
* |
|
296 |
* @throws IllegalArgumentException if {@code name} or {@code |
|
297 |
* description} are null or empty string, or {@code openType} is |
|
298 |
* null. |
|
299 |
* |
|
300 |
* @throws OpenDataException if {@code defaultValue} is not a |
|
301 |
* valid value for the specified {@code openType}, or one value in |
|
302 |
* {@code legalValues} is not valid for the specified {@code |
|
303 |
* openType}, or {@code defaultValue} is non null and {@code |
|
304 |
* openType} is an {@code ArrayType} or a {@code TabularType}, or |
|
305 |
* {@code legalValues} is non null and non empty and {@code |
|
306 |
* openType} is an {@code ArrayType} or a {@code TabularType}, or |
|
307 |
* {@code legalValues} is non null and non empty and {@code |
|
308 |
* defaultValue} is not contained in {@code legalValues}. |
|
309 |
*/ |
|
310 |
public <T> OpenMBeanAttributeInfoSupport(String name, |
|
311 |
String description, |
|
312 |
OpenType<T> openType, |
|
313 |
boolean isReadable, |
|
314 |
boolean isWritable, |
|
315 |
boolean isIs, |
|
316 |
T defaultValue, |
|
317 |
T[] legalValues) |
|
318 |
throws OpenDataException { |
|
319 |
this(name, description, openType, isReadable, isWritable, isIs, |
|
320 |
defaultValue, legalValues, null, null); |
|
321 |
} |
|
322 |
||
323 |
||
324 |
/** |
|
325 |
* Constructs an {@code OpenMBeanAttributeInfoSupport} instance, |
|
326 |
* which describes the attribute of an open MBean, with the |
|
327 |
* specified {@code name}, {@code openType}, {@code description}, |
|
328 |
* {@code defaultValue}, {@code minValue} and {@code maxValue}. |
|
329 |
* |
|
330 |
* It is possible to specify minimal and maximal values only for |
|
331 |
* an open type whose values are {@code Comparable}. |
|
332 |
* |
|
333 |
* @param name cannot be a null or empty string. |
|
334 |
* |
|
335 |
* @param description cannot be a null or empty string. |
|
336 |
* |
|
337 |
* @param openType cannot be null. |
|
338 |
* |
|
339 |
* @param isReadable {@code true} if the attribute has a getter |
|
340 |
* exposed for management. |
|
341 |
* |
|
342 |
* @param isWritable {@code true} if the attribute has a setter |
|
343 |
* exposed for management. |
|
344 |
* |
|
345 |
* @param isIs {@code true} if the attribute's getter is of the |
|
346 |
* form <tt>is<i>XXX</i></tt>. |
|
347 |
* |
|
348 |
* @param defaultValue must be a valid value for the {@code |
|
349 |
* openType} specified for this attribute; default value not |
|
350 |
* supported for {@code ArrayType} and {@code TabularType}; can be |
|
351 |
* null, in which case it means that no default value is set. |
|
352 |
* |
|
353 |
* @param minValue must be valid for the {@code openType} |
|
354 |
* specified for this attribute; can be null, in which case it |
|
355 |
* means that no minimal value is set. |
|
356 |
* |
|
357 |
* @param maxValue must be valid for the {@code openType} |
|
358 |
* specified for this attribute; can be null, in which case it |
|
359 |
* means that no maximal value is set. |
|
360 |
* |
|
361 |
* @param <T> allows the compiler to check that the {@code |
|
362 |
* defaultValue}, {@code minValue}, and {@code maxValue}, if |
|
363 |
* non-null, have the correct Java type for the given {@code |
|
364 |
* openType}. |
|
365 |
* |
|
366 |
* @throws IllegalArgumentException if {@code name} or {@code |
|
367 |
* description} are null or empty string, or {@code openType} is |
|
368 |
* null. |
|
369 |
* |
|
370 |
* @throws OpenDataException if {@code defaultValue}, {@code |
|
371 |
* minValue} or {@code maxValue} is not a valid value for the |
|
372 |
* specified {@code openType}, or {@code defaultValue} is non null |
|
373 |
* and {@code openType} is an {@code ArrayType} or a {@code |
|
374 |
* TabularType}, or both {@code minValue} and {@code maxValue} are |
|
375 |
* non-null and {@code minValue.compareTo(maxValue) > 0} is {@code |
|
376 |
* true}, or both {@code defaultValue} and {@code minValue} are |
|
377 |
* non-null and {@code minValue.compareTo(defaultValue) > 0} is |
|
378 |
* {@code true}, or both {@code defaultValue} and {@code maxValue} |
|
379 |
* are non-null and {@code defaultValue.compareTo(maxValue) > 0} |
|
380 |
* is {@code true}. |
|
381 |
*/ |
|
382 |
public <T> OpenMBeanAttributeInfoSupport(String name, |
|
383 |
String description, |
|
384 |
OpenType<T> openType, |
|
385 |
boolean isReadable, |
|
386 |
boolean isWritable, |
|
387 |
boolean isIs, |
|
388 |
T defaultValue, |
|
389 |
Comparable<T> minValue, |
|
390 |
Comparable<T> maxValue) |
|
391 |
throws OpenDataException { |
|
392 |
this(name, description, openType, isReadable, isWritable, isIs, |
|
393 |
defaultValue, null, minValue, maxValue); |
|
394 |
} |
|
395 |
||
396 |
private <T> OpenMBeanAttributeInfoSupport(String name, |
|
397 |
String description, |
|
398 |
OpenType<T> openType, |
|
399 |
boolean isReadable, |
|
400 |
boolean isWritable, |
|
401 |
boolean isIs, |
|
402 |
T defaultValue, |
|
403 |
T[] legalValues, |
|
404 |
Comparable<T> minValue, |
|
405 |
Comparable<T> maxValue) |
|
406 |
throws OpenDataException { |
|
407 |
super(name, |
|
408 |
(openType==null) ? null : openType.getClassName(), |
|
409 |
description, |
|
410 |
isReadable, |
|
411 |
isWritable, |
|
412 |
isIs, |
|
413 |
makeDescriptor(openType, |
|
414 |
defaultValue, legalValues, minValue, maxValue)); |
|
415 |
||
416 |
this.openType = openType; |
|
417 |
||
418 |
Descriptor d = getDescriptor(); |
|
419 |
this.defaultValue = defaultValue; |
|
420 |
this.minValue = minValue; |
|
421 |
this.maxValue = maxValue; |
|
422 |
// We already converted the array into an unmodifiable Set |
|
423 |
// in the descriptor. |
|
424 |
this.legalValues = (Set<?>) d.getFieldValue("legalValues"); |
|
425 |
||
426 |
check(this); |
|
427 |
} |
|
428 |
||
429 |
/** |
|
430 |
* An object serialized in a version of the API before Descriptors were |
|
431 |
* added to this class will have an empty or null Descriptor. |
|
432 |
* For consistency with our |
|
433 |
* behavior in this version, we must replace the object with one |
|
434 |
* where the Descriptors reflect the same values of openType, defaultValue, |
|
435 |
* etc. |
|
436 |
**/ |
|
437 |
private Object readResolve() { |
|
438 |
if (getDescriptor().getFieldNames().length == 0) { |
|
439 |
OpenType<Object> xopenType = cast(openType); |
|
440 |
Set<Object> xlegalValues = cast(legalValues); |
|
441 |
Comparable<Object> xminValue = cast(minValue); |
|
442 |
Comparable<Object> xmaxValue = cast(maxValue); |
|
443 |
return new OpenMBeanAttributeInfoSupport( |
|
444 |
name, description, openType, |
|
445 |
isReadable(), isWritable(), isIs(), |
|
446 |
makeDescriptor(xopenType, defaultValue, xlegalValues, |
|
447 |
xminValue, xmaxValue)); |
|
448 |
} else |
|
449 |
return this; |
|
450 |
} |
|
451 |
||
452 |
static void check(OpenMBeanParameterInfo info) throws OpenDataException { |
|
1510 | 453 |
OpenType<?> openType = info.getOpenType(); |
2 | 454 |
if (openType == null) |
455 |
throw new IllegalArgumentException("OpenType cannot be null"); |
|
456 |
||
457 |
if (info.getName() == null || |
|
458 |
info.getName().trim().equals("")) |
|
459 |
throw new IllegalArgumentException("Name cannot be null or empty"); |
|
460 |
||
461 |
if (info.getDescription() == null || |
|
462 |
info.getDescription().trim().equals("")) |
|
463 |
throw new IllegalArgumentException("Description cannot be null or empty"); |
|
464 |
||
465 |
// Check and initialize defaultValue |
|
466 |
// |
|
467 |
if (info.hasDefaultValue()) { |
|
468 |
// Default value not supported for ArrayType and TabularType |
|
469 |
// Cast to Object because "OpenType<T> instanceof" is illegal |
|
470 |
if (openType.isArray() || (Object)openType instanceof TabularType) { |
|
471 |
throw new OpenDataException("Default value not supported " + |
|
472 |
"for ArrayType and TabularType"); |
|
473 |
} |
|
474 |
// Check defaultValue's class |
|
475 |
if (!openType.isValue(info.getDefaultValue())) { |
|
476 |
final String msg = |
|
477 |
"Argument defaultValue's class [\"" + |
|
478 |
info.getDefaultValue().getClass().getName() + |
|
479 |
"\"] does not match the one defined in openType[\"" + |
|
480 |
openType.getClassName() +"\"]"; |
|
481 |
throw new OpenDataException(msg); |
|
482 |
} |
|
483 |
} |
|
484 |
||
485 |
// Check that we don't have both legalValues and min or max |
|
486 |
// |
|
487 |
if (info.hasLegalValues() && |
|
488 |
(info.hasMinValue() || info.hasMaxValue())) { |
|
489 |
throw new OpenDataException("cannot have both legalValue and " + |
|
490 |
"minValue or maxValue"); |
|
491 |
} |
|
492 |
||
493 |
// Check minValue and maxValue |
|
494 |
if (info.hasMinValue() && !openType.isValue(info.getMinValue())) { |
|
495 |
final String msg = |
|
496 |
"Type of minValue [" + info.getMinValue().getClass().getName() + |
|
497 |
"] does not match OpenType [" + openType.getClassName() + "]"; |
|
498 |
throw new OpenDataException(msg); |
|
499 |
} |
|
500 |
if (info.hasMaxValue() && !openType.isValue(info.getMaxValue())) { |
|
501 |
final String msg = |
|
502 |
"Type of maxValue [" + info.getMaxValue().getClass().getName() + |
|
503 |
"] does not match OpenType [" + openType.getClassName() + "]"; |
|
504 |
throw new OpenDataException(msg); |
|
505 |
} |
|
506 |
||
507 |
// Check that defaultValue is a legal value |
|
508 |
// |
|
509 |
if (info.hasDefaultValue()) { |
|
510 |
Object defaultValue = info.getDefaultValue(); |
|
511 |
if (info.hasLegalValues() && |
|
512 |
!info.getLegalValues().contains(defaultValue)) { |
|
513 |
throw new OpenDataException("defaultValue is not contained " + |
|
514 |
"in legalValues"); |
|
515 |
} |
|
516 |
||
517 |
// Check that minValue <= defaultValue <= maxValue |
|
518 |
// |
|
519 |
if (info.hasMinValue()) { |
|
520 |
if (compare(info.getMinValue(), defaultValue) > 0) { |
|
521 |
throw new OpenDataException("minValue cannot be greater " + |
|
522 |
"than defaultValue"); |
|
523 |
} |
|
524 |
} |
|
525 |
if (info.hasMaxValue()) { |
|
526 |
if (compare(info.getMaxValue(), defaultValue) < 0) { |
|
527 |
throw new OpenDataException("maxValue cannot be less " + |
|
528 |
"than defaultValue"); |
|
529 |
} |
|
530 |
} |
|
531 |
} |
|
532 |
||
533 |
// Check legalValues |
|
534 |
// |
|
535 |
if (info.hasLegalValues()) { |
|
536 |
// legalValues not supported for TabularType and arrays |
|
537 |
if ((Object)openType instanceof TabularType || openType.isArray()) { |
|
538 |
throw new OpenDataException("Legal values not supported " + |
|
539 |
"for TabularType and arrays"); |
|
540 |
} |
|
541 |
// Check legalValues are valid with openType |
|
542 |
for (Object v : info.getLegalValues()) { |
|
543 |
if (!openType.isValue(v)) { |
|
544 |
final String msg = |
|
545 |
"Element of legalValues [" + v + |
|
546 |
"] is not a valid value for the specified openType [" + |
|
547 |
openType.toString() +"]"; |
|
548 |
throw new OpenDataException(msg); |
|
549 |
} |
|
550 |
} |
|
551 |
} |
|
552 |
||
553 |
||
554 |
// Check that, if both specified, minValue <= maxValue |
|
555 |
// |
|
556 |
if (info.hasMinValue() && info.hasMaxValue()) { |
|
557 |
if (compare(info.getMinValue(), info.getMaxValue()) > 0) { |
|
558 |
throw new OpenDataException("minValue cannot be greater " + |
|
559 |
"than maxValue"); |
|
560 |
} |
|
561 |
} |
|
562 |
||
563 |
} |
|
564 |
||
1510 | 565 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
2 | 566 |
static int compare(Object x, Object y) { |
567 |
return ((Comparable) x).compareTo(y); |
|
568 |
} |
|
569 |
||
570 |
static <T> Descriptor makeDescriptor(OpenType<T> openType, |
|
571 |
T defaultValue, |
|
572 |
T[] legalValues, |
|
573 |
Comparable<T> minValue, |
|
574 |
Comparable<T> maxValue) { |
|
575 |
Map<String, Object> map = new HashMap<String, Object>(); |
|
576 |
if (defaultValue != null) |
|
577 |
map.put("defaultValue", defaultValue); |
|
578 |
if (legalValues != null) { |
|
579 |
Set<T> set = new HashSet<T>(); |
|
580 |
for (T v : legalValues) |
|
581 |
set.add(v); |
|
582 |
set = Collections.unmodifiableSet(set); |
|
583 |
map.put("legalValues", set); |
|
584 |
} |
|
585 |
if (minValue != null) |
|
586 |
map.put("minValue", minValue); |
|
587 |
if (maxValue != null) |
|
588 |
map.put("maxValue", maxValue); |
|
589 |
if (map.isEmpty()) { |
|
590 |
return openType.getDescriptor(); |
|
591 |
} else { |
|
592 |
map.put("openType", openType); |
|
593 |
return new ImmutableDescriptor(map); |
|
594 |
} |
|
595 |
} |
|
596 |
||
597 |
static <T> Descriptor makeDescriptor(OpenType<T> openType, |
|
598 |
T defaultValue, |
|
599 |
Set<T> legalValues, |
|
600 |
Comparable<T> minValue, |
|
601 |
Comparable<T> maxValue) { |
|
602 |
T[] legals; |
|
603 |
if (legalValues == null) |
|
604 |
legals = null; |
|
605 |
else { |
|
606 |
legals = cast(new Object[legalValues.size()]); |
|
607 |
legalValues.toArray(legals); |
|
608 |
} |
|
609 |
return makeDescriptor(openType, defaultValue, legals, minValue, maxValue); |
|
610 |
} |
|
611 |
||
612 |
||
613 |
static <T> T valueFrom(Descriptor d, String name, OpenType<T> openType) { |
|
614 |
Object x = d.getFieldValue(name); |
|
615 |
if (x == null) |
|
616 |
return null; |
|
617 |
try { |
|
618 |
return convertFrom(x, openType); |
|
619 |
} catch (Exception e) { |
|
620 |
final String msg = |
|
621 |
"Cannot convert descriptor field " + name + " to " + |
|
622 |
openType.getTypeName(); |
|
623 |
throw EnvHelp.initCause(new IllegalArgumentException(msg), e); |
|
624 |
} |
|
625 |
} |
|
626 |
||
627 |
static <T> Set<T> valuesFrom(Descriptor d, String name, |
|
628 |
OpenType<T> openType) { |
|
629 |
Object x = d.getFieldValue(name); |
|
630 |
if (x == null) |
|
631 |
return null; |
|
632 |
Collection<?> coll; |
|
633 |
if (x instanceof Set<?>) { |
|
634 |
Set<?> set = (Set<?>) x; |
|
635 |
boolean asis = true; |
|
636 |
for (Object element : set) { |
|
637 |
if (!openType.isValue(element)) { |
|
638 |
asis = false; |
|
639 |
break; |
|
640 |
} |
|
641 |
} |
|
642 |
if (asis) |
|
643 |
return cast(set); |
|
644 |
coll = set; |
|
645 |
} else if (x instanceof Object[]) { |
|
646 |
coll = Arrays.asList((Object[]) x); |
|
647 |
} else { |
|
648 |
final String msg = |
|
649 |
"Descriptor value for " + name + " must be a Set or " + |
|
650 |
"an array: " + x.getClass().getName(); |
|
651 |
throw new IllegalArgumentException(msg); |
|
652 |
} |
|
653 |
||
654 |
Set<T> result = new HashSet<T>(); |
|
655 |
for (Object element : coll) |
|
656 |
result.add(convertFrom(element, openType)); |
|
657 |
return result; |
|
658 |
} |
|
659 |
||
1510 | 660 |
static <T> Comparable<?> comparableValueFrom(Descriptor d, String name, |
661 |
OpenType<T> openType) { |
|
2 | 662 |
T t = valueFrom(d, name, openType); |
663 |
if (t == null || t instanceof Comparable<?>) |
|
1510 | 664 |
return (Comparable<?>) t; |
2 | 665 |
final String msg = |
666 |
"Descriptor field " + name + " with value " + t + |
|
667 |
" is not Comparable"; |
|
668 |
throw new IllegalArgumentException(msg); |
|
669 |
} |
|
670 |
||
671 |
private static <T> T convertFrom(Object x, OpenType<T> openType) { |
|
672 |
if (openType.isValue(x)) { |
|
673 |
T t = OpenMBeanAttributeInfoSupport.<T>cast(x); |
|
674 |
return t; |
|
675 |
} |
|
676 |
return convertFromStrings(x, openType); |
|
677 |
} |
|
678 |
||
679 |
private static <T> T convertFromStrings(Object x, OpenType<T> openType) { |
|
680 |
if (openType instanceof ArrayType<?>) |
|
681 |
return convertFromStringArray(x, openType); |
|
682 |
else if (x instanceof String) |
|
683 |
return convertFromString((String) x, openType); |
|
684 |
final String msg = |
|
685 |
"Cannot convert value " + x + " of type " + |
|
686 |
x.getClass().getName() + " to type " + openType.getTypeName(); |
|
687 |
throw new IllegalArgumentException(msg); |
|
688 |
} |
|
689 |
||
690 |
private static <T> T convertFromString(String s, OpenType<T> openType) { |
|
691 |
Class<T> c; |
|
692 |
try { |
|
3446
4a07252d2e92
6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents:
1510
diff
changeset
|
693 |
c = cast(Class.forName(openType.safeGetClassName())); |
2 | 694 |
} catch (ClassNotFoundException e) { |
695 |
throw new NoClassDefFoundError(e.toString()); // can't happen |
|
696 |
} |
|
697 |
||
698 |
// Look for: public static T valueOf(String) |
|
699 |
Method valueOf; |
|
700 |
try { |
|
701 |
valueOf = c.getMethod("valueOf", String.class); |
|
702 |
if (!Modifier.isStatic(valueOf.getModifiers()) || |
|
703 |
valueOf.getReturnType() != c) |
|
704 |
valueOf = null; |
|
705 |
} catch (NoSuchMethodException e) { |
|
706 |
valueOf = null; |
|
707 |
} |
|
708 |
if (valueOf != null) { |
|
709 |
try { |
|
710 |
return c.cast(valueOf.invoke(null, s)); |
|
711 |
} catch (Exception e) { |
|
712 |
final String msg = |
|
713 |
"Could not convert \"" + s + "\" using method: " + valueOf; |
|
3446
4a07252d2e92
6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents:
1510
diff
changeset
|
714 |
throw new IllegalArgumentException(msg, e); |
2 | 715 |
} |
716 |
} |
|
717 |
||
718 |
// Look for: public T(String) |
|
719 |
Constructor<T> con; |
|
720 |
try { |
|
721 |
con = c.getConstructor(String.class); |
|
722 |
} catch (NoSuchMethodException e) { |
|
723 |
con = null; |
|
724 |
} |
|
725 |
if (con != null) { |
|
726 |
try { |
|
727 |
return con.newInstance(s); |
|
728 |
} catch (Exception e) { |
|
729 |
final String msg = |
|
730 |
"Could not convert \"" + s + "\" using constructor: " + con; |
|
3446
4a07252d2e92
6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents:
1510
diff
changeset
|
731 |
throw new IllegalArgumentException(msg, e); |
2 | 732 |
} |
733 |
} |
|
734 |
||
735 |
throw new IllegalArgumentException("Don't know how to convert " + |
|
736 |
"string to " + |
|
737 |
openType.getTypeName()); |
|
738 |
} |
|
739 |
||
740 |
||
741 |
/* A Descriptor contained an array value encoded as Strings. The |
|
742 |
Strings must be organized in an array corresponding to the desired |
|
743 |
array. If the desired array has n dimensions, so must the String |
|
744 |
array. We will convert element by element from String to desired |
|
745 |
component type. */ |
|
746 |
private static <T> T convertFromStringArray(Object x, |
|
747 |
OpenType<T> openType) { |
|
748 |
ArrayType<?> arrayType = (ArrayType<?>) openType; |
|
749 |
OpenType<?> baseType = arrayType.getElementOpenType(); |
|
750 |
int dim = arrayType.getDimension(); |
|
751 |
String squareBrackets = "["; |
|
752 |
for (int i = 1; i < dim; i++) |
|
753 |
squareBrackets += "["; |
|
754 |
Class<?> stringArrayClass; |
|
755 |
Class<?> targetArrayClass; |
|
756 |
try { |
|
757 |
stringArrayClass = |
|
758 |
Class.forName(squareBrackets + "Ljava.lang.String;"); |
|
759 |
targetArrayClass = |
|
3446
4a07252d2e92
6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents:
1510
diff
changeset
|
760 |
Class.forName(squareBrackets + "L" + baseType.safeGetClassName() + |
2 | 761 |
";"); |
762 |
} catch (ClassNotFoundException e) { |
|
763 |
throw new NoClassDefFoundError(e.toString()); // can't happen |
|
764 |
} |
|
765 |
if (!stringArrayClass.isInstance(x)) { |
|
766 |
final String msg = |
|
767 |
"Value for " + dim + "-dimensional array of " + |
|
768 |
baseType.getTypeName() + " must be same type or a String " + |
|
769 |
"array with same dimensions"; |
|
770 |
throw new IllegalArgumentException(msg); |
|
771 |
} |
|
772 |
OpenType<?> componentOpenType; |
|
773 |
if (dim == 1) |
|
774 |
componentOpenType = baseType; |
|
775 |
else { |
|
776 |
try { |
|
777 |
componentOpenType = new ArrayType<T>(dim - 1, baseType); |
|
778 |
} catch (OpenDataException e) { |
|
779 |
throw new IllegalArgumentException(e.getMessage(), e); |
|
780 |
// can't happen |
|
781 |
} |
|
782 |
} |
|
783 |
int n = Array.getLength(x); |
|
784 |
Object[] targetArray = (Object[]) |
|
785 |
Array.newInstance(targetArrayClass.getComponentType(), n); |
|
786 |
for (int i = 0; i < n; i++) { |
|
787 |
Object stringish = Array.get(x, i); // String or String[] etc |
|
788 |
Object converted = |
|
789 |
convertFromStrings(stringish, componentOpenType); |
|
790 |
Array.set(targetArray, i, converted); |
|
791 |
} |
|
792 |
return OpenMBeanAttributeInfoSupport.<T>cast(targetArray); |
|
793 |
} |
|
794 |
||
795 |
@SuppressWarnings("unchecked") |
|
796 |
static <T> T cast(Object x) { |
|
797 |
return (T) x; |
|
798 |
} |
|
799 |
||
800 |
/** |
|
801 |
* Returns the open type for the values of the attribute described |
|
802 |
* by this {@code OpenMBeanAttributeInfoSupport} instance. |
|
803 |
*/ |
|
804 |
public OpenType<?> getOpenType() { |
|
805 |
return openType; |
|
806 |
} |
|
807 |
||
808 |
/** |
|
809 |
* Returns the default value for the attribute described by this |
|
810 |
* {@code OpenMBeanAttributeInfoSupport} instance, if specified, |
|
811 |
* or {@code null} otherwise. |
|
812 |
*/ |
|
813 |
public Object getDefaultValue() { |
|
814 |
||
815 |
// Special case for ArrayType and TabularType |
|
816 |
// [JF] TODO: clone it so that it cannot be altered, |
|
817 |
// [JF] TODO: if we decide to support defaultValue as an array itself. |
|
818 |
// [JF] As of today (oct 2000) it is not supported so |
|
819 |
// defaultValue is null for arrays. Nothing to do. |
|
820 |
||
821 |
return defaultValue; |
|
822 |
} |
|
823 |
||
824 |
/** |
|
825 |
* Returns an unmodifiable Set of legal values for the attribute |
|
826 |
* described by this {@code OpenMBeanAttributeInfoSupport} |
|
827 |
* instance, if specified, or {@code null} otherwise. |
|
828 |
*/ |
|
829 |
public Set<?> getLegalValues() { |
|
830 |
||
831 |
// Special case for ArrayType and TabularType |
|
832 |
// [JF] TODO: clone values so that they cannot be altered, |
|
833 |
// [JF] TODO: if we decide to support LegalValues as an array itself. |
|
834 |
// [JF] As of today (oct 2000) it is not supported so |
|
835 |
// legalValues is null for arrays. Nothing to do. |
|
836 |
||
837 |
// Returns our legalValues Set (set was constructed unmodifiable) |
|
838 |
return (legalValues); |
|
839 |
} |
|
840 |
||
841 |
/** |
|
842 |
* Returns the minimal value for the attribute described by this |
|
843 |
* {@code OpenMBeanAttributeInfoSupport} instance, if specified, |
|
844 |
* or {@code null} otherwise. |
|
845 |
*/ |
|
846 |
public Comparable<?> getMinValue() { |
|
847 |
||
848 |
// Note: only comparable values have a minValue, |
|
849 |
// so that's not the case of arrays and tabulars (always null). |
|
850 |
||
851 |
return minValue; |
|
852 |
} |
|
853 |
||
854 |
/** |
|
855 |
* Returns the maximal value for the attribute described by this |
|
856 |
* {@code OpenMBeanAttributeInfoSupport} instance, if specified, |
|
857 |
* or {@code null} otherwise. |
|
858 |
*/ |
|
859 |
public Comparable<?> getMaxValue() { |
|
860 |
||
861 |
// Note: only comparable values have a maxValue, |
|
862 |
// so that's not the case of arrays and tabulars (always null). |
|
863 |
||
864 |
return maxValue; |
|
865 |
} |
|
866 |
||
867 |
/** |
|
868 |
* Returns {@code true} if this {@code |
|
869 |
* OpenMBeanAttributeInfoSupport} instance specifies a non-null |
|
870 |
* default value for the described attribute, {@code false} |
|
871 |
* otherwise. |
|
872 |
*/ |
|
873 |
public boolean hasDefaultValue() { |
|
874 |
||
875 |
return (defaultValue != null); |
|
876 |
} |
|
877 |
||
878 |
/** |
|
879 |
* Returns {@code true} if this {@code |
|
880 |
* OpenMBeanAttributeInfoSupport} instance specifies a non-null |
|
881 |
* set of legal values for the described attribute, {@code false} |
|
882 |
* otherwise. |
|
883 |
*/ |
|
884 |
public boolean hasLegalValues() { |
|
885 |
||
886 |
return (legalValues != null); |
|
887 |
} |
|
888 |
||
889 |
/** |
|
890 |
* Returns {@code true} if this {@code |
|
891 |
* OpenMBeanAttributeInfoSupport} instance specifies a non-null |
|
892 |
* minimal value for the described attribute, {@code false} |
|
893 |
* otherwise. |
|
894 |
*/ |
|
895 |
public boolean hasMinValue() { |
|
896 |
||
897 |
return (minValue != null); |
|
898 |
} |
|
899 |
||
900 |
/** |
|
901 |
* Returns {@code true} if this {@code |
|
902 |
* OpenMBeanAttributeInfoSupport} instance specifies a non-null |
|
903 |
* maximal value for the described attribute, {@code false} |
|
904 |
* otherwise. |
|
905 |
*/ |
|
906 |
public boolean hasMaxValue() { |
|
907 |
||
908 |
return (maxValue != null); |
|
909 |
} |
|
910 |
||
911 |
||
912 |
/** |
|
913 |
* Tests whether {@code obj} is a valid value for the attribute |
|
914 |
* described by this {@code OpenMBeanAttributeInfoSupport} |
|
915 |
* instance. |
|
916 |
* |
|
917 |
* @param obj the object to be tested. |
|
918 |
* |
|
919 |
* @return {@code true} if {@code obj} is a valid value for |
|
920 |
* the parameter described by this {@code |
|
921 |
* OpenMBeanAttributeInfoSupport} instance, {@code false} |
|
922 |
* otherwise. |
|
923 |
*/ |
|
924 |
public boolean isValue(Object obj) { |
|
925 |
return isValue(this, obj); |
|
926 |
} |
|
927 |
||
1510 | 928 |
@SuppressWarnings({"unchecked", "rawtypes"}) // cast to Comparable |
2 | 929 |
static boolean isValue(OpenMBeanParameterInfo info, Object obj) { |
930 |
if (info.hasDefaultValue() && obj == null) |
|
931 |
return true; |
|
932 |
return |
|
933 |
info.getOpenType().isValue(obj) && |
|
934 |
(!info.hasLegalValues() || info.getLegalValues().contains(obj)) && |
|
935 |
(!info.hasMinValue() || |
|
936 |
((Comparable) info.getMinValue()).compareTo(obj) <= 0) && |
|
937 |
(!info.hasMaxValue() || |
|
938 |
((Comparable) info.getMaxValue()).compareTo(obj) >= 0); |
|
939 |
} |
|
940 |
||
941 |
/* *** Commodity methods from java.lang.Object *** */ |
|
942 |
||
943 |
||
944 |
/** |
|
945 |
* Compares the specified {@code obj} parameter with this {@code |
|
946 |
* OpenMBeanAttributeInfoSupport} instance for equality. |
|
947 |
* <p> |
|
948 |
* Returns {@code true} if and only if all of the following statements are true: |
|
949 |
* <ul> |
|
950 |
* <li>{@code obj} is non null,</li> |
|
951 |
* <li>{@code obj} also implements the {@code OpenMBeanAttributeInfo} interface,</li> |
|
952 |
* <li>their names are equal</li> |
|
953 |
* <li>their open types are equal</li> |
|
954 |
* <li>their access properties (isReadable, isWritable and isIs) are equal</li> |
|
955 |
* <li>their default, min, max and legal values are equal.</li> |
|
956 |
* </ul> |
|
957 |
* This ensures that this {@code equals} method works properly for |
|
958 |
* {@code obj} parameters which are different implementations of |
|
959 |
* the {@code OpenMBeanAttributeInfo} interface. |
|
960 |
* |
|
961 |
* <p>If {@code obj} also implements {@link DescriptorRead}, then its |
|
962 |
* {@link DescriptorRead#getDescriptor() getDescriptor()} method must |
|
963 |
* also return the same value as for this object.</p> |
|
964 |
* |
|
965 |
* @param obj the object to be compared for equality with this |
|
966 |
* {@code OpenMBeanAttributeInfoSupport} instance. |
|
967 |
* |
|
968 |
* @return {@code true} if the specified object is equal to this |
|
969 |
* {@code OpenMBeanAttributeInfoSupport} instance. |
|
970 |
*/ |
|
971 |
public boolean equals(Object obj) { |
|
972 |
if (!(obj instanceof OpenMBeanAttributeInfo)) |
|
973 |
return false; |
|
974 |
||
975 |
OpenMBeanAttributeInfo other = (OpenMBeanAttributeInfo) obj; |
|
976 |
||
977 |
return |
|
978 |
this.isReadable() == other.isReadable() && |
|
979 |
this.isWritable() == other.isWritable() && |
|
980 |
this.isIs() == other.isIs() && |
|
981 |
equal(this, other); |
|
982 |
} |
|
983 |
||
984 |
static boolean equal(OpenMBeanParameterInfo x1, OpenMBeanParameterInfo x2) { |
|
985 |
if (x1 instanceof DescriptorRead) { |
|
986 |
if (!(x2 instanceof DescriptorRead)) |
|
987 |
return false; |
|
988 |
Descriptor d1 = ((DescriptorRead) x1).getDescriptor(); |
|
989 |
Descriptor d2 = ((DescriptorRead) x2).getDescriptor(); |
|
990 |
if (!d1.equals(d2)) |
|
991 |
return false; |
|
992 |
} else if (x2 instanceof DescriptorRead) |
|
993 |
return false; |
|
994 |
||
995 |
return |
|
996 |
x1.getName().equals(x2.getName()) && |
|
997 |
x1.getOpenType().equals(x2.getOpenType()) && |
|
998 |
(x1.hasDefaultValue() ? |
|
999 |
x1.getDefaultValue().equals(x2.getDefaultValue()) : |
|
1000 |
!x2.hasDefaultValue()) && |
|
1001 |
(x1.hasMinValue() ? |
|
1002 |
x1.getMinValue().equals(x2.getMinValue()) : |
|
1003 |
!x2.hasMinValue()) && |
|
1004 |
(x1.hasMaxValue() ? |
|
1005 |
x1.getMaxValue().equals(x2.getMaxValue()) : |
|
1006 |
!x2.hasMaxValue()) && |
|
1007 |
(x1.hasLegalValues() ? |
|
1008 |
x1.getLegalValues().equals(x2.getLegalValues()) : |
|
1009 |
!x2.hasLegalValues()); |
|
1010 |
} |
|
1011 |
||
1012 |
/** |
|
1013 |
* <p>Returns the hash code value for this {@code |
|
1014 |
* OpenMBeanAttributeInfoSupport} instance.</p> |
|
1015 |
* |
|
1016 |
* <p>The hash code of an {@code OpenMBeanAttributeInfoSupport} |
|
1017 |
* instance is the sum of the hash codes of all elements of |
|
1018 |
* information used in {@code equals} comparisons (ie: its name, |
|
1019 |
* its <i>open type</i>, its default, min, max and legal |
|
1020 |
* values, and its Descriptor). |
|
1021 |
* |
|
1022 |
* <p>This ensures that {@code t1.equals(t2)} implies that {@code |
|
1023 |
* t1.hashCode()==t2.hashCode()} for any two {@code |
|
1024 |
* OpenMBeanAttributeInfoSupport} instances {@code t1} and {@code |
|
1025 |
* t2}, as required by the general contract of the method {@link |
|
1026 |
* Object#hashCode() Object.hashCode()}. |
|
1027 |
* |
|
1028 |
* <p>However, note that another instance of a class implementing |
|
1029 |
* the {@code OpenMBeanAttributeInfo} interface may be equal to |
|
1030 |
* this {@code OpenMBeanAttributeInfoSupport} instance as defined |
|
1031 |
* by {@link #equals(java.lang.Object)}, but may have a different |
|
1032 |
* hash code if it is calculated differently. |
|
1033 |
* |
|
1034 |
* <p>As {@code OpenMBeanAttributeInfoSupport} instances are |
|
1035 |
* immutable, the hash code for this instance is calculated once, |
|
1036 |
* on the first call to {@code hashCode}, and then the same value |
|
1037 |
* is returned for subsequent calls. |
|
1038 |
* |
|
1039 |
* @return the hash code value for this {@code |
|
1040 |
* OpenMBeanAttributeInfoSupport} instance |
|
1041 |
*/ |
|
1042 |
public int hashCode() { |
|
1043 |
||
1044 |
// Calculate the hash code value if it has not yet been done |
|
1045 |
// (ie 1st call to hashCode()) |
|
1046 |
// |
|
1047 |
if (myHashCode == null) |
|
1048 |
myHashCode = hashCode(this); |
|
1049 |
||
1050 |
// return always the same hash code for this instance (immutable) |
|
1051 |
// |
|
1052 |
return myHashCode.intValue(); |
|
1053 |
} |
|
1054 |
||
1055 |
static int hashCode(OpenMBeanParameterInfo info) { |
|
1056 |
int value = 0; |
|
1057 |
value += info.getName().hashCode(); |
|
1058 |
value += info.getOpenType().hashCode(); |
|
1059 |
if (info.hasDefaultValue()) |
|
1060 |
value += info.getDefaultValue().hashCode(); |
|
1061 |
if (info.hasMinValue()) |
|
1062 |
value += info.getMinValue().hashCode(); |
|
1063 |
if (info.hasMaxValue()) |
|
1064 |
value += info.getMaxValue().hashCode(); |
|
1065 |
if (info.hasLegalValues()) |
|
1066 |
value += info.getLegalValues().hashCode(); |
|
1067 |
if (info instanceof DescriptorRead) |
|
1068 |
value += ((DescriptorRead) info).getDescriptor().hashCode(); |
|
1069 |
return value; |
|
1070 |
} |
|
1071 |
||
1072 |
/** |
|
1073 |
* Returns a string representation of this |
|
1074 |
* {@code OpenMBeanAttributeInfoSupport} instance. |
|
1075 |
* <p> |
|
1076 |
* The string representation consists of the name of this class (i.e. |
|
1077 |
* {@code javax.management.openmbean.OpenMBeanAttributeInfoSupport}), |
|
1078 |
* the string representation of the name and open type of the |
|
1079 |
* described parameter, the string representation of its |
|
1080 |
* default, min, max and legal values and the string |
|
1081 |
* representation of its descriptor. |
|
1082 |
* |
|
1083 |
* <p>As {@code OpenMBeanAttributeInfoSupport} instances are |
|
1084 |
* immutable, the string representation for this instance is |
|
1085 |
* calculated once, on the first call to {@code toString}, and |
|
1086 |
* then the same value is returned for subsequent calls. |
|
1087 |
* |
|
1088 |
* @return a string representation of this |
|
1089 |
* {@code OpenMBeanAttributeInfoSupport} instance. |
|
1090 |
*/ |
|
1091 |
public String toString() { |
|
1092 |
||
1093 |
// Calculate the string value if it has not yet been done |
|
1094 |
// (ie 1st call to toString()) |
|
1095 |
// |
|
1096 |
if (myToString == null) |
|
1097 |
myToString = toString(this); |
|
1098 |
||
1099 |
// return always the same string representation for this |
|
1100 |
// instance (immutable) |
|
1101 |
// |
|
1102 |
return myToString; |
|
1103 |
} |
|
1104 |
||
1105 |
static String toString(OpenMBeanParameterInfo info) { |
|
1106 |
Descriptor d = (info instanceof DescriptorRead) ? |
|
1107 |
((DescriptorRead) info).getDescriptor() : null; |
|
1108 |
return |
|
1109 |
info.getClass().getName() + |
|
1110 |
"(name=" + info.getName() + |
|
1111 |
",openType=" + info.getOpenType() + |
|
1112 |
",default=" + info.getDefaultValue() + |
|
1113 |
",minValue=" + info.getMinValue() + |
|
1114 |
",maxValue=" + info.getMaxValue() + |
|
1115 |
",legalValues=" + info.getLegalValues() + |
|
1116 |
((d == null) ? "" : ",descriptor=" + d) + |
|
1117 |
")"; |
|
1118 |
} |
|
1119 |
} |