39 import java.util.List;
40 import java.util.LinkedList;
41 import java.util.Locale;
42 import java.util.Map;
43 import java.util.WeakHashMap;
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
52 import com.sun.jmx.remote.util.EnvHelp;
53 import java.beans.BeanInfo;
54 import java.beans.PropertyDescriptor;
55 import java.lang.reflect.Array;
56 import java.lang.reflect.InvocationTargetException;
57 import javax.management.AttributeNotFoundException;
58 import javax.management.openmbean.CompositeData;
59
60 /**
61 * This class contains the methods for performing all the tests needed to verify
62 * that a class represents a JMX compliant MBean.
63 *
64 * @since 1.5
65 */
66 public class Introspector {
67
68
69 /*
70 * ------------------------------------------
71 * PRIVATE CONSTRUCTORS
72 * ------------------------------------------
73 */
74
75 // private constructor defined to "hide" the default public constructor
76 private Introspector() {
77
78 // ------------------------------
511 return ((CompositeData) complex).get(element);
512 } else {
513 // Java Beans introspection
514 //
515 Class<?> clazz = complex.getClass();
516 Method readMethod = null;
517 if (BeansHelper.isAvailable()) {
518 Object bi = BeansHelper.getBeanInfo(clazz);
519 Object[] pds = BeansHelper.getPropertyDescriptors(bi);
520 for (Object pd: pds) {
521 if (BeansHelper.getPropertyName(pd).equals(element)) {
522 readMethod = BeansHelper.getReadMethod(pd);
523 break;
524 }
525 }
526 } else {
527 // Java Beans not available so use simple introspection
528 // to locate method
529 readMethod = SimpleIntrospector.getReadMethod(clazz, element);
530 }
531 if (readMethod != null)
532 return readMethod.invoke(complex);
533
534 throw new AttributeNotFoundException(
535 "Could not find the getter method for the property " +
536 element + " using the Java Beans introspector");
537 }
538 } catch (InvocationTargetException e) {
539 throw new IllegalArgumentException(e);
540 } catch (AttributeNotFoundException e) {
541 throw e;
542 } catch (Exception e) {
543 throw EnvHelp.initCause(
544 new AttributeNotFoundException(e.getMessage()), e);
545 }
546 }
547
548 /**
549 * A simple introspector that uses reflection to analyze a class and
550 * identify its "getter" methods. This class is intended for use only when
551 * Java Beans is not present (which implies that there isn't explicit
552 * information about the bean available).
|
39 import java.util.List;
40 import java.util.LinkedList;
41 import java.util.Locale;
42 import java.util.Map;
43 import java.util.WeakHashMap;
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
52 import com.sun.jmx.remote.util.EnvHelp;
53 import java.beans.BeanInfo;
54 import java.beans.PropertyDescriptor;
55 import java.lang.reflect.Array;
56 import java.lang.reflect.InvocationTargetException;
57 import javax.management.AttributeNotFoundException;
58 import javax.management.openmbean.CompositeData;
59 import sun.reflect.misc.MethodUtil;
60 import sun.reflect.misc.ReflectUtil;
61
62 /**
63 * This class contains the methods for performing all the tests needed to verify
64 * that a class represents a JMX compliant MBean.
65 *
66 * @since 1.5
67 */
68 public class Introspector {
69
70
71 /*
72 * ------------------------------------------
73 * PRIVATE CONSTRUCTORS
74 * ------------------------------------------
75 */
76
77 // private constructor defined to "hide" the default public constructor
78 private Introspector() {
79
80 // ------------------------------
513 return ((CompositeData) complex).get(element);
514 } else {
515 // Java Beans introspection
516 //
517 Class<?> clazz = complex.getClass();
518 Method readMethod = null;
519 if (BeansHelper.isAvailable()) {
520 Object bi = BeansHelper.getBeanInfo(clazz);
521 Object[] pds = BeansHelper.getPropertyDescriptors(bi);
522 for (Object pd: pds) {
523 if (BeansHelper.getPropertyName(pd).equals(element)) {
524 readMethod = BeansHelper.getReadMethod(pd);
525 break;
526 }
527 }
528 } else {
529 // Java Beans not available so use simple introspection
530 // to locate method
531 readMethod = SimpleIntrospector.getReadMethod(clazz, element);
532 }
533 if (readMethod != null) {
534 ReflectUtil.checkPackageAccess(readMethod.getDeclaringClass());
535 return MethodUtil.invoke(readMethod, complex, new Class[0]);
536 }
537
538 throw new AttributeNotFoundException(
539 "Could not find the getter method for the property " +
540 element + " using the Java Beans introspector");
541 }
542 } catch (InvocationTargetException e) {
543 throw new IllegalArgumentException(e);
544 } catch (AttributeNotFoundException e) {
545 throw e;
546 } catch (Exception e) {
547 throw EnvHelp.initCause(
548 new AttributeNotFoundException(e.getMessage()), e);
549 }
550 }
551
552 /**
553 * A simple introspector that uses reflection to analyze a class and
554 * identify its "getter" methods. This class is intended for use only when
555 * Java Beans is not present (which implies that there isn't explicit
556 * information about the bean available).
|