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