author | emcmanus |
Wed, 24 Sep 2008 15:19:07 +0200 | |
changeset 1322 | d038148778cc |
parent 834 | dc74d4ddc28e |
child 1510 | e747d3193ef2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.jmx.mbeanserver; |
|
27 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
28 |
import com.sun.jmx.remote.util.EnvHelp; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
29 |
import java.beans.BeanInfo; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
30 |
import java.beans.PropertyDescriptor; |
2 | 31 |
import java.lang.annotation.Annotation; |
32 |
import java.lang.reflect.AnnotatedElement; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
33 |
import java.lang.reflect.Array; |
2 | 34 |
import java.lang.reflect.Constructor; |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
35 |
import java.lang.reflect.InvocationTargetException; |
2 | 36 |
import java.lang.reflect.Method; |
37 |
import java.lang.reflect.Modifier; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
38 |
import java.lang.reflect.Proxy; |
2 | 39 |
import java.lang.reflect.UndeclaredThrowableException; |
40 |
import java.util.Arrays; |
|
41 |
import java.util.HashMap; |
|
42 |
import java.util.Map; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
43 |
import java.util.logging.Level; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
44 |
import javax.management.AttributeNotFoundException; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
45 |
import javax.management.Description; |
2 | 46 |
|
47 |
import javax.management.Descriptor; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
48 |
import javax.management.DescriptorFields; |
2 | 49 |
import javax.management.DescriptorKey; |
50 |
import javax.management.DynamicMBean; |
|
51 |
import javax.management.ImmutableDescriptor; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
52 |
import javax.management.MBean; |
2 | 53 |
import javax.management.MBeanInfo; |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
54 |
import javax.management.MXBean; |
2 | 55 |
import javax.management.NotCompliantMBeanException; |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
56 |
import javax.management.openmbean.CompositeData; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
57 |
import javax.management.openmbean.MXBeanMappingFactory; |
2 | 58 |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
59 |
import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
60 |
import com.sun.jmx.mbeanserver.Util; |
34 | 61 |
import com.sun.jmx.remote.util.EnvHelp; |
62 |
import java.beans.BeanInfo; |
|
63 |
import java.beans.PropertyDescriptor; |
|
64 |
import java.lang.reflect.Array; |
|
65 |
import java.lang.reflect.InvocationTargetException; |
|
66 |
import javax.management.AttributeNotFoundException; |
|
67 |
import javax.management.openmbean.CompositeData; |
|
687 | 68 |
import javax.management.openmbean.MXBeanMappingFactory; |
2 | 69 |
|
70 |
/** |
|
71 |
* This class contains the methods for performing all the tests needed to verify |
|
72 |
* that a class represents a JMX compliant MBean. |
|
73 |
* |
|
74 |
* @since 1.5 |
|
75 |
*/ |
|
76 |
public class Introspector { |
|
77 |
||
78 |
||
79 |
/* |
|
80 |
* ------------------------------------------ |
|
81 |
* PRIVATE CONSTRUCTORS |
|
82 |
* ------------------------------------------ |
|
83 |
*/ |
|
84 |
||
85 |
// private constructor defined to "hide" the default public constructor |
|
86 |
private Introspector() { |
|
87 |
||
88 |
// ------------------------------ |
|
89 |
// ------------------------------ |
|
90 |
||
91 |
} |
|
92 |
||
93 |
/* |
|
94 |
* ------------------------------------------ |
|
95 |
* PUBLIC METHODS |
|
96 |
* ------------------------------------------ |
|
97 |
*/ |
|
98 |
||
99 |
/** |
|
100 |
* Tell whether a MBean of the given class is a Dynamic MBean. |
|
101 |
* This method does nothing more than returning |
|
102 |
* <pre> |
|
103 |
* javax.management.DynamicMBean.class.isAssignableFrom(c) |
|
104 |
* </pre> |
|
105 |
* This method does not check for any JMX MBean compliance: |
|
106 |
* <ul><li>If <code>true</code> is returned, then instances of |
|
107 |
* <code>c</code> are DynamicMBean.</li> |
|
108 |
* <li>If <code>false</code> is returned, then no further |
|
109 |
* assumption can be made on instances of <code>c</code>. |
|
110 |
* In particular, instances of <code>c</code> may, or may not |
|
111 |
* be JMX standard MBeans.</li> |
|
112 |
* </ul> |
|
113 |
* @param c The class of the MBean under examination. |
|
114 |
* @return <code>true</code> if instances of <code>c</code> are |
|
115 |
* Dynamic MBeans, <code>false</code> otherwise. |
|
116 |
* |
|
117 |
**/ |
|
118 |
public static final boolean isDynamic(final Class c) { |
|
119 |
// Check if the MBean implements the DynamicMBean interface |
|
120 |
return javax.management.DynamicMBean.class.isAssignableFrom(c); |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* Basic method for testing that a MBean of a given class can be |
|
125 |
* instantiated by the MBean server.<p> |
|
126 |
* This method checks that: |
|
127 |
* <ul><li>The given class is a concrete class.</li> |
|
128 |
* <li>The given class exposes at least one public constructor.</li> |
|
129 |
* </ul> |
|
130 |
* If these conditions are not met, throws a NotCompliantMBeanException. |
|
131 |
* @param c The class of the MBean we want to create. |
|
132 |
* @exception NotCompliantMBeanException if the MBean class makes it |
|
133 |
* impossible to instantiate the MBean from within the |
|
134 |
* MBeanServer. |
|
135 |
* |
|
136 |
**/ |
|
137 |
public static void testCreation(Class c) |
|
138 |
throws NotCompliantMBeanException { |
|
139 |
// Check if the class is a concrete class |
|
140 |
final int mods = c.getModifiers(); |
|
141 |
if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) { |
|
142 |
throw new NotCompliantMBeanException("MBean class must be concrete"); |
|
143 |
} |
|
144 |
||
145 |
// Check if the MBean has a public constructor |
|
146 |
final Constructor[] consList = c.getConstructors(); |
|
147 |
if (consList.length == 0) { |
|
148 |
throw new NotCompliantMBeanException("MBean class must have public constructor"); |
|
149 |
} |
|
150 |
} |
|
151 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
152 |
public static void checkCompliance(Class<?> mbeanClass) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
153 |
throws NotCompliantMBeanException { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
154 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
155 |
// Check that @Resource is used correctly (if it used). |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
156 |
MBeanInjector.validate(mbeanClass); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
157 |
|
2 | 158 |
// Is DynamicMBean? |
159 |
// |
|
160 |
if (DynamicMBean.class.isAssignableFrom(mbeanClass)) |
|
161 |
return; |
|
162 |
// Is Standard MBean? |
|
163 |
// |
|
164 |
final Exception mbeanException; |
|
165 |
try { |
|
166 |
getStandardMBeanInterface(mbeanClass); |
|
167 |
return; |
|
168 |
} catch (NotCompliantMBeanException e) { |
|
169 |
mbeanException = e; |
|
170 |
} |
|
171 |
// Is MXBean? |
|
172 |
// |
|
173 |
final Exception mxbeanException; |
|
174 |
try { |
|
175 |
getMXBeanInterface(mbeanClass); |
|
176 |
return; |
|
177 |
} catch (NotCompliantMBeanException e) { |
|
178 |
mxbeanException = e; |
|
179 |
} |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
180 |
// Is @MBean or @MXBean class? |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
181 |
// In fact we find @MBean or @MXBean as a hacky variant of |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
182 |
// getStandardMBeanInterface or getMXBeanInterface. If we get here |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
183 |
// then nothing worked. |
2 | 184 |
final String msg = |
185 |
"MBean class " + mbeanClass.getName() + " does not implement " + |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
186 |
"DynamicMBean; does not follow the Standard MBean conventions (" + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
187 |
mbeanException.toString() + "); does not follow the MXBean conventions (" + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
188 |
mxbeanException.toString() + "); and does not have or inherit the @" + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
189 |
MBean.class.getSimpleName() + " or @" + MXBean.class.getSimpleName() + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
190 |
" annotation"; |
2 | 191 |
throw new NotCompliantMBeanException(msg); |
192 |
} |
|
193 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
194 |
/** |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
195 |
* <p>Make a DynamicMBean out of the existing MBean object. The object |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
196 |
* may already be a DynamicMBean, or it may be a Standard MBean or |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
197 |
* MXBean, possibly defined using {@code @MBean} or {@code @MXBean}.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
198 |
* @param mbean the object to convert to a DynamicMBean. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
199 |
* @param <T> a type parameter defined for implementation convenience |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
200 |
* (which would have to be removed if this method were part of the public |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
201 |
* API). |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
202 |
* @return the converted DynamicMBean. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
203 |
* @throws NotCompliantMBeanException if {@code mbean} is not a compliant |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
204 |
* MBean object, including the case where it is null. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
205 |
*/ |
687 | 206 |
public static <T> DynamicMBean makeDynamicMBean(T mbean) |
207 |
throws NotCompliantMBeanException { |
|
208 |
if (mbean == null) |
|
209 |
throw new NotCompliantMBeanException("Null MBean object"); |
|
2 | 210 |
if (mbean instanceof DynamicMBean) |
211 |
return (DynamicMBean) mbean; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
212 |
final Class<?> mbeanClass = mbean.getClass(); |
687 | 213 |
Class<? super T> c = null; |
2 | 214 |
try { |
687 | 215 |
c = Util.cast(getStandardMBeanInterface(mbeanClass)); |
2 | 216 |
} catch (NotCompliantMBeanException e) { |
217 |
// Ignore exception - we need to check whether |
|
218 |
// mbean is an MXBean first. |
|
219 |
} |
|
220 |
if (c != null) |
|
687 | 221 |
return new StandardMBeanSupport(mbean, c); |
2 | 222 |
|
223 |
try { |
|
687 | 224 |
c = Util.cast(getMXBeanInterface(mbeanClass)); |
2 | 225 |
} catch (NotCompliantMBeanException e) { |
226 |
// Ignore exception - we cannot decide whether mbean was supposed |
|
227 |
// to be an MBean or an MXBean. We will call checkCompliance() |
|
228 |
// to generate the appropriate exception. |
|
229 |
} |
|
687 | 230 |
if (c != null) { |
1322
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
231 |
MXBeanMappingFactory factory; |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
232 |
try { |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
233 |
factory = MXBeanMappingFactory.forInterface(c); |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
234 |
} catch (IllegalArgumentException e) { |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
235 |
NotCompliantMBeanException ncmbe = |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
236 |
new NotCompliantMBeanException(e.getMessage()); |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
237 |
ncmbe.initCause(e); |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
238 |
throw ncmbe; |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
239 |
} |
687 | 240 |
return new MXBeanSupport(mbean, c, factory); |
241 |
} |
|
2 | 242 |
checkCompliance(mbeanClass); |
243 |
throw new NotCompliantMBeanException("Not compliant"); // not reached |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* Basic method for testing if a given class is a JMX compliant MBean. |
|
248 |
* |
|
249 |
* @param baseClass The class to be tested |
|
250 |
* |
|
251 |
* @return <code>null</code> if the MBean is a DynamicMBean, |
|
252 |
* the computed {@link javax.management.MBeanInfo} otherwise. |
|
253 |
* @exception NotCompliantMBeanException The specified class is not a |
|
254 |
* JMX compliant MBean |
|
255 |
*/ |
|
256 |
public static MBeanInfo testCompliance(Class baseClass) |
|
257 |
throws NotCompliantMBeanException { |
|
258 |
||
259 |
// ------------------------------ |
|
260 |
// ------------------------------ |
|
261 |
||
262 |
// Check if the MBean implements the MBean or the Dynamic |
|
263 |
// MBean interface |
|
264 |
if (isDynamic(baseClass)) |
|
265 |
return null; |
|
266 |
||
267 |
return testCompliance(baseClass, null); |
|
268 |
} |
|
269 |
||
687 | 270 |
public static void testComplianceMXBeanInterface(Class interfaceClass, |
271 |
MXBeanMappingFactory factory) |
|
2 | 272 |
throws NotCompliantMBeanException { |
687 | 273 |
MXBeanIntrospector.getInstance(factory).getAnalyzer(interfaceClass); |
2 | 274 |
} |
275 |
||
276 |
/** |
|
277 |
* Basic method for testing if a given class is a JMX compliant |
|
278 |
* Standard MBean. This method is only called by the legacy code |
|
279 |
* in com.sun.management.jmx. |
|
280 |
* |
|
281 |
* @param baseClass The class to be tested. |
|
282 |
* |
|
283 |
* @param mbeanInterface the MBean interface that the class implements, |
|
284 |
* or null if the interface must be determined by introspection. |
|
285 |
* |
|
286 |
* @return the computed {@link javax.management.MBeanInfo}. |
|
287 |
* @exception NotCompliantMBeanException The specified class is not a |
|
288 |
* JMX compliant Standard MBean |
|
289 |
*/ |
|
290 |
public static synchronized MBeanInfo |
|
291 |
testCompliance(final Class<?> baseClass, |
|
292 |
Class<?> mbeanInterface) |
|
293 |
throws NotCompliantMBeanException { |
|
294 |
if (mbeanInterface == null) |
|
295 |
mbeanInterface = getStandardMBeanInterface(baseClass); |
|
296 |
MBeanIntrospector<?> introspector = StandardMBeanIntrospector.getInstance(); |
|
297 |
return getClassMBeanInfo(introspector, baseClass, mbeanInterface); |
|
298 |
} |
|
299 |
||
300 |
private static <M> MBeanInfo |
|
301 |
getClassMBeanInfo(MBeanIntrospector<M> introspector, |
|
302 |
Class<?> baseClass, Class<?> mbeanInterface) |
|
303 |
throws NotCompliantMBeanException { |
|
304 |
PerInterface<M> perInterface = introspector.getPerInterface(mbeanInterface); |
|
305 |
return introspector.getClassMBeanInfo(baseClass, perInterface); |
|
306 |
} |
|
307 |
||
308 |
/** |
|
309 |
* Get the MBean interface implemented by a JMX Standard |
|
310 |
* MBean class. This method is only called by the legacy |
|
311 |
* code in "com.sun.management.jmx". |
|
312 |
* |
|
313 |
* @param baseClass The class to be tested. |
|
314 |
* |
|
315 |
* @return The MBean interface implemented by the MBean. |
|
316 |
* Return <code>null</code> if the MBean is a DynamicMBean, |
|
317 |
* or if no MBean interface is found. |
|
318 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
319 |
public static Class<?> getMBeanInterface(Class<?> baseClass) { |
2 | 320 |
// Check if the given class implements the MBean interface |
321 |
// or the Dynamic MBean interface |
|
322 |
if (isDynamic(baseClass)) return null; |
|
323 |
try { |
|
324 |
return getStandardMBeanInterface(baseClass); |
|
325 |
} catch (NotCompliantMBeanException e) { |
|
326 |
return null; |
|
327 |
} |
|
328 |
} |
|
329 |
||
330 |
/** |
|
331 |
* Get the MBean interface implemented by a JMX Standard MBean class. |
|
332 |
* |
|
333 |
* @param baseClass The class to be tested. |
|
334 |
* |
|
335 |
* @return The MBean interface implemented by the Standard MBean. |
|
336 |
* |
|
337 |
* @throws NotCompliantMBeanException The specified class is |
|
338 |
* not a JMX compliant Standard MBean. |
|
339 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
340 |
public static <T> Class<? super T> getStandardMBeanInterface(Class<T> baseClass) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
341 |
throws NotCompliantMBeanException { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
342 |
if (baseClass.isAnnotationPresent(MBean.class)) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
343 |
return baseClass; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
344 |
Class<? super T> current = baseClass; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
345 |
Class<? super T> mbeanInterface = null; |
2 | 346 |
while (current != null) { |
347 |
mbeanInterface = |
|
348 |
findMBeanInterface(current, current.getName()); |
|
349 |
if (mbeanInterface != null) break; |
|
350 |
current = current.getSuperclass(); |
|
351 |
} |
|
352 |
if (mbeanInterface != null) { |
|
353 |
return mbeanInterface; |
|
354 |
} else { |
|
355 |
final String msg = |
|
356 |
"Class " + baseClass.getName() + |
|
357 |
" is not a JMX compliant Standard MBean"; |
|
358 |
throw new NotCompliantMBeanException(msg); |
|
359 |
} |
|
360 |
} |
|
361 |
||
362 |
/** |
|
363 |
* Get the MXBean interface implemented by a JMX MXBean class. |
|
364 |
* |
|
365 |
* @param baseClass The class to be tested. |
|
366 |
* |
|
367 |
* @return The MXBean interface implemented by the MXBean. |
|
368 |
* |
|
369 |
* @throws NotCompliantMBeanException The specified class is |
|
370 |
* not a JMX compliant MXBean. |
|
371 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
372 |
public static <T> Class<? super T> getMXBeanInterface(Class<T> baseClass) |
2 | 373 |
throws NotCompliantMBeanException { |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
374 |
if (hasMXBeanAnnotation(baseClass)) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
375 |
return baseClass; |
2 | 376 |
try { |
377 |
return MXBeanSupport.findMXBeanInterface(baseClass); |
|
378 |
} catch (Exception e) { |
|
379 |
throw throwException(baseClass,e); |
|
380 |
} |
|
381 |
} |
|
382 |
||
687 | 383 |
public static <T> Class<? super T> getStandardOrMXBeanInterface( |
384 |
Class<T> baseClass, boolean mxbean) |
|
385 |
throws NotCompliantMBeanException { |
|
386 |
if (mxbean) |
|
387 |
return getMXBeanInterface(baseClass); |
|
388 |
else |
|
389 |
return getStandardMBeanInterface(baseClass); |
|
390 |
} |
|
391 |
||
2 | 392 |
/* |
393 |
* ------------------------------------------ |
|
394 |
* PRIVATE METHODS |
|
395 |
* ------------------------------------------ |
|
396 |
*/ |
|
397 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
398 |
static boolean hasMXBeanAnnotation(Class<?> c) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
399 |
MXBean m = c.getAnnotation(MXBean.class); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
400 |
return (m != null && m.value()); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
401 |
} |
2 | 402 |
|
403 |
/** |
|
404 |
* Try to find the MBean interface corresponding to the class aName |
|
405 |
* - i.e. <i>aName</i>MBean, from within aClass and its superclasses. |
|
406 |
**/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
407 |
private static <T> Class<? super T> findMBeanInterface( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
408 |
Class<T> aClass, String aName) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
409 |
Class<? super T> current = aClass; |
2 | 410 |
while (current != null) { |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
411 |
final Class<?>[] interfaces = current.getInterfaces(); |
2 | 412 |
final int len = interfaces.length; |
413 |
for (int i=0;i<len;i++) { |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
414 |
Class<? super T> inter = Util.cast(interfaces[i]); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
415 |
inter = implementsMBean(inter, aName); |
2 | 416 |
if (inter != null) return inter; |
417 |
} |
|
418 |
current = current.getSuperclass(); |
|
419 |
} |
|
420 |
return null; |
|
421 |
} |
|
422 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
423 |
public static String descriptionForElement(AnnotatedElement elmt) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
424 |
if (elmt == null) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
425 |
return null; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
426 |
Description d = elmt.getAnnotation(Description.class); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
427 |
if (d == null) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
428 |
return null; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
429 |
return d.value(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
430 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
431 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
432 |
public static String descriptionForParameter( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
433 |
Annotation[] parameterAnnotations) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
434 |
for (Annotation a : parameterAnnotations) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
435 |
if (a instanceof Description) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
436 |
return ((Description) a).value(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
437 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
438 |
return null; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
439 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
440 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
441 |
public static String nameForParameter( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
442 |
Annotation[] parameterAnnotations) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
443 |
for (Annotation a : parameterAnnotations) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
444 |
Class<? extends Annotation> ac = a.annotationType(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
445 |
// You'd really have to go out of your way to have more than |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
446 |
// one @Name annotation, so we don't check for that. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
447 |
if (ac.getSimpleName().equals("Name")) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
448 |
try { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
449 |
Method value = ac.getMethod("value"); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
450 |
if (value.getReturnType() == String.class && |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
451 |
value.getParameterTypes().length == 0) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
452 |
return (String) value.invoke(a); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
453 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
454 |
} catch (Exception e) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
455 |
MBEANSERVER_LOGGER.log( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
456 |
Level.WARNING, |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
457 |
"Unexpected exception getting @" + ac.getName(), |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
458 |
e); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
459 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
460 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
461 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
462 |
return null; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
463 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
464 |
|
2 | 465 |
public static Descriptor descriptorForElement(final AnnotatedElement elmt) { |
466 |
if (elmt == null) |
|
467 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR; |
|
468 |
final Annotation[] annots = elmt.getAnnotations(); |
|
469 |
return descriptorForAnnotations(annots); |
|
470 |
} |
|
471 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
472 |
public static Descriptor descriptorForAnnotation(Annotation annot) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
473 |
return descriptorForAnnotations(new Annotation[] {annot}); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
474 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
475 |
|
2 | 476 |
public static Descriptor descriptorForAnnotations(Annotation[] annots) { |
477 |
if (annots.length == 0) |
|
478 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR; |
|
479 |
Map<String, Object> descriptorMap = new HashMap<String, Object>(); |
|
480 |
for (Annotation a : annots) { |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
481 |
if (a instanceof DescriptorFields) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
482 |
addDescriptorFieldsToMap(descriptorMap, (DescriptorFields) a); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
483 |
addAnnotationFieldsToMap(descriptorMap, a); |
2 | 484 |
} |
485 |
||
486 |
if (descriptorMap.isEmpty()) |
|
487 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR; |
|
488 |
else |
|
489 |
return new ImmutableDescriptor(descriptorMap); |
|
490 |
} |
|
491 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
492 |
private static void addDescriptorFieldsToMap( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
493 |
Map<String, Object> descriptorMap, DescriptorFields df) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
494 |
for (String field : df.value()) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
495 |
int eq = field.indexOf('='); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
496 |
if (eq < 0) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
497 |
throw new IllegalArgumentException( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
498 |
"@DescriptorFields string must contain '=': " + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
499 |
field); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
500 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
501 |
String name = field.substring(0, eq); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
502 |
String value = field.substring(eq + 1); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
503 |
addToMap(descriptorMap, name, value); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
504 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
505 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
506 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
507 |
private static void addAnnotationFieldsToMap( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
508 |
Map<String, Object> descriptorMap, Annotation a) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
509 |
Class<? extends Annotation> c = a.annotationType(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
510 |
Method[] elements = c.getMethods(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
511 |
for (Method element : elements) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
512 |
DescriptorKey key = element.getAnnotation(DescriptorKey.class); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
513 |
if (key != null) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
514 |
String name = key.value(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
515 |
Object value; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
516 |
try { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
517 |
value = element.invoke(a); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
518 |
} catch (RuntimeException e) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
519 |
// we don't expect this - except for possibly |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
520 |
// security exceptions? |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
521 |
// RuntimeExceptions shouldn't be "UndeclaredThrowable". |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
522 |
// anyway... |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
523 |
throw e; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
524 |
} catch (Exception e) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
525 |
// we don't expect this |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
526 |
throw new UndeclaredThrowableException(e); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
527 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
528 |
if (!key.omitIfDefault() || |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
529 |
!equals(value, element.getDefaultValue())) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
530 |
value = annotationToField(value); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
531 |
addToMap(descriptorMap, name, value); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
532 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
533 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
534 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
535 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
536 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
537 |
private static void addToMap( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
538 |
Map<String, Object> descriptorMap, String name, Object value) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
539 |
Object oldValue = descriptorMap.put(name, value); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
540 |
if (oldValue != null && !equals(oldValue, value)) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
541 |
final String msg = |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
542 |
"Inconsistent values for descriptor field " + name + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
543 |
" from annotations: " + value + " :: " + oldValue; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
544 |
throw new IllegalArgumentException(msg); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
545 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
546 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
547 |
|
2 | 548 |
/** |
549 |
* Throws a NotCompliantMBeanException or a SecurityException. |
|
550 |
* @param notCompliant the class which was under examination |
|
551 |
* @param cause the raeson why NotCompliantMBeanException should |
|
552 |
* be thrown. |
|
553 |
* @return nothing - this method always throw an exception. |
|
554 |
* The return type makes it possible to write |
|
555 |
* <pre> throw throwException(clazz,cause); </pre> |
|
556 |
* @throws SecurityException - if cause is a SecurityException |
|
557 |
* @throws NotCompliantMBeanException otherwise. |
|
558 |
**/ |
|
559 |
static NotCompliantMBeanException throwException(Class<?> notCompliant, |
|
560 |
Throwable cause) |
|
561 |
throws NotCompliantMBeanException, SecurityException { |
|
562 |
if (cause instanceof SecurityException) |
|
563 |
throw (SecurityException) cause; |
|
564 |
if (cause instanceof NotCompliantMBeanException) |
|
565 |
throw (NotCompliantMBeanException)cause; |
|
566 |
final String classname = |
|
567 |
(notCompliant==null)?"null class":notCompliant.getName(); |
|
568 |
final String reason = |
|
569 |
(cause==null)?"Not compliant":cause.getMessage(); |
|
570 |
final NotCompliantMBeanException res = |
|
571 |
new NotCompliantMBeanException(classname+": "+reason); |
|
572 |
res.initCause(cause); |
|
573 |
throw res; |
|
574 |
} |
|
575 |
||
576 |
// Convert a value from an annotation element to a descriptor field value |
|
577 |
// E.g. with @interface Foo {class value()} an annotation @Foo(String.class) |
|
578 |
// will produce a Descriptor field value "java.lang.String" |
|
579 |
private static Object annotationToField(Object x) { |
|
580 |
// An annotation element cannot have a null value but never mind |
|
581 |
if (x == null) |
|
582 |
return null; |
|
583 |
if (x instanceof Number || x instanceof String || |
|
584 |
x instanceof Character || x instanceof Boolean || |
|
585 |
x instanceof String[]) |
|
586 |
return x; |
|
587 |
// Remaining possibilities: array of primitive (e.g. int[]), |
|
588 |
// enum, class, array of enum or class. |
|
589 |
Class<?> c = x.getClass(); |
|
590 |
if (c.isArray()) { |
|
591 |
if (c.getComponentType().isPrimitive()) |
|
592 |
return x; |
|
593 |
Object[] xx = (Object[]) x; |
|
594 |
String[] ss = new String[xx.length]; |
|
595 |
for (int i = 0; i < xx.length; i++) |
|
596 |
ss[i] = (String) annotationToField(xx[i]); |
|
597 |
return ss; |
|
598 |
} |
|
599 |
if (x instanceof Class) |
|
600 |
return ((Class<?>) x).getName(); |
|
601 |
if (x instanceof Enum) |
|
602 |
return ((Enum) x).name(); |
|
603 |
// The only other possibility is that the value is another |
|
604 |
// annotation, or that the language has evolved since this code |
|
605 |
// was written. We don't allow for either of those currently. |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
606 |
// If it is indeed another annotation, then x will be a proxy |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
607 |
// with an unhelpful name like $Proxy2. So we extract the |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
608 |
// proxy's interface to use that in the exception message. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
609 |
if (Proxy.isProxyClass(c)) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
610 |
c = c.getInterfaces()[0]; // array "can't be empty" |
2 | 611 |
throw new IllegalArgumentException("Illegal type for annotation " + |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
612 |
"element using @DescriptorKey: " + c.getName()); |
2 | 613 |
} |
614 |
||
615 |
// This must be consistent with the check for duplicate field values in |
|
616 |
// ImmutableDescriptor.union. But we don't expect to be called very |
|
617 |
// often so this inefficient check should be enough. |
|
618 |
private static boolean equals(Object x, Object y) { |
|
619 |
return Arrays.deepEquals(new Object[] {x}, new Object[] {y}); |
|
620 |
} |
|
621 |
||
622 |
/** |
|
623 |
* Returns the XXMBean interface or null if no such interface exists |
|
624 |
* |
|
625 |
* @param c The interface to be tested |
|
626 |
* @param clName The name of the class implementing this interface |
|
627 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
628 |
private static <T> Class<? super T> implementsMBean(Class<T> c, String clName) { |
2 | 629 |
String clMBeanName = clName + "MBean"; |
630 |
if (c.getName().equals(clMBeanName)) { |
|
631 |
return c; |
|
632 |
} |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
633 |
Class<?>[] interfaces = c.getInterfaces(); |
2 | 634 |
for (int i = 0;i < interfaces.length; i++) { |
635 |
if (interfaces[i].getName().equals(clMBeanName)) |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
636 |
return Util.cast(interfaces[i]); |
2 | 637 |
} |
638 |
||
639 |
return null; |
|
640 |
} |
|
34 | 641 |
|
642 |
public static Object elementFromComplex(Object complex, String element) |
|
643 |
throws AttributeNotFoundException { |
|
644 |
try { |
|
645 |
if (complex.getClass().isArray() && element.equals("length")) { |
|
646 |
return Array.getLength(complex); |
|
647 |
} else if (complex instanceof CompositeData) { |
|
648 |
return ((CompositeData) complex).get(element); |
|
649 |
} else { |
|
650 |
// Java Beans introspection |
|
651 |
// |
|
652 |
BeanInfo bi = java.beans.Introspector.getBeanInfo(complex.getClass()); |
|
653 |
PropertyDescriptor[] pds = bi.getPropertyDescriptors(); |
|
654 |
for (PropertyDescriptor pd : pds) |
|
655 |
if (pd.getName().equals(element)) |
|
656 |
return pd.getReadMethod().invoke(complex); |
|
657 |
throw new AttributeNotFoundException( |
|
658 |
"Could not find the getter method for the property " + |
|
659 |
element + " using the Java Beans introspector"); |
|
660 |
} |
|
661 |
} catch (InvocationTargetException e) { |
|
662 |
throw new IllegalArgumentException(e); |
|
663 |
} catch (AttributeNotFoundException e) { |
|
664 |
throw e; |
|
665 |
} catch (Exception e) { |
|
666 |
throw EnvHelp.initCause( |
|
667 |
new AttributeNotFoundException(e.getMessage()), e); |
|
668 |
} |
|
669 |
} |
|
2 | 670 |
} |