Ejemplo n.º 1
0
  @SuppressWarnings("rawtypes")
  public static ApiObjectDoc buildFromAnnotation(ApiObject annotation, Class clazz) {

    final List<ApiObjectFieldDoc> fieldDocs = new ArrayList<ApiObjectFieldDoc>();
    for (Field field : clazz.getDeclaredFields())
      if (field.getAnnotation(ApiObjectField.class) != null)
        fieldDocs.add(
            ApiObjectFieldDoc.buildFromAnnotation(
                field.getAnnotation(ApiObjectField.class), field));

    for (Method getter : clazz.getDeclaredMethods())
      if (getter.getAnnotation(ApiObjectGetter.class) != null
          && getter.getName().startsWith("get")
          && getter.getParameterTypes().length == 0)
        fieldDocs.add(
            ApiObjectFieldDoc.buildFromAnnotation(
                getter.getAnnotation(ApiObjectGetter.class), getter));

    Class<?> c = clazz.getSuperclass();
    if (c != null) {
      if (c.isAnnotationPresent(ApiObject.class)) {
        ApiObjectDoc objDoc = ApiObjectDoc.buildFromAnnotation(c.getAnnotation(ApiObject.class), c);
        fieldDocs.addAll(objDoc.getFields());
      }
    }

    return new ApiObjectDoc(annotation.name(), annotation.description(), fieldDocs);
  }
  public static ApiObjectDoc build(Class<?> clazz) {
    ApiObject apiObject = clazz.getAnnotation(ApiObject.class);
    ApiObjectDoc apiObjectDoc = new ApiObjectDoc();

    Set<ApiObjectFieldDoc> fieldDocs = new TreeSet<ApiObjectFieldDoc>();

    for (Field field : clazz.getDeclaredFields()) {
      if (field.getAnnotation(ApiObjectField.class) != null) {
        ApiObjectFieldDoc fieldDoc =
            JSONDocApiObjectFieldDocBuilder.build(field.getAnnotation(ApiObjectField.class), field);
        fieldDoc.setSupportedversions(JSONDocApiVersionDocBuilder.build(field));
        fieldDocs.add(fieldDoc);
      }
    }

    for (Method method : clazz.getDeclaredMethods()) {
      if (JSONDocUtils.isFieldMethod(method)) {
        ApiObjectFieldDoc fieldDoc =
            JSONDocApiObjectFieldDocBuilder.build(
                method.getAnnotation(ApiObjectField.class), method);
        fieldDoc.setSupportedversions(JSONDocApiVersionDocBuilder.build(method));
        fieldDocs.add(fieldDoc);
      }
    }

    Class<?> c = clazz.getSuperclass();
    if (c != null) {
      if (c.isAnnotationPresent(ApiObject.class)) {
        ApiObjectDoc objDoc = build(c);
        fieldDocs.addAll(objDoc.getFields());
      }
    }

    if (clazz.isEnum()) {
      apiObjectDoc.setAllowedvalues(
          DefaultJSONDocScanner.enumConstantsToStringArray(clazz.getEnumConstants()));
    }

    if (apiObject.name().trim().isEmpty()) {
      apiObjectDoc.setName(clazz.getSimpleName().toLowerCase());
    } else {
      apiObjectDoc.setName(apiObject.name());
    }

    apiObjectDoc.setDescription(apiObject.description());
    apiObjectDoc.setFields(fieldDocs);
    apiObjectDoc.setGroup(apiObject.group());
    apiObjectDoc.setVisibility(apiObject.visibility());
    apiObjectDoc.setStage(apiObject.stage());
    apiObjectDoc.setShow(apiObject.show());

    return apiObjectDoc;
  }