在我们讨论反射代码之前,我认为您需要另一个注释来表示表示表示自定义表单的类。你的电流
@Form
注释更适合于字段级别,因为它表示每个表单字段的标签。我会把它改名为
@FormField
. 那么,你的
@形式
注释只用于告诉反射API哪些类是自定义表单。
private static Reflections reflections = new Reflections("your.java.package");
用你的
@形式
Set> customForms = reflections.getTypesAnnotatedWith(Form.class)
现在,您可以循环遍历每个自定义表单,并解析每个字段的注释:
// Loop through every class with Form annotation
for (Class extends Forms> form : customForms) {
for (Field field : form.getDeclaredFields()) {
// Check each field, if it has your FormField attribute, you can then access the annotation methods
if (field.isAnnotationPresent(FormField.class)) {
Label label = new Label(field.getAnnotation(FormField.class).label());
// Do additional stuff to build your form
}
}
}