Search in sources :

Example 1 with FlexibleString

use of com.terran4j.commons.api2doc.impl.FlexibleString 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)1 FlexibleString (com.terran4j.commons.api2doc.impl.FlexibleString)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1