author | dholmes |
Mon, 18 Nov 2019 23:41:06 -0500 | |
changeset 59132 | 189f47d990b5 |
parent 47909 | da4fb7d2f917 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
47909
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.management; |
|
27 |
||
28 |
import com.sun.jmx.mbeanserver.Introspector; |
|
29 |
import java.lang.annotation.Annotation; |
|
30 |
import java.lang.reflect.Method; |
|
31 |
import java.util.Arrays; |
|
19852 | 32 |
import java.util.Objects; |
2 | 33 |
|
34 |
/** |
|
35 |
* Describes a management operation exposed by an MBean. Instances of |
|
36 |
* this class are immutable. Subclasses may be mutable but this is |
|
37 |
* not recommended. |
|
38 |
* |
|
39 |
* @since 1.5 |
|
40 |
*/ |
|
41 |
public class MBeanOperationInfo extends MBeanFeatureInfo implements Cloneable { |
|
42 |
||
43 |
/* Serial version */ |
|
44 |
static final long serialVersionUID = -6178860474881375330L; |
|
45 |
||
46 |
static final MBeanOperationInfo[] NO_OPERATIONS = |
|
47 |
new MBeanOperationInfo[0]; |
|
48 |
||
49 |
/** |
|
833 | 50 |
* Indicates that the operation is read-like: |
51 |
* it returns information but does not change any state. |
|
2 | 52 |
*/ |
53 |
public static final int INFO = 0; |
|
54 |
||
55 |
/** |
|
833 | 56 |
* Indicates that the operation is write-like: it has an effect but does |
57 |
* not return any information from the MBean. |
|
2 | 58 |
*/ |
59 |
public static final int ACTION = 1; |
|
60 |
||
61 |
/** |
|
833 | 62 |
* Indicates that the operation is both read-like and write-like: |
63 |
* it has an effect, and it also returns information from the MBean. |
|
2 | 64 |
*/ |
65 |
public static final int ACTION_INFO = 2; |
|
66 |
||
67 |
/** |
|
833 | 68 |
* Indicates that the impact of the operation is unknown or cannot be |
69 |
* expressed using one of the other values. |
|
2 | 70 |
*/ |
71 |
public static final int UNKNOWN = 3; |
|
72 |
||
73 |
/** |
|
74 |
* @serial The method's return value. |
|
75 |
*/ |
|
76 |
private final String type; |
|
77 |
||
78 |
/** |
|
79 |
* @serial The signature of the method, that is, the class names |
|
80 |
* of the arguments. |
|
81 |
*/ |
|
82 |
private final MBeanParameterInfo[] signature; |
|
83 |
||
84 |
/** |
|
85 |
* @serial The impact of the method, one of |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
86 |
* {@code INFO, ACTION, ACTION_INFO, UNKNOWN}. |
2 | 87 |
*/ |
88 |
private final int impact; |
|
89 |
||
90 |
/** @see MBeanInfo#arrayGettersSafe */ |
|
91 |
private final transient boolean arrayGettersSafe; |
|
92 |
||
93 |
||
94 |
/** |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
95 |
* Constructs an {@code MBeanOperationInfo} object. The |
2 | 96 |
* {@link Descriptor} of the constructed object will include |
97 |
* fields contributed by any annotations on the {@code Method} |
|
98 |
* object that contain the {@link DescriptorKey} meta-annotation. |
|
99 |
* |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
100 |
* @param method The {@code java.lang.reflect.Method} object |
2 | 101 |
* describing the MBean operation. |
102 |
* @param description A human readable description of the operation. |
|
103 |
*/ |
|
104 |
public MBeanOperationInfo(String description, Method method) { |
|
105 |
this(method.getName(), |
|
106 |
description, |
|
107 |
methodSignature(method), |
|
108 |
method.getReturnType().getName(), |
|
109 |
UNKNOWN, |
|
4156 | 110 |
Introspector.descriptorForElement(method)); |
2 | 111 |
} |
112 |
||
113 |
/** |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
114 |
* Constructs an {@code MBeanOperationInfo} object. |
2 | 115 |
* |
116 |
* @param name The name of the method. |
|
117 |
* @param description A human readable description of the operation. |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
118 |
* @param signature {@code MBeanParameterInfo} objects |
2 | 119 |
* describing the parameters(arguments) of the method. This may be |
120 |
* null with the same effect as a zero-length array. |
|
121 |
* @param type The type of the method's return value. |
|
833 | 122 |
* @param impact The impact of the method, one of |
123 |
* {@link #INFO}, {@link #ACTION}, {@link #ACTION_INFO}, |
|
124 |
* {@link #UNKNOWN}. |
|
2 | 125 |
*/ |
126 |
public MBeanOperationInfo(String name, |
|
127 |
String description, |
|
128 |
MBeanParameterInfo[] signature, |
|
129 |
String type, |
|
130 |
int impact) { |
|
131 |
this(name, description, signature, type, impact, (Descriptor) null); |
|
132 |
} |
|
133 |
||
134 |
/** |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
135 |
* Constructs an {@code MBeanOperationInfo} object. |
2 | 136 |
* |
137 |
* @param name The name of the method. |
|
138 |
* @param description A human readable description of the operation. |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
139 |
* @param signature {@code MBeanParameterInfo} objects |
2 | 140 |
* describing the parameters(arguments) of the method. This may be |
141 |
* null with the same effect as a zero-length array. |
|
142 |
* @param type The type of the method's return value. |
|
833 | 143 |
* @param impact The impact of the method, one of |
144 |
* {@link #INFO}, {@link #ACTION}, {@link #ACTION_INFO}, |
|
145 |
* {@link #UNKNOWN}. |
|
2 | 146 |
* @param descriptor The descriptor for the operation. This may be null |
147 |
* which is equivalent to an empty descriptor. |
|
148 |
* |
|
47909
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
149 |
* @throws IllegalArgumentException if {@code impact} is not one of |
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
150 |
* {@linkplain #ACTION}, {@linkplain #ACTION_INFO}, {@linkplain #INFO} or {@linkplain #UNKNOWN}. |
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
151 |
* |
2 | 152 |
* @since 1.6 |
153 |
*/ |
|
154 |
public MBeanOperationInfo(String name, |
|
155 |
String description, |
|
156 |
MBeanParameterInfo[] signature, |
|
157 |
String type, |
|
158 |
int impact, |
|
159 |
Descriptor descriptor) { |
|
160 |
||
161 |
super(name, description, descriptor); |
|
162 |
||
47909
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
163 |
if (impact < INFO || impact > UNKNOWN) { |
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
164 |
throw new IllegalArgumentException("Argument impact can only be " |
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
165 |
+ "one of ACTION, ACTION_INFO, " |
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
166 |
+ "INFO, or UNKNOWN" + " given value is :" + impact); |
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
167 |
} |
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
168 |
|
2 | 169 |
if (signature == null || signature.length == 0) |
170 |
signature = MBeanParameterInfo.NO_PARAMS; |
|
171 |
else |
|
172 |
signature = signature.clone(); |
|
173 |
this.signature = signature; |
|
174 |
this.type = type; |
|
175 |
this.impact = impact; |
|
176 |
this.arrayGettersSafe = |
|
177 |
MBeanInfo.arrayGettersSafe(this.getClass(), |
|
178 |
MBeanOperationInfo.class); |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* <p>Returns a shallow clone of this instance. |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
183 |
* The clone is obtained by simply calling {@code super.clone()}, |
2 | 184 |
* thus calling the default native shallow cloning mechanism |
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
185 |
* implemented by {@code Object.clone()}. |
2 | 186 |
* No deeper cloning of any internal field is made.</p> |
187 |
* |
|
188 |
* <p>Since this class is immutable, cloning is chiefly of interest |
|
189 |
* to subclasses.</p> |
|
190 |
*/ |
|
4156 | 191 |
@Override |
2 | 192 |
public Object clone () { |
193 |
try { |
|
194 |
return super.clone() ; |
|
195 |
} catch (CloneNotSupportedException e) { |
|
196 |
// should not happen as this class is cloneable |
|
197 |
return null; |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* Returns the type of the method's return value. |
|
203 |
* |
|
204 |
* @return the return type. |
|
205 |
*/ |
|
206 |
public String getReturnType() { |
|
207 |
return type; |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* <p>Returns the list of parameters for this operation. Each |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
212 |
* parameter is described by an {@code MBeanParameterInfo} |
2 | 213 |
* object.</p> |
214 |
* |
|
215 |
* <p>The returned array is a shallow copy of the internal array, |
|
216 |
* which means that it is a copy of the internal array of |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
217 |
* references to the {@code MBeanParameterInfo} objects but |
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
218 |
* that each referenced {@code MBeanParameterInfo} object is |
2 | 219 |
* not copied.</p> |
220 |
* |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
221 |
* @return An array of {@code MBeanParameterInfo} objects. |
2 | 222 |
*/ |
223 |
public MBeanParameterInfo[] getSignature() { |
|
224 |
// If MBeanOperationInfo was created in our implementation, |
|
225 |
// signature cannot be null - because our constructors replace |
|
226 |
// null with MBeanParameterInfo.NO_PARAMS; |
|
227 |
// |
|
228 |
// However, signature could be null if an MBeanOperationInfo is |
|
229 |
// deserialized from a byte array produced by another implementation. |
|
230 |
// This is not very likely but possible, since the serial form says |
|
231 |
// nothing against it. (see 6373150) |
|
232 |
// |
|
233 |
if (signature == null) |
|
234 |
// if signature is null simply return an empty array . |
|
235 |
// |
|
236 |
return MBeanParameterInfo.NO_PARAMS; |
|
237 |
else if (signature.length == 0) |
|
238 |
return signature; |
|
239 |
else |
|
240 |
return signature.clone(); |
|
241 |
} |
|
242 |
||
243 |
private MBeanParameterInfo[] fastGetSignature() { |
|
244 |
if (arrayGettersSafe) { |
|
245 |
// if signature is null simply return an empty array . |
|
246 |
// see getSignature() above. |
|
247 |
// |
|
248 |
if (signature == null) |
|
249 |
return MBeanParameterInfo.NO_PARAMS; |
|
250 |
else return signature; |
|
251 |
} else return getSignature(); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Returns the impact of the method, one of |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
256 |
* {@code INFO, ACTION, ACTION_INFO, UNKNOWN}. |
2 | 257 |
* |
258 |
* @return the impact code. |
|
259 |
*/ |
|
260 |
public int getImpact() { |
|
261 |
return impact; |
|
262 |
} |
|
263 |
||
4156 | 264 |
@Override |
2 | 265 |
public String toString() { |
266 |
String impactString; |
|
267 |
switch (getImpact()) { |
|
268 |
case ACTION: impactString = "action"; break; |
|
269 |
case ACTION_INFO: impactString = "action/info"; break; |
|
270 |
case INFO: impactString = "info"; break; |
|
47909
da4fb7d2f917
8024352: MBeanOperationInfo accepts any int value as "impact"
uvangapally
parents:
47216
diff
changeset
|
271 |
default: impactString = "unknown"; |
2 | 272 |
} |
273 |
return getClass().getName() + "[" + |
|
274 |
"description=" + getDescription() + ", " + |
|
275 |
"name=" + getName() + ", " + |
|
276 |
"returnType=" + getReturnType() + ", " + |
|
277 |
"signature=" + Arrays.asList(fastGetSignature()) + ", " + |
|
278 |
"impact=" + impactString + ", " + |
|
279 |
"descriptor=" + getDescriptor() + |
|
280 |
"]"; |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* Compare this MBeanOperationInfo to another. |
|
285 |
* |
|
286 |
* @param o the object to compare to. |
|
287 |
* |
|
32034
05676cfd40b5
8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents:
25859
diff
changeset
|
288 |
* @return true if and only if {@code o} is an MBeanOperationInfo such |
2 | 289 |
* that its {@link #getName()}, {@link #getReturnType()}, {@link |
290 |
* #getDescription()}, {@link #getImpact()}, {@link #getDescriptor()} |
|
291 |
* and {@link #getSignature()} values are equal (not necessarily identical) |
|
292 |
* to those of this MBeanConstructorInfo. Two signature arrays |
|
293 |
* are equal if their elements are pairwise equal. |
|
294 |
*/ |
|
4156 | 295 |
@Override |
2 | 296 |
public boolean equals(Object o) { |
297 |
if (o == this) |
|
298 |
return true; |
|
299 |
if (!(o instanceof MBeanOperationInfo)) |
|
300 |
return false; |
|
301 |
MBeanOperationInfo p = (MBeanOperationInfo) o; |
|
20174 | 302 |
return (Objects.equals(p.getName(), getName()) && |
303 |
Objects.equals(p.getReturnType(), getReturnType()) && |
|
304 |
Objects.equals(p.getDescription(), getDescription()) && |
|
2 | 305 |
p.getImpact() == getImpact() && |
306 |
Arrays.equals(p.fastGetSignature(), fastGetSignature()) && |
|
20174 | 307 |
Objects.equals(p.getDescriptor(), getDescriptor())); |
2 | 308 |
} |
309 |
||
310 |
/* We do not include everything in the hashcode. We assume that |
|
311 |
if two operations are different they'll probably have different |
|
312 |
names or types. The penalty we pay when this assumption is |
|
313 |
wrong should be less than the penalty we would pay if it were |
|
314 |
right and we needlessly hashed in the description and the |
|
315 |
parameter array. */ |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1247
diff
changeset
|
316 |
@Override |
2 | 317 |
public int hashCode() { |
19852 | 318 |
return Objects.hash(getName(), getReturnType()); |
2 | 319 |
} |
320 |
||
321 |
private static MBeanParameterInfo[] methodSignature(Method method) { |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1247
diff
changeset
|
322 |
final Class<?>[] classes = method.getParameterTypes(); |
2 | 323 |
final Annotation[][] annots = method.getParameterAnnotations(); |
324 |
return parameters(classes, annots); |
|
325 |
} |
|
326 |
||
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1247
diff
changeset
|
327 |
static MBeanParameterInfo[] parameters(Class<?>[] classes, |
2 | 328 |
Annotation[][] annots) { |
329 |
final MBeanParameterInfo[] params = |
|
330 |
new MBeanParameterInfo[classes.length]; |
|
331 |
assert(classes.length == annots.length); |
|
332 |
||
333 |
for (int i = 0; i < classes.length; i++) { |
|
334 |
Descriptor d = Introspector.descriptorForAnnotations(annots[i]); |
|
4156 | 335 |
final String pn = "p" + (i + 1); |
336 |
params[i] = |
|
337 |
new MBeanParameterInfo(pn, classes[i].getName(), "", d); |
|
2 | 338 |
} |
339 |
||
340 |
return params; |
|
341 |
} |
|
342 |
} |