Example #1
0
  private void findFields() {
    // traverse class hierarchy and find all annotated fields
    for (Class<?> clazz = getObject().getClass(); clazz != null; clazz = clazz.getSuperclass()) {

      Field[] fields = clazz.getDeclaredFields();
      for (Field field : fields) {
        ManagedAttribute attr = field.getAnnotation(ManagedAttribute.class);
        Property prop = field.getAnnotation(Property.class);
        boolean expose_prop = prop != null && prop.exposeAsManagedAttribute();
        boolean expose = attr != null || expose_prop;

        if (expose) {
          String fieldName = Util.attributeNameToMethodName(field.getName());
          String descr = attr != null ? attr.description() : prop.description();
          boolean writable = attr != null ? attr.writable() : prop.writable();

          MBeanAttributeInfo info =
              new MBeanAttributeInfo(
                  fieldName,
                  field.getType().getCanonicalName(),
                  descr,
                  true,
                  !Modifier.isFinal(field.getModifiers()) && writable,
                  false);

          atts.put(fieldName, new FieldAttributeEntry(info, field));
        }
      }
    }
  }