我是勺子库的新手,用于转换java源代码。尝试使用它来转换现有的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;
}
有人可以识别丢失的部分。我无法弄清楚。
答案 0 :(得分:0)
为了在值周围添加括号,您需要创建一个包含注释值的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")
})