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