我是java源代码转换的spoon库新手。尝试使用它来转换现有的REST服务。需求是在类级别添加io.swagger.annotations.SwaggerDefinition注释,例如:
@SwaggerDefinition(
tags = {
@Tag(
name = "api name", description = "api description"
)
}
)
面临的问题是,当我添加注释时,它被添加为:
@SwaggerDefinition(
tags =
@Tag(
name = "api name", description = "api description"
)
)
注意:缺少大括号'{'和匹配的右大括号'}'
下面是正在使用的函数:
private CtAnnotation createSwaggerDefinitionAnnotation(CtClass<?> ctClass) throws JSONException {
CtAnnotation tagAnnotation = createTagAnnotation(ctClass);
CtAnnotation swaggerDefAnnotation = getFactory().Core().createAnnotation();
CtTypeReference<Object> ref = getFactory().Core().createTypeReference();
ref.setSimpleName("SwaggerDefinition");
CtPackageReference refPackage = getFactory().Core().createPackageReference();
refPackage.setSimpleName("io.swagger.annotations");
ref.setPackage(refPackage);
swaggerDefAnnotation.setAnnotationType(ref);
swaggerDefAnnotation.addValue("tags", tagAnnotation); // TODO: The tag should be wrapped in curly braces
return swaggerDefAnnotation;
}
private CtAnnotation createTagAnnotation(CtClass<?> ctClass) {
String className = ctClass.getQualifiedName();
CtAnnotation tagAnnotation = getFactory().Core().createAnnotation();
CtTypeReference<Object> ref = getFactory().Core().createTypeReference();
ref.setSimpleName("Tag");
CtPackageReference refPackage = getFactory().Core().createPackageReference();
refPackage.setSimpleName("io.swagger.annotations");
ref.setPackage(refPackage);
tagAnnotation.setAnnotationType(ref);
tagAnnotation.addValue("name", getSwaggerDefinitionTagName(className));
tagAnnotation.addValue("description", getSwaggerDefinitionTagDescription(className));
return tagAnnotation;
}
有人能认出丢失的那块吗?我看不懂
为了在值周围添加大括号,您需要创建一个包含注释值的CtNewArray。
private CtAnnotation createSwaggerDefinitionAnnotation(CtClass<?> ctClass) {
CtAnnotation tagAnnotation = createTagAnnotation(ctClass);
CtAnnotation swaggerDefAnnotation = getFactory().Core().createAnnotation();
CtTypeReference<Object> ref = getFactory().Core().createTypeReference();
ref.setSimpleName("SwaggerDefinition");
CtPackageReference refPackage = getFactory().Core().createPackageReference();
refPackage.setSimpleName("io.swagger.annotations");
ref.setPackage(refPackage);
swaggerDefAnnotation.setAnnotationType(ref);
CtNewArray<Object> arrayValue = getFactory().Core().createNewArray();
arrayValue.addElement(tagAnnotation);
swaggerDefAnnotation.addValue("tags", arrayValue);
return swaggerDefAnnotation;
}
输出:
@io.swagger.annotations.SwaggerDefinition(
tags = {
@io.swagger.annotations.Tag(
description = "description", name = "name")
})