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));
        }
      }
    }
  }
Example #2
0
 private static void _testAttributeNameToMethodName(String input, String expected_output) {
   String method_name = Util.attributeNameToMethodName(input);
   System.out.println(
       "attrname="
           + input
           + ", method name="
           + method_name
           + ", expected output="
           + expected_output);
   assert method_name.equals(expected_output)
       : "attrname="
           + input
           + ", method name="
           + method_name
           + ", expected output="
           + expected_output;
 }