Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.management/share/classes/javax/management/MBeanAttributeInfo.java
41154 views
1
/*
2
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.management;
27
28
import java.lang.reflect.Method;
29
import java.security.AccessController;
30
31
import com.sun.jmx.mbeanserver.GetPropertyAction;
32
import com.sun.jmx.mbeanserver.Introspector;
33
import java.util.Objects;
34
35
36
/**
37
* Describes an MBean attribute exposed for management. Instances of
38
* this class are immutable. Subclasses may be mutable but this is
39
* not recommended.
40
*
41
* @since 1.5
42
*/
43
@SuppressWarnings("serial") // serialVersionUID not constant
44
public class MBeanAttributeInfo extends MBeanFeatureInfo implements Cloneable {
45
46
/* Serial version */
47
private static final long serialVersionUID;
48
static {
49
/* For complicated reasons, the serialVersionUID changed
50
between JMX 1.0 and JMX 1.1, even though JMX 1.1 did not
51
have compatibility code for this class. So the
52
serialization produced by this class with JMX 1.2 and
53
jmx.serial.form=1.0 is not the same as that produced by
54
this class with JMX 1.1 and jmx.serial.form=1.0. However,
55
the serialization without that property is the same, and
56
that is the only form required by JMX 1.2.
57
*/
58
long uid = 8644704819898565848L;
59
try {
60
GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
61
@SuppressWarnings("removal")
62
String form = AccessController.doPrivileged(act);
63
if ("1.0".equals(form))
64
uid = 7043855487133450673L;
65
} catch (Exception e) {
66
// OK: exception means no compat with 1.0, too bad
67
}
68
serialVersionUID = uid;
69
}
70
71
static final MBeanAttributeInfo[] NO_ATTRIBUTES =
72
new MBeanAttributeInfo[0];
73
74
/**
75
* @serial The actual attribute type.
76
*/
77
private final String attributeType;
78
79
/**
80
* @serial The attribute write right.
81
*/
82
private final boolean isWrite;
83
84
/**
85
* @serial The attribute read right.
86
*/
87
private final boolean isRead;
88
89
/**
90
* @serial Indicates if this method is a "is"
91
*/
92
private final boolean is;
93
94
95
/**
96
* Constructs an {@code MBeanAttributeInfo} object.
97
*
98
* @param name The name of the attribute.
99
* @param type The type or class name of the attribute.
100
* @param description A human readable description of the attribute.
101
* @param isReadable True if the attribute has a getter method, false otherwise.
102
* @param isWritable True if the attribute has a setter method, false otherwise.
103
* @param isIs True if this attribute has an "is" getter, false otherwise.
104
*
105
* @throws IllegalArgumentException if {@code isIs} is true but
106
* {@code isReadable} is not, or if {@code isIs} is true and
107
* {@code type} is not {@code boolean} or {@code java.lang.Boolean}.
108
* (New code should always use {@code boolean} rather than
109
* {@code java.lang.Boolean}.)
110
*/
111
public MBeanAttributeInfo(String name,
112
String type,
113
String description,
114
boolean isReadable,
115
boolean isWritable,
116
boolean isIs) {
117
this(name, type, description, isReadable, isWritable, isIs,
118
(Descriptor) null);
119
}
120
121
/**
122
* Constructs an {@code MBeanAttributeInfo} object.
123
*
124
* @param name The name of the attribute.
125
* @param type The type or class name of the attribute.
126
* @param description A human readable description of the attribute.
127
* @param isReadable True if the attribute has a getter method, false otherwise.
128
* @param isWritable True if the attribute has a setter method, false otherwise.
129
* @param isIs True if this attribute has an "is" getter, false otherwise.
130
* @param descriptor The descriptor for the attribute. This may be null
131
* which is equivalent to an empty descriptor.
132
*
133
* @throws IllegalArgumentException if {@code isIs} is true but
134
* {@code isReadable} is not, or if {@code isIs} is true and
135
* {@code type} is not {@code boolean} or {@code java.lang.Boolean}.
136
* (New code should always use {@code boolean} rather than
137
* {@code java.lang.Boolean}.)
138
*
139
* @since 1.6
140
*/
141
public MBeanAttributeInfo(String name,
142
String type,
143
String description,
144
boolean isReadable,
145
boolean isWritable,
146
boolean isIs,
147
Descriptor descriptor) {
148
super(name, description, descriptor);
149
150
this.attributeType = type;
151
this.isRead = isReadable;
152
this.isWrite = isWritable;
153
if (isIs && !isReadable) {
154
throw new IllegalArgumentException("Cannot have an \"is\" getter " +
155
"for a non-readable attribute");
156
}
157
if (isIs && !type.equals("java.lang.Boolean") &&
158
!type.equals("boolean")) {
159
throw new IllegalArgumentException("Cannot have an \"is\" getter " +
160
"for a non-boolean attribute");
161
}
162
this.is = isIs;
163
}
164
165
/**
166
* <p>This constructor takes the name of a simple attribute, and Method
167
* objects for reading and writing the attribute. The {@link Descriptor}
168
* of the constructed object will include fields contributed by any
169
* annotations on the {@code Method} objects that contain the
170
* {@link DescriptorKey} meta-annotation.
171
*
172
* @param name The programmatic name of the attribute.
173
* @param description A human readable description of the attribute.
174
* @param getter The method used for reading the attribute value.
175
* May be null if the property is write-only.
176
* @param setter The method used for writing the attribute value.
177
* May be null if the attribute is read-only.
178
* @exception IntrospectionException There is a consistency
179
* problem in the definition of this attribute.
180
*/
181
public MBeanAttributeInfo(String name,
182
String description,
183
Method getter,
184
Method setter) throws IntrospectionException {
185
this(name,
186
attributeType(getter, setter),
187
description,
188
(getter != null),
189
(setter != null),
190
isIs(getter),
191
ImmutableDescriptor.union(Introspector.descriptorForElement(getter),
192
Introspector.descriptorForElement(setter)));
193
}
194
195
/**
196
* <p>Returns a shallow clone of this instance.
197
* The clone is obtained by simply calling {@code super.clone()},
198
* thus calling the default native shallow cloning mechanism
199
* implemented by {@code Object.clone()}.
200
* No deeper cloning of any internal field is made.</p>
201
*
202
* <p>Since this class is immutable, cloning is chiefly of
203
* interest to subclasses.</p>
204
*/
205
public Object clone () {
206
try {
207
return super.clone() ;
208
} catch (CloneNotSupportedException e) {
209
// should not happen as this class is cloneable
210
return null;
211
}
212
}
213
214
/**
215
* Returns the class name of the attribute.
216
*
217
* @return the class name.
218
*/
219
public String getType() {
220
return attributeType;
221
}
222
223
/**
224
* Whether the value of the attribute can be read.
225
*
226
* @return True if the attribute can be read, false otherwise.
227
*/
228
public boolean isReadable() {
229
return isRead;
230
}
231
232
/**
233
* Whether new values can be written to the attribute.
234
*
235
* @return True if the attribute can be written to, false otherwise.
236
*/
237
public boolean isWritable() {
238
return isWrite;
239
}
240
241
/**
242
* Indicates if this attribute has an "is" getter.
243
*
244
* @return true if this attribute has an "is" getter.
245
*/
246
public boolean isIs() {
247
return is;
248
}
249
250
public String toString() {
251
String access;
252
if (isReadable()) {
253
if (isWritable())
254
access = "read/write";
255
else
256
access = "read-only";
257
} else if (isWritable())
258
access = "write-only";
259
else
260
access = "no-access";
261
262
return
263
getClass().getName() + "[" +
264
"description=" + getDescription() + ", " +
265
"name=" + getName() + ", " +
266
"type=" + getType() + ", " +
267
access + ", " +
268
(isIs() ? "isIs, " : "") +
269
"descriptor=" + getDescriptor() +
270
"]";
271
}
272
273
/**
274
* Compare this MBeanAttributeInfo to another.
275
*
276
* @param o the object to compare to.
277
*
278
* @return true if and only if {@code o} is an MBeanAttributeInfo such
279
* that its {@link #getName()}, {@link #getType()}, {@link
280
* #getDescription()}, {@link #isReadable()}, {@link
281
* #isWritable()}, and {@link #isIs()} values are equal (not
282
* necessarily identical) to those of this MBeanAttributeInfo.
283
*/
284
public boolean equals(Object o) {
285
if (o == this)
286
return true;
287
if (!(o instanceof MBeanAttributeInfo))
288
return false;
289
MBeanAttributeInfo p = (MBeanAttributeInfo) o;
290
return (Objects.equals(p.getName(), getName()) &&
291
Objects.equals(p.getType(), getType()) &&
292
Objects.equals(p.getDescription(), getDescription()) &&
293
Objects.equals(p.getDescriptor(), getDescriptor()) &&
294
p.isReadable() == isReadable() &&
295
p.isWritable() == isWritable() &&
296
p.isIs() == isIs());
297
}
298
299
/* We do not include everything in the hashcode. We assume that
300
if two operations are different they'll probably have different
301
names or types. The penalty we pay when this assumption is
302
wrong should be less than the penalty we would pay if it were
303
right and we needlessly hashed in the description and parameter
304
array. */
305
public int hashCode() {
306
return Objects.hash(getName(), getType());
307
}
308
309
private static boolean isIs(Method getter) {
310
return (getter != null &&
311
getter.getName().startsWith("is") &&
312
(getter.getReturnType().equals(Boolean.TYPE) ||
313
getter.getReturnType().equals(Boolean.class)));
314
}
315
316
/**
317
* Finds the type of the attribute.
318
*/
319
private static String attributeType(Method getter, Method setter)
320
throws IntrospectionException {
321
Class<?> type = null;
322
323
if (getter != null) {
324
if (getter.getParameterTypes().length != 0) {
325
throw new IntrospectionException("bad getter arg count");
326
}
327
type = getter.getReturnType();
328
if (type == Void.TYPE) {
329
throw new IntrospectionException("getter " + getter.getName() +
330
" returns void");
331
}
332
}
333
334
if (setter != null) {
335
Class<?> params[] = setter.getParameterTypes();
336
if (params.length != 1) {
337
throw new IntrospectionException("bad setter arg count");
338
}
339
if (type == null)
340
type = params[0];
341
else if (type != params[0]) {
342
throw new IntrospectionException("type mismatch between " +
343
"getter and setter");
344
}
345
}
346
347
if (type == null) {
348
throw new IntrospectionException("getter and setter cannot " +
349
"both be null");
350
}
351
352
return type.getName();
353
}
354
355
}
356
357