author | ewendeli |
Tue, 05 Feb 2013 15:35:35 +0100 | |
changeset 16107 | 110e24f9ad05 |
parent 15537 | cca8fab7313a |
parent 16086 | 0a1053497df1 |
child 18213 | 45c8ed869a1b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1999, 2008, 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 com.sun.jmx.mbeanserver; |
|
27 |
||
28 |
import java.lang.annotation.Annotation; |
|
4328
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
29 |
import java.lang.ref.SoftReference; |
2 | 30 |
import java.lang.reflect.AnnotatedElement; |
31 |
import java.lang.reflect.Constructor; |
|
32 |
import java.lang.reflect.Method; |
|
33 |
import java.lang.reflect.Modifier; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
34 |
import java.lang.reflect.Proxy; |
2 | 35 |
import java.lang.reflect.UndeclaredThrowableException; |
36 |
import java.util.Arrays; |
|
4328
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
37 |
import java.util.Collections; |
2 | 38 |
import java.util.HashMap; |
4328
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
39 |
import java.util.List; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
40 |
import java.util.LinkedList; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
41 |
import java.util.Locale; |
2 | 42 |
import java.util.Map; |
4328
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
43 |
import java.util.WeakHashMap; |
2 | 44 |
|
45 |
import javax.management.Descriptor; |
|
46 |
import javax.management.DescriptorKey; |
|
47 |
import javax.management.DynamicMBean; |
|
48 |
import javax.management.ImmutableDescriptor; |
|
49 |
import javax.management.MBeanInfo; |
|
50 |
import javax.management.NotCompliantMBeanException; |
|
51 |
||
34 | 52 |
import com.sun.jmx.remote.util.EnvHelp; |
53 |
import java.lang.reflect.Array; |
|
54 |
import java.lang.reflect.InvocationTargetException; |
|
55 |
import javax.management.AttributeNotFoundException; |
|
56 |
import javax.management.openmbean.CompositeData; |
|
16086
0a1053497df1
8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents:
5506
diff
changeset
|
57 |
import sun.reflect.misc.MethodUtil; |
0a1053497df1
8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents:
5506
diff
changeset
|
58 |
import sun.reflect.misc.ReflectUtil; |
2 | 59 |
|
60 |
/** |
|
61 |
* This class contains the methods for performing all the tests needed to verify |
|
62 |
* that a class represents a JMX compliant MBean. |
|
63 |
* |
|
64 |
* @since 1.5 |
|
65 |
*/ |
|
66 |
public class Introspector { |
|
67 |
||
4156 | 68 |
|
2 | 69 |
/* |
70 |
* ------------------------------------------ |
|
71 |
* PRIVATE CONSTRUCTORS |
|
72 |
* ------------------------------------------ |
|
73 |
*/ |
|
74 |
||
75 |
// private constructor defined to "hide" the default public constructor |
|
76 |
private Introspector() { |
|
77 |
||
78 |
// ------------------------------ |
|
79 |
// ------------------------------ |
|
80 |
||
81 |
} |
|
82 |
||
83 |
/* |
|
84 |
* ------------------------------------------ |
|
85 |
* PUBLIC METHODS |
|
86 |
* ------------------------------------------ |
|
87 |
*/ |
|
88 |
||
89 |
/** |
|
90 |
* Tell whether a MBean of the given class is a Dynamic MBean. |
|
91 |
* This method does nothing more than returning |
|
92 |
* <pre> |
|
93 |
* javax.management.DynamicMBean.class.isAssignableFrom(c) |
|
94 |
* </pre> |
|
95 |
* This method does not check for any JMX MBean compliance: |
|
96 |
* <ul><li>If <code>true</code> is returned, then instances of |
|
97 |
* <code>c</code> are DynamicMBean.</li> |
|
98 |
* <li>If <code>false</code> is returned, then no further |
|
99 |
* assumption can be made on instances of <code>c</code>. |
|
100 |
* In particular, instances of <code>c</code> may, or may not |
|
101 |
* be JMX standard MBeans.</li> |
|
102 |
* </ul> |
|
103 |
* @param c The class of the MBean under examination. |
|
104 |
* @return <code>true</code> if instances of <code>c</code> are |
|
105 |
* Dynamic MBeans, <code>false</code> otherwise. |
|
106 |
* |
|
107 |
**/ |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
108 |
public static final boolean isDynamic(final Class<?> c) { |
2 | 109 |
// Check if the MBean implements the DynamicMBean interface |
110 |
return javax.management.DynamicMBean.class.isAssignableFrom(c); |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Basic method for testing that a MBean of a given class can be |
|
115 |
* instantiated by the MBean server.<p> |
|
116 |
* This method checks that: |
|
117 |
* <ul><li>The given class is a concrete class.</li> |
|
118 |
* <li>The given class exposes at least one public constructor.</li> |
|
119 |
* </ul> |
|
120 |
* If these conditions are not met, throws a NotCompliantMBeanException. |
|
121 |
* @param c The class of the MBean we want to create. |
|
122 |
* @exception NotCompliantMBeanException if the MBean class makes it |
|
123 |
* impossible to instantiate the MBean from within the |
|
124 |
* MBeanServer. |
|
125 |
* |
|
126 |
**/ |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
127 |
public static void testCreation(Class<?> c) |
2 | 128 |
throws NotCompliantMBeanException { |
129 |
// Check if the class is a concrete class |
|
130 |
final int mods = c.getModifiers(); |
|
131 |
if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) { |
|
132 |
throw new NotCompliantMBeanException("MBean class must be concrete"); |
|
133 |
} |
|
134 |
||
135 |
// Check if the MBean has a public constructor |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
136 |
final Constructor<?>[] consList = c.getConstructors(); |
2 | 137 |
if (consList.length == 0) { |
138 |
throw new NotCompliantMBeanException("MBean class must have public constructor"); |
|
139 |
} |
|
140 |
} |
|
141 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
142 |
public static void checkCompliance(Class<?> mbeanClass) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
143 |
throws NotCompliantMBeanException { |
2 | 144 |
// Is DynamicMBean? |
145 |
// |
|
146 |
if (DynamicMBean.class.isAssignableFrom(mbeanClass)) |
|
147 |
return; |
|
148 |
// Is Standard MBean? |
|
149 |
// |
|
150 |
final Exception mbeanException; |
|
151 |
try { |
|
152 |
getStandardMBeanInterface(mbeanClass); |
|
153 |
return; |
|
154 |
} catch (NotCompliantMBeanException e) { |
|
155 |
mbeanException = e; |
|
156 |
} |
|
157 |
// Is MXBean? |
|
158 |
// |
|
159 |
final Exception mxbeanException; |
|
160 |
try { |
|
161 |
getMXBeanInterface(mbeanClass); |
|
162 |
return; |
|
163 |
} catch (NotCompliantMBeanException e) { |
|
164 |
mxbeanException = e; |
|
165 |
} |
|
166 |
final String msg = |
|
167 |
"MBean class " + mbeanClass.getName() + " does not implement " + |
|
4156 | 168 |
"DynamicMBean, and neither follows the Standard MBean conventions (" + |
169 |
mbeanException.toString() + ") nor the MXBean conventions (" + |
|
170 |
mxbeanException.toString() + ")"; |
|
2 | 171 |
throw new NotCompliantMBeanException(msg); |
172 |
} |
|
173 |
||
687 | 174 |
public static <T> DynamicMBean makeDynamicMBean(T mbean) |
4156 | 175 |
throws NotCompliantMBeanException { |
2 | 176 |
if (mbean instanceof DynamicMBean) |
177 |
return (DynamicMBean) mbean; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
178 |
final Class<?> mbeanClass = mbean.getClass(); |
687 | 179 |
Class<? super T> c = null; |
2 | 180 |
try { |
687 | 181 |
c = Util.cast(getStandardMBeanInterface(mbeanClass)); |
2 | 182 |
} catch (NotCompliantMBeanException e) { |
183 |
// Ignore exception - we need to check whether |
|
184 |
// mbean is an MXBean first. |
|
185 |
} |
|
186 |
if (c != null) |
|
687 | 187 |
return new StandardMBeanSupport(mbean, c); |
2 | 188 |
|
189 |
try { |
|
687 | 190 |
c = Util.cast(getMXBeanInterface(mbeanClass)); |
2 | 191 |
} catch (NotCompliantMBeanException e) { |
192 |
// Ignore exception - we cannot decide whether mbean was supposed |
|
193 |
// to be an MBean or an MXBean. We will call checkCompliance() |
|
194 |
// to generate the appropriate exception. |
|
195 |
} |
|
4156 | 196 |
if (c != null) |
197 |
return new MXBeanSupport(mbean, c); |
|
2 | 198 |
checkCompliance(mbeanClass); |
199 |
throw new NotCompliantMBeanException("Not compliant"); // not reached |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Basic method for testing if a given class is a JMX compliant MBean. |
|
204 |
* |
|
205 |
* @param baseClass The class to be tested |
|
206 |
* |
|
207 |
* @return <code>null</code> if the MBean is a DynamicMBean, |
|
208 |
* the computed {@link javax.management.MBeanInfo} otherwise. |
|
209 |
* @exception NotCompliantMBeanException The specified class is not a |
|
210 |
* JMX compliant MBean |
|
211 |
*/ |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
212 |
public static MBeanInfo testCompliance(Class<?> baseClass) |
2 | 213 |
throws NotCompliantMBeanException { |
214 |
||
215 |
// ------------------------------ |
|
216 |
// ------------------------------ |
|
217 |
||
218 |
// Check if the MBean implements the MBean or the Dynamic |
|
219 |
// MBean interface |
|
220 |
if (isDynamic(baseClass)) |
|
221 |
return null; |
|
222 |
||
223 |
return testCompliance(baseClass, null); |
|
224 |
} |
|
225 |
||
4156 | 226 |
public static void testComplianceMXBeanInterface(Class<?> interfaceClass) |
2 | 227 |
throws NotCompliantMBeanException { |
4156 | 228 |
MXBeanIntrospector.getInstance().getAnalyzer(interfaceClass); |
2 | 229 |
} |
230 |
||
231 |
/** |
|
232 |
* Basic method for testing if a given class is a JMX compliant |
|
233 |
* Standard MBean. This method is only called by the legacy code |
|
234 |
* in com.sun.management.jmx. |
|
235 |
* |
|
236 |
* @param baseClass The class to be tested. |
|
237 |
* |
|
238 |
* @param mbeanInterface the MBean interface that the class implements, |
|
239 |
* or null if the interface must be determined by introspection. |
|
240 |
* |
|
241 |
* @return the computed {@link javax.management.MBeanInfo}. |
|
242 |
* @exception NotCompliantMBeanException The specified class is not a |
|
243 |
* JMX compliant Standard MBean |
|
244 |
*/ |
|
245 |
public static synchronized MBeanInfo |
|
246 |
testCompliance(final Class<?> baseClass, |
|
247 |
Class<?> mbeanInterface) |
|
248 |
throws NotCompliantMBeanException { |
|
249 |
if (mbeanInterface == null) |
|
250 |
mbeanInterface = getStandardMBeanInterface(baseClass); |
|
251 |
MBeanIntrospector<?> introspector = StandardMBeanIntrospector.getInstance(); |
|
252 |
return getClassMBeanInfo(introspector, baseClass, mbeanInterface); |
|
253 |
} |
|
254 |
||
255 |
private static <M> MBeanInfo |
|
256 |
getClassMBeanInfo(MBeanIntrospector<M> introspector, |
|
257 |
Class<?> baseClass, Class<?> mbeanInterface) |
|
258 |
throws NotCompliantMBeanException { |
|
259 |
PerInterface<M> perInterface = introspector.getPerInterface(mbeanInterface); |
|
260 |
return introspector.getClassMBeanInfo(baseClass, perInterface); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Get the MBean interface implemented by a JMX Standard |
|
265 |
* MBean class. This method is only called by the legacy |
|
266 |
* code in "com.sun.management.jmx". |
|
267 |
* |
|
268 |
* @param baseClass The class to be tested. |
|
269 |
* |
|
270 |
* @return The MBean interface implemented by the MBean. |
|
271 |
* Return <code>null</code> if the MBean is a DynamicMBean, |
|
272 |
* or if no MBean interface is found. |
|
273 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
274 |
public static Class<?> getMBeanInterface(Class<?> baseClass) { |
2 | 275 |
// Check if the given class implements the MBean interface |
276 |
// or the Dynamic MBean interface |
|
277 |
if (isDynamic(baseClass)) return null; |
|
278 |
try { |
|
279 |
return getStandardMBeanInterface(baseClass); |
|
280 |
} catch (NotCompliantMBeanException e) { |
|
281 |
return null; |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
/** |
|
286 |
* Get the MBean interface implemented by a JMX Standard MBean class. |
|
287 |
* |
|
288 |
* @param baseClass The class to be tested. |
|
289 |
* |
|
290 |
* @return The MBean interface implemented by the Standard MBean. |
|
291 |
* |
|
292 |
* @throws NotCompliantMBeanException The specified class is |
|
293 |
* not a JMX compliant Standard MBean. |
|
294 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
295 |
public static <T> Class<? super T> getStandardMBeanInterface(Class<T> baseClass) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
296 |
throws NotCompliantMBeanException { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
297 |
Class<? super T> current = baseClass; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
298 |
Class<? super T> mbeanInterface = null; |
2 | 299 |
while (current != null) { |
300 |
mbeanInterface = |
|
301 |
findMBeanInterface(current, current.getName()); |
|
302 |
if (mbeanInterface != null) break; |
|
303 |
current = current.getSuperclass(); |
|
304 |
} |
|
305 |
if (mbeanInterface != null) { |
|
306 |
return mbeanInterface; |
|
307 |
} else { |
|
308 |
final String msg = |
|
309 |
"Class " + baseClass.getName() + |
|
310 |
" is not a JMX compliant Standard MBean"; |
|
311 |
throw new NotCompliantMBeanException(msg); |
|
312 |
} |
|
313 |
} |
|
314 |
||
315 |
/** |
|
316 |
* Get the MXBean interface implemented by a JMX MXBean class. |
|
317 |
* |
|
318 |
* @param baseClass The class to be tested. |
|
319 |
* |
|
320 |
* @return The MXBean interface implemented by the MXBean. |
|
321 |
* |
|
322 |
* @throws NotCompliantMBeanException The specified class is |
|
323 |
* not a JMX compliant MXBean. |
|
324 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
325 |
public static <T> Class<? super T> getMXBeanInterface(Class<T> baseClass) |
2 | 326 |
throws NotCompliantMBeanException { |
327 |
try { |
|
328 |
return MXBeanSupport.findMXBeanInterface(baseClass); |
|
329 |
} catch (Exception e) { |
|
330 |
throw throwException(baseClass,e); |
|
331 |
} |
|
332 |
} |
|
333 |
||
334 |
/* |
|
335 |
* ------------------------------------------ |
|
336 |
* PRIVATE METHODS |
|
337 |
* ------------------------------------------ |
|
338 |
*/ |
|
339 |
||
340 |
||
341 |
/** |
|
342 |
* Try to find the MBean interface corresponding to the class aName |
|
343 |
* - i.e. <i>aName</i>MBean, from within aClass and its superclasses. |
|
344 |
**/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
345 |
private static <T> Class<? super T> findMBeanInterface( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
346 |
Class<T> aClass, String aName) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
347 |
Class<? super T> current = aClass; |
2 | 348 |
while (current != null) { |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
349 |
final Class<?>[] interfaces = current.getInterfaces(); |
2 | 350 |
final int len = interfaces.length; |
351 |
for (int i=0;i<len;i++) { |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
352 |
Class<? super T> inter = Util.cast(interfaces[i]); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
353 |
inter = implementsMBean(inter, aName); |
2 | 354 |
if (inter != null) return inter; |
355 |
} |
|
356 |
current = current.getSuperclass(); |
|
357 |
} |
|
358 |
return null; |
|
359 |
} |
|
360 |
||
4156 | 361 |
public static Descriptor descriptorForElement(final AnnotatedElement elmt) { |
2 | 362 |
if (elmt == null) |
363 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR; |
|
364 |
final Annotation[] annots = elmt.getAnnotations(); |
|
4156 | 365 |
return descriptorForAnnotations(annots); |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
366 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
367 |
|
2 | 368 |
public static Descriptor descriptorForAnnotations(Annotation[] annots) { |
369 |
if (annots.length == 0) |
|
370 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR; |
|
371 |
Map<String, Object> descriptorMap = new HashMap<String, Object>(); |
|
372 |
for (Annotation a : annots) { |
|
4156 | 373 |
Class<? extends Annotation> c = a.annotationType(); |
374 |
Method[] elements = c.getMethods(); |
|
375 |
for (Method element : elements) { |
|
376 |
DescriptorKey key = element.getAnnotation(DescriptorKey.class); |
|
377 |
if (key != null) { |
|
378 |
String name = key.value(); |
|
379 |
Object value; |
|
380 |
try { |
|
381 |
value = element.invoke(a); |
|
382 |
} catch (RuntimeException e) { |
|
383 |
// we don't expect this - except for possibly |
|
384 |
// security exceptions? |
|
385 |
// RuntimeExceptions shouldn't be "UndeclaredThrowable". |
|
386 |
// anyway... |
|
387 |
// |
|
388 |
throw e; |
|
389 |
} catch (Exception e) { |
|
390 |
// we don't expect this |
|
391 |
throw new UndeclaredThrowableException(e); |
|
392 |
} |
|
393 |
value = annotationToField(value); |
|
394 |
Object oldValue = descriptorMap.put(name, value); |
|
395 |
if (oldValue != null && !equals(oldValue, value)) { |
|
396 |
final String msg = |
|
397 |
"Inconsistent values for descriptor field " + name + |
|
398 |
" from annotations: " + value + " :: " + oldValue; |
|
399 |
throw new IllegalArgumentException(msg); |
|
400 |
} |
|
401 |
} |
|
402 |
} |
|
2 | 403 |
} |
404 |
||
405 |
if (descriptorMap.isEmpty()) |
|
406 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR; |
|
407 |
else |
|
408 |
return new ImmutableDescriptor(descriptorMap); |
|
409 |
} |
|
410 |
||
1699
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
411 |
/** |
2 | 412 |
* Throws a NotCompliantMBeanException or a SecurityException. |
413 |
* @param notCompliant the class which was under examination |
|
414 |
* @param cause the raeson why NotCompliantMBeanException should |
|
415 |
* be thrown. |
|
416 |
* @return nothing - this method always throw an exception. |
|
417 |
* The return type makes it possible to write |
|
418 |
* <pre> throw throwException(clazz,cause); </pre> |
|
419 |
* @throws SecurityException - if cause is a SecurityException |
|
420 |
* @throws NotCompliantMBeanException otherwise. |
|
421 |
**/ |
|
422 |
static NotCompliantMBeanException throwException(Class<?> notCompliant, |
|
423 |
Throwable cause) |
|
424 |
throws NotCompliantMBeanException, SecurityException { |
|
425 |
if (cause instanceof SecurityException) |
|
426 |
throw (SecurityException) cause; |
|
427 |
if (cause instanceof NotCompliantMBeanException) |
|
428 |
throw (NotCompliantMBeanException)cause; |
|
429 |
final String classname = |
|
430 |
(notCompliant==null)?"null class":notCompliant.getName(); |
|
431 |
final String reason = |
|
432 |
(cause==null)?"Not compliant":cause.getMessage(); |
|
433 |
final NotCompliantMBeanException res = |
|
434 |
new NotCompliantMBeanException(classname+": "+reason); |
|
435 |
res.initCause(cause); |
|
436 |
throw res; |
|
437 |
} |
|
438 |
||
439 |
// Convert a value from an annotation element to a descriptor field value |
|
440 |
// E.g. with @interface Foo {class value()} an annotation @Foo(String.class) |
|
441 |
// will produce a Descriptor field value "java.lang.String" |
|
442 |
private static Object annotationToField(Object x) { |
|
443 |
// An annotation element cannot have a null value but never mind |
|
444 |
if (x == null) |
|
445 |
return null; |
|
446 |
if (x instanceof Number || x instanceof String || |
|
447 |
x instanceof Character || x instanceof Boolean || |
|
448 |
x instanceof String[]) |
|
449 |
return x; |
|
450 |
// Remaining possibilities: array of primitive (e.g. int[]), |
|
451 |
// enum, class, array of enum or class. |
|
452 |
Class<?> c = x.getClass(); |
|
453 |
if (c.isArray()) { |
|
454 |
if (c.getComponentType().isPrimitive()) |
|
455 |
return x; |
|
456 |
Object[] xx = (Object[]) x; |
|
457 |
String[] ss = new String[xx.length]; |
|
458 |
for (int i = 0; i < xx.length; i++) |
|
459 |
ss[i] = (String) annotationToField(xx[i]); |
|
460 |
return ss; |
|
461 |
} |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
462 |
if (x instanceof Class<?>) |
2 | 463 |
return ((Class<?>) x).getName(); |
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
464 |
if (x instanceof Enum<?>) |
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
465 |
return ((Enum<?>) x).name(); |
2 | 466 |
// The only other possibility is that the value is another |
467 |
// annotation, or that the language has evolved since this code |
|
468 |
// was written. We don't allow for either of those currently. |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
469 |
// If it is indeed another annotation, then x will be a proxy |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
470 |
// with an unhelpful name like $Proxy2. So we extract the |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
471 |
// proxy's interface to use that in the exception message. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
472 |
if (Proxy.isProxyClass(c)) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
473 |
c = c.getInterfaces()[0]; // array "can't be empty" |
2 | 474 |
throw new IllegalArgumentException("Illegal type for annotation " + |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
475 |
"element using @DescriptorKey: " + c.getName()); |
2 | 476 |
} |
477 |
||
478 |
// This must be consistent with the check for duplicate field values in |
|
479 |
// ImmutableDescriptor.union. But we don't expect to be called very |
|
480 |
// often so this inefficient check should be enough. |
|
481 |
private static boolean equals(Object x, Object y) { |
|
482 |
return Arrays.deepEquals(new Object[] {x}, new Object[] {y}); |
|
483 |
} |
|
484 |
||
485 |
/** |
|
486 |
* Returns the XXMBean interface or null if no such interface exists |
|
487 |
* |
|
488 |
* @param c The interface to be tested |
|
489 |
* @param clName The name of the class implementing this interface |
|
490 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
491 |
private static <T> Class<? super T> implementsMBean(Class<T> c, String clName) { |
2 | 492 |
String clMBeanName = clName + "MBean"; |
493 |
if (c.getName().equals(clMBeanName)) { |
|
494 |
return c; |
|
495 |
} |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
496 |
Class<?>[] interfaces = c.getInterfaces(); |
2 | 497 |
for (int i = 0;i < interfaces.length; i++) { |
498 |
if (interfaces[i].getName().equals(clMBeanName)) |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
499 |
return Util.cast(interfaces[i]); |
2 | 500 |
} |
501 |
||
502 |
return null; |
|
503 |
} |
|
34 | 504 |
|
505 |
public static Object elementFromComplex(Object complex, String element) |
|
506 |
throws AttributeNotFoundException { |
|
507 |
try { |
|
508 |
if (complex.getClass().isArray() && element.equals("length")) { |
|
509 |
return Array.getLength(complex); |
|
510 |
} else if (complex instanceof CompositeData) { |
|
511 |
return ((CompositeData) complex).get(element); |
|
512 |
} else { |
|
513 |
// Java Beans introspection |
|
514 |
// |
|
4328
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
515 |
Class<?> clazz = complex.getClass(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
516 |
Method readMethod = null; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
517 |
if (BeansHelper.isAvailable()) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
518 |
Object bi = BeansHelper.getBeanInfo(clazz); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
519 |
Object[] pds = BeansHelper.getPropertyDescriptors(bi); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
520 |
for (Object pd: pds) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
521 |
if (BeansHelper.getPropertyName(pd).equals(element)) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
522 |
readMethod = BeansHelper.getReadMethod(pd); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
523 |
break; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
524 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
525 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
526 |
} else { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
527 |
// Java Beans not available so use simple introspection |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
528 |
// to locate method |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
529 |
readMethod = SimpleIntrospector.getReadMethod(clazz, element); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
530 |
} |
16086
0a1053497df1
8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents:
5506
diff
changeset
|
531 |
if (readMethod != null) { |
0a1053497df1
8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents:
5506
diff
changeset
|
532 |
ReflectUtil.checkPackageAccess(readMethod.getDeclaringClass()); |
0a1053497df1
8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents:
5506
diff
changeset
|
533 |
return MethodUtil.invoke(readMethod, complex, new Class[0]); |
0a1053497df1
8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents:
5506
diff
changeset
|
534 |
} |
4328
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
535 |
|
34 | 536 |
throw new AttributeNotFoundException( |
537 |
"Could not find the getter method for the property " + |
|
538 |
element + " using the Java Beans introspector"); |
|
539 |
} |
|
540 |
} catch (InvocationTargetException e) { |
|
541 |
throw new IllegalArgumentException(e); |
|
542 |
} catch (AttributeNotFoundException e) { |
|
543 |
throw e; |
|
544 |
} catch (Exception e) { |
|
545 |
throw EnvHelp.initCause( |
|
546 |
new AttributeNotFoundException(e.getMessage()), e); |
|
547 |
} |
|
548 |
} |
|
4328
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
549 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
550 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
551 |
* A simple introspector that uses reflection to analyze a class and |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
552 |
* identify its "getter" methods. This class is intended for use only when |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
553 |
* Java Beans is not present (which implies that there isn't explicit |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
554 |
* information about the bean available). |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
555 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
556 |
private static class SimpleIntrospector { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
557 |
private SimpleIntrospector() { } |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
558 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
559 |
private static final String GET_METHOD_PREFIX = "get"; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
560 |
private static final String IS_METHOD_PREFIX = "is"; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
561 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
562 |
// cache to avoid repeated lookups |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
563 |
private static final Map<Class<?>,SoftReference<List<Method>>> cache = |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
564 |
Collections.synchronizedMap( |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
565 |
new WeakHashMap<Class<?>,SoftReference<List<Method>>> ()); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
566 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
567 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
568 |
* Returns the list of methods cached for the given class, or {@code null} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
569 |
* if not cached. |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
570 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
571 |
private static List<Method> getCachedMethods(Class<?> clazz) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
572 |
// return cached methods if possible |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
573 |
SoftReference<List<Method>> ref = cache.get(clazz); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
574 |
if (ref != null) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
575 |
List<Method> cached = ref.get(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
576 |
if (cached != null) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
577 |
return cached; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
578 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
579 |
return null; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
580 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
581 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
582 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
583 |
* Returns {@code true} if the given method is a "getter" method (where |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
584 |
* "getter" method is a public method of the form getXXX or "boolean |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
585 |
* isXXX") |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
586 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
587 |
static boolean isReadMethod(Method method) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
588 |
// ignore static methods |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
589 |
int modifiers = method.getModifiers(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
590 |
if (Modifier.isStatic(modifiers)) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
591 |
return false; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
592 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
593 |
String name = method.getName(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
594 |
Class<?>[] paramTypes = method.getParameterTypes(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
595 |
int paramCount = paramTypes.length; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
596 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
597 |
if (paramCount == 0 && name.length() > 2) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
598 |
// boolean isXXX() |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
599 |
if (name.startsWith(IS_METHOD_PREFIX)) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
600 |
return (method.getReturnType() == boolean.class); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
601 |
// getXXX() |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
602 |
if (name.length() > 3 && name.startsWith(GET_METHOD_PREFIX)) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
603 |
return (method.getReturnType() != void.class); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
604 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
605 |
return false; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
606 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
607 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
608 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
609 |
* Returns the list of "getter" methods for the given class. The list |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
610 |
* is ordered so that isXXX methods appear before getXXX methods - this |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
611 |
* is for compatability with the JavaBeans Introspector. |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
612 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
613 |
static List<Method> getReadMethods(Class<?> clazz) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
614 |
// return cached result if available |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
615 |
List<Method> cachedResult = getCachedMethods(clazz); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
616 |
if (cachedResult != null) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
617 |
return cachedResult; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
618 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
619 |
// get list of public methods, filtering out methods that have |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
620 |
// been overridden to return a more specific type. |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
621 |
List<Method> methods = |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
622 |
StandardMBeanIntrospector.getInstance().getMethods(clazz); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
623 |
methods = MBeanAnalyzer.eliminateCovariantMethods(methods); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
624 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
625 |
// filter out the non-getter methods |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
626 |
List<Method> result = new LinkedList<Method>(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
627 |
for (Method m: methods) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
628 |
if (isReadMethod(m)) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
629 |
// favor isXXX over getXXX |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
630 |
if (m.getName().startsWith(IS_METHOD_PREFIX)) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
631 |
result.add(0, m); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
632 |
} else { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
633 |
result.add(m); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
634 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
635 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
636 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
637 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
638 |
// add result to cache |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
639 |
cache.put(clazz, new SoftReference<List<Method>>(result)); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
640 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
641 |
return result; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
642 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
643 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
644 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
645 |
* Returns the "getter" to read the given property from the given class or |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
646 |
* {@code null} if no method is found. |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
647 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
648 |
static Method getReadMethod(Class<?> clazz, String property) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
649 |
// first character in uppercase (compatability with JavaBeans) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
650 |
property = property.substring(0, 1).toUpperCase(Locale.ENGLISH) + |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
651 |
property.substring(1); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
652 |
String getMethod = GET_METHOD_PREFIX + property; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
653 |
String isMethod = IS_METHOD_PREFIX + property; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
654 |
for (Method m: getReadMethods(clazz)) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
655 |
String name = m.getName(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
656 |
if (name.equals(isMethod) || name.equals(getMethod)) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
657 |
return m; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
658 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
659 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
660 |
return null; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
661 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
662 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
663 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
664 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
665 |
* A class that provides access to the JavaBeans Introspector and |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
666 |
* PropertyDescriptors without creating a static dependency on java.beans. |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
667 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
668 |
private static class BeansHelper { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
669 |
private static final Class<?> introspectorClass = |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
670 |
getClass("java.beans.Introspector"); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
671 |
private static final Class<?> beanInfoClass = |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
672 |
(introspectorClass == null) ? null : getClass("java.beans.BeanInfo"); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
673 |
private static final Class<?> getPropertyDescriptorClass = |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
674 |
(beanInfoClass == null) ? null : getClass("java.beans.PropertyDescriptor"); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
675 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
676 |
private static final Method getBeanInfo = |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
677 |
getMethod(introspectorClass, "getBeanInfo", Class.class); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
678 |
private static final Method getPropertyDescriptors = |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
679 |
getMethod(beanInfoClass, "getPropertyDescriptors"); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
680 |
private static final Method getPropertyName = |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
681 |
getMethod(getPropertyDescriptorClass, "getName"); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
682 |
private static final Method getReadMethod = |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
683 |
getMethod(getPropertyDescriptorClass, "getReadMethod"); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
684 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
685 |
private static Class<?> getClass(String name) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
686 |
try { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
687 |
return Class.forName(name, true, null); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
688 |
} catch (ClassNotFoundException e) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
689 |
return null; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
690 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
691 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
692 |
private static Method getMethod(Class<?> clazz, |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
693 |
String name, |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
694 |
Class<?>... paramTypes) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
695 |
{ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
696 |
if (clazz != null) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
697 |
try { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
698 |
return clazz.getMethod(name, paramTypes); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
699 |
} catch (NoSuchMethodException e) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
700 |
throw new AssertionError(e); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
701 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
702 |
} else { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
703 |
return null; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
704 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
705 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
706 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
707 |
private BeansHelper() { } |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
708 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
709 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
710 |
* Returns {@code true} if java.beans is available. |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
711 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
712 |
static boolean isAvailable() { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
713 |
return introspectorClass != null; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
714 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
715 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
716 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
717 |
* Invokes java.beans.Introspector.getBeanInfo(Class) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
718 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
719 |
static Object getBeanInfo(Class<?> clazz) throws Exception { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
720 |
try { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
721 |
return getBeanInfo.invoke(null, clazz); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
722 |
} catch (InvocationTargetException e) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
723 |
Throwable cause = e.getCause(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
724 |
if (cause instanceof Exception) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
725 |
throw (Exception)cause; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
726 |
throw new AssertionError(e); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
727 |
} catch (IllegalAccessException iae) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
728 |
throw new AssertionError(iae); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
729 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
730 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
731 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
732 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
733 |
* Invokes java.beans.BeanInfo.getPropertyDescriptors() |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
734 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
735 |
static Object[] getPropertyDescriptors(Object bi) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
736 |
try { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
737 |
return (Object[])getPropertyDescriptors.invoke(bi); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
738 |
} catch (InvocationTargetException e) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
739 |
Throwable cause = e.getCause(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
740 |
if (cause instanceof RuntimeException) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
741 |
throw (RuntimeException)cause; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
742 |
throw new AssertionError(e); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
743 |
} catch (IllegalAccessException iae) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
744 |
throw new AssertionError(iae); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
745 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
746 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
747 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
748 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
749 |
* Invokes java.beans.PropertyDescriptor.getName() |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
750 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
751 |
static String getPropertyName(Object pd) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
752 |
try { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
753 |
return (String)getPropertyName.invoke(pd); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
754 |
} catch (InvocationTargetException e) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
755 |
Throwable cause = e.getCause(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
756 |
if (cause instanceof RuntimeException) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
757 |
throw (RuntimeException)cause; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
758 |
throw new AssertionError(e); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
759 |
} catch (IllegalAccessException iae) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
760 |
throw new AssertionError(iae); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
761 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
762 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
763 |
|
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
764 |
/** |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
765 |
* Invokes java.beans.PropertyDescriptor.getReadMethod() |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
766 |
*/ |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
767 |
static Method getReadMethod(Object pd) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
768 |
try { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
769 |
return (Method)getReadMethod.invoke(pd); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
770 |
} catch (InvocationTargetException e) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
771 |
Throwable cause = e.getCause(); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
772 |
if (cause instanceof RuntimeException) |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
773 |
throw (RuntimeException)cause; |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
774 |
throw new AssertionError(e); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
775 |
} catch (IllegalAccessException iae) { |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
776 |
throw new AssertionError(iae); |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
777 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
778 |
} |
9591511c1d88
6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents:
4156
diff
changeset
|
779 |
} |
2 | 780 |
} |