Search in sources :

Example 11 with ApiComment

use of com.terran4j.commons.api2doc.annotations.ApiComment in project commons by terran4j.

the class ApiCommentUtils method getSeeApiComment.

/**
 * 获取可参照的 @ApiComment 注解对象。
 * @param apiComment
 * @param defaultSeeClass
 * @param defaultSeeField
 * @return
 */
private static ApiComment getSeeApiComment(ApiComment apiComment, Class<?> defaultSeeClass, String defaultSeeField) {
    Class<?> seeClass = null;
    if (apiComment != null) {
        seeClass = apiComment.seeClass();
    }
    if (seeClass == null || seeClass == Object.class) {
        seeClass = defaultSeeClass;
    }
    if (seeClass == null || seeClass == Object.class) {
        return null;
    }
    String seeField = null;
    if (apiComment != null) {
        seeField = apiComment.seeField();
    }
    if (StringUtils.isEmpty(seeField)) {
        seeField = defaultSeeField;
    }
    if (StringUtils.isEmpty(seeField)) {
        return null;
    }
    // 记录引用过的 seeClass, 用于循环引用检测。
    List<Class<?>> path = new ArrayList<>();
    Field field = null;
    ApiComment seeComment = null;
    while (seeClass != null) {
        // 循环引用检测。
        if (path.contains(seeClass)) {
            StringBuffer sb = new StringBuffer();
            sb.append("@ApiComment 中的 seeClass 不允许循环引用:");
            for (int i = 0; i < path.size(); i++) {
                if (i > 0) {
                    sb.append(" --> ");
                }
                sb.append(seeClass.getSimpleName());
            }
            throw new RuntimeException(sb.toString());
        }
        path.add(seeClass);
        // 寻找匹配字段中的 ApiComment 。
        field = Classes.getField(seeField, seeClass);
        if (field != null) {
            seeComment = field.getAnnotation(ApiComment.class);
            if (seeComment != null) {
                return seeComment;
            }
        }
        ApiComment parentApiComment = seeClass.getAnnotation(ApiComment.class);
        if (parentApiComment == null) {
            break;
        }
        seeClass = parentApiComment.seeClass();
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) ApiComment(com.terran4j.commons.api2doc.annotations.ApiComment) ArrayList(java.util.ArrayList) ApiObject(com.terran4j.commons.api2doc.domain.ApiObject)

Example 12 with ApiComment

use of com.terran4j.commons.api2doc.annotations.ApiComment in project commons by terran4j.

the class EnumCodeWriter method writeCode.

@SuppressWarnings({ "rawtypes", "unchecked" })
public // 
void writeCode(// 
Class<?> currentClass, // 
String className, CodeOutput out, CodeConfig config) throws Exception {
    if (currentClass == null || !currentClass.isEnum()) {
        return;
    }
    Map<String, Object> model = new HashMap<>();
    model.put("class", className);
    if (config == null) {
        config = new CodeConfig();
    }
    model.put("config", config);
    List<EnumInfo> enumInfos = new ArrayList<>();
    Class<Enum<?>> enumClass = (Class<Enum<?>>) currentClass;
    Enum[] enums = enumClass.getEnumConstants();
    for (Enum enumObject : enums) {
        EnumInfo enumInfo = new EnumInfo();
        String name = enumObject.name();
        enumInfo.setName(name);
        String comment = null;
        Field field = null;
        try {
            field = enumClass.getDeclaredField(name);
        } catch (NoSuchFieldException | SecurityException e1) {
            log.error(// 
            "Can't get field \"" + name + "\" from Enum: " + enumClass.getName(), e1);
            continue;
        }
        ApiComment apiComment = field.getAnnotation(ApiComment.class);
        comment = ApiCommentUtils.getComment(apiComment, null, field.getName());
        if (comment != null) {
            comment = new FlexibleString(comment).javadoc(1);
        }
        // if (apiComment != null && StringUtils.hasText(apiComment.value())) {
        // comment = new FlexibleString(apiComment.value().trim()).javadoc(1);
        // }
        enumInfo.setComment(comment);
        enumInfos.add(enumInfo);
    }
    model.put("enums", enumInfos);
    String code = classpathFreeMarker.build(enumTemplate, model);
    out.writeCodeFile(className + ".java", code);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FlexibleString(com.terran4j.commons.api2doc.impl.FlexibleString) FlexibleString(com.terran4j.commons.api2doc.impl.FlexibleString) Field(java.lang.reflect.Field) ApiComment(com.terran4j.commons.api2doc.annotations.ApiComment)

Aggregations

ApiComment (com.terran4j.commons.api2doc.annotations.ApiComment)12 Field (java.lang.reflect.Field)5 Api2Doc (com.terran4j.commons.api2doc.annotations.Api2Doc)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 PropertyDescriptor (java.beans.PropertyDescriptor)2 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)2 ApiError (com.terran4j.commons.api2doc.annotations.ApiError)1 ApiErrors (com.terran4j.commons.api2doc.annotations.ApiErrors)1 ApiDataType (com.terran4j.commons.api2doc.domain.ApiDataType)1 ApiObject (com.terran4j.commons.api2doc.domain.ApiObject)1 FlexibleString (com.terran4j.commons.api2doc.impl.FlexibleString)1 KeyedList (com.terran4j.commons.util.value.KeyedList)1 IOException (java.io.IOException)1 Parameter (java.lang.reflect.Parameter)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1 Resource (org.springframework.core.io.Resource)1