Ejemplo n.º 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));
        }
      }
    }
  }
  private static void convertProtocolToAsciidocTable(Properties props, Class<Protocol> clazz)
      throws Exception {
    boolean isUnsupported = clazz.isAnnotationPresent(Unsupported.class);
    if (isUnsupported) return;

    Map<String, String> nameToDescription = new TreeMap<>();

    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
      if (field.isAnnotationPresent(Property.class)) {
        String property = field.getName();
        Property annotation = field.getAnnotation(Property.class);
        String desc = annotation.description();
        nameToDescription.put(property, desc);
      }
    }

    // iterate methods
    Method[] methods = clazz.getDeclaredMethods();
    for (Method method : methods) {
      if (method.isAnnotationPresent(Property.class)) {

        Property annotation = method.getAnnotation(Property.class);
        String desc = annotation.description();

        if (desc == null || desc.isEmpty()) desc = "n/a";

        String name = annotation.name();
        if (name.length() < 1) {
          name = Util.methodNameToAttributeName(method.getName());
        }
        nameToDescription.put(name, desc);
      }
    }

    // do we have more than one property (superclass Protocol has only one property (stats))
    if (nameToDescription.isEmpty()) return;

    List<String[]> rows = new ArrayList<>(nameToDescription.size() + 1);
    rows.add(new String[] {"Name", "Description"});
    for (Map.Entry<String, String> entry : nameToDescription.entrySet())
      rows.add(new String[] {entry.getKey(), entry.getValue()});

    String tmp =
        createAsciidocTable(
            rows,
            clazz.getSimpleName(),
            "[align=\"left\",width=\"90%\",cols=\"2,10\",options=\"header\"]");
    props.put(clazz.getSimpleName(), tmp);
  }
Ejemplo n.º 3
0
  private void exposeManagedAttribute(Method method) {
    String methodName = method.getName();
    if (!methodName.startsWith("get")
        && !methodName.startsWith("set")
        && !methodName.startsWith("is")) {
      if (log.isWarnEnabled())
        log.warn(
            "method name "
                + methodName
                + " doesn't start with \"get\", \"set\", or \"is\""
                + ", but is annotated with @ManagedAttribute: will be ignored");
      return;
    }
    ManagedAttribute attr = method.getAnnotation(ManagedAttribute.class);
    Property prop = method.getAnnotation(Property.class);

    boolean expose_prop = prop != null && prop.exposeAsManagedAttribute();
    boolean expose = attr != null || expose_prop;
    if (!expose) return;

    // Is name field of @ManagedAttributed used?
    String attributeName = attr != null ? attr.name() : null;
    if (attributeName != null && attributeName.trim().length() > 0)
      attributeName = attributeName.trim();
    else attributeName = null;

    String descr = attr != null ? attr.description() : prop != null ? prop.description() : null;

    boolean writeAttribute = false;
    MBeanAttributeInfo info = null;
    if (isSetMethod(method)) { // setter
      attributeName = (attributeName == null) ? methodName.substring(3) : attributeName;
      info =
          new MBeanAttributeInfo(
              attributeName,
              method.getParameterTypes()[0].getCanonicalName(),
              descr,
              true,
              true,
              false);
      writeAttribute = true;
    } else { // getter
      if (method.getParameterTypes().length == 0 && method.getReturnType() != java.lang.Void.TYPE) {
        boolean hasSetter = atts.containsKey(attributeName);
        // we found is method
        if (methodName.startsWith("is")) {
          attributeName = (attributeName == null) ? methodName.substring(2) : attributeName;
          info =
              new MBeanAttributeInfo(
                  attributeName,
                  method.getReturnType().getCanonicalName(),
                  descr,
                  true,
                  hasSetter,
                  true);
        } else {
          // this has to be get
          attributeName = (attributeName == null) ? methodName.substring(3) : attributeName;
          info =
              new MBeanAttributeInfo(
                  attributeName,
                  method.getReturnType().getCanonicalName(),
                  descr,
                  true,
                  hasSetter,
                  false);
        }
      } else {
        if (log.isWarnEnabled()) {
          log.warn(
              "Method " + method.getName() + " must have a valid return type and zero parameters");
        }
        // silently skip this method
        return;
      }
    }

    AttributeEntry ae = atts.get(attributeName);
    // is it a read method?
    if (!writeAttribute) {
      // we already have annotated field as read
      if (ae instanceof FieldAttributeEntry && ae.getInfo().isReadable()) {
        log.warn("not adding annotated method " + method + " since we already have read attribute");
      }
      // we already have annotated set method
      else if (ae instanceof MethodAttributeEntry) {
        MethodAttributeEntry mae = (MethodAttributeEntry) ae;
        if (mae.hasSetMethod()) {
          atts.put(
              attributeName, new MethodAttributeEntry(mae.getInfo(), mae.getSetMethod(), method));
        }
      } // we don't have such entry
      else {
        atts.put(
            attributeName,
            new MethodAttributeEntry(info, findSetter(obj.getClass(), attributeName), method));
      }
    } // is it a set method?
    else {
      if (ae instanceof FieldAttributeEntry) {
        // we already have annotated field as write
        if (ae.getInfo().isWritable()) {
          log.warn(
              "Not adding annotated method "
                  + methodName
                  + " since we already have writable attribute");
        } else {
          // we already have annotated field as read
          // lets make the field writable
          Field f = ((FieldAttributeEntry) ae).getField();
          MBeanAttributeInfo i =
              new MBeanAttributeInfo(
                  ae.getInfo().getName(),
                  f.getType().getCanonicalName(),
                  descr,
                  true,
                  !Modifier.isFinal(f.getModifiers()),
                  false);
          atts.put(attributeName, new FieldAttributeEntry(i, f));
        }
      }
      // we already have annotated getOrIs method
      else if (ae instanceof MethodAttributeEntry) {
        MethodAttributeEntry mae = (MethodAttributeEntry) ae;
        if (mae.hasIsOrGetMethod()) {
          atts.put(attributeName, new MethodAttributeEntry(info, method, mae.getIsOrGetMethod()));
        }
      } // we don't have such entry
      else {
        atts.put(
            attributeName,
            new MethodAttributeEntry(info, method, findGetter(obj.getClass(), attributeName)));
      }
    }
  }