使用 AbstractProcessor 获取自定义注解的方法 获取注解的属性名称和值 属性值是 AnnotationValue 类型 使用 javapoet AnnotationSpec.Builder(xx) 生成代码。AddMember(String name, String format, Object... args) 生成错误代码: 如:method (@AnnotationName(value = "/" v1 / "/v2"/")) Method (@annotationname (value= {"v1","v2"})) 既然有好几种注解属性类型,那么如何分辨AnnotationValue是哪一个,用javapoet生成正确的代码
AnnotationSpec.Builder builder = AnnotationSpec.builder(cc);
//Gets the attributes and attribute values of the annotation
//String:Attribute name of the annotation
//Object:(AnnotationVale) Attribute value
Map<String, Object> annoMap = annotationModel.getParams();
if (annoMap != null) {
Set<Map.Entry<String, Object>> set = annoMap.entrySet();
for (Map.Entry<String, Object> m : set) {
String memberName = m.getKey();
Object values = m.getValue();
builder.addMember(memberName, "$S", values);
}
}
这是javapoet当前生成的错误代码:
@RequestMapping( method = "org.springframework.web.bind.annotation.RequestMethod.GET,org.springframework.web.bind.annotation.RequestMethod.POST", value = "\"/api\",\"/ll\"" ) class TestController$ { @RequestMapping( vv = "@com.seal.base.core.annotation.SealVar(\"1111\")", method = "org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET", name = "name", value = "\"/register\",\"/api\"" )