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;
}
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);
}
Aggregations