I'm using JSR-380 for my bean validation and I'd like to (at run-time) be able to annotate the generic parameter in a list (using javassist) to look like:
Class ABC {
public List<@Size(min=10, max=100) String> value;
}
I've written the following code which gets part of the way, but I'm still missing the annotation on the genetic type and the result simply validates the list size, not the list item size:
public static void main(String[] args) throws Exception {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.makeClass("MY_CCCLASS");
CtClass ccList = ClassPool.getDefault().get("java.util.List");
CtField f = new CtField(ccList, "value", cc);
f.setGenericSignature(SignatureAttribute.toClassSignature(getGenericSignature(String.class)).encode());
final ClassFile cfile = cc.getClassFile();
final ConstPool cpool = cfile.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag);
Annotation a = new Annotation(Size.class.getName(), cpool);
attr.setAnnotation(a);
f.getFieldInfo().addAttribute(attr);
cc.addField(f);
cc.toClass();
cc.writeFile();
}
Which gives:
import java.util.List;
import javax.validation.constraints.Size;
public class MY_CCCLASS {
@Size
List<String> value;
public MY_CCCLASS() {
}
}
Anyone know how to do this? I'm using the following javassist lib:
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.17.1-GA</version>
</dependency>
Thanks
Aucun commentaire:
Enregistrer un commentaire