我正在使用com.sun.codemodel来生成我的类并添加JSR303注释。这工作正常,除非我想添加@ Pattern.List注释。
field.annotate(Pattern.class).param("regexp", "some_expression");
这是目前生成的内容:
@Pattern(regexp = "first_expression", message = "first.Pattern.message")
@Pattern(regexp = "second_expression", message = "second.Pattern.message")
@Pattern(regexp = "third_expression", message = "third.Pattern.message")
这就是我想要生成的内容
@Pattern.List({
@Pattern(regexp = "first_expression", message = "first.Pattern.message")
@Pattern(regexp = "second_expression", message = "second.Pattern.message")
@Pattern(regexp = "third_expression", message = "third.Pattern.message")
})
有人知道这是怎么做到的吗?
答案 0 :(得分:3)
假设你想要@PatternList(list = {@Pattern ...}),你需要使用paramArray()
方法:
JDefinedClass testme = codeModel._class("Testme");
JAnnotationUse patternListAnnotation = testme.annotate(Pattern.List.class);
JAnnotationArrayMember list = patternListAnnotation.paramArray("value");
list.annotate(Pattern.class).param("regexp", "first_expression").param("message", "first.Pattern.message");
list.annotate(Pattern.class).param("regexp", "second_expression").param("message", "second.Pattern.message");
list.annotate(Pattern.class).param("regexp", "third_expression").param("message", "third.Pattern.message");
输出以下内容:
@Pattern.List({
@Pattern(regexp = "first_expression", message = "first.Pattern.message"),
@Pattern(regexp = "second_expression", message = "second.Pattern.message"),
@Pattern(regexp = "third_expression", message = "third.Pattern.message")
})
public class Testme {
}
对于示例代码中提示的字段,这应该是相同的。