We can iterate over class fields, annotations with some code like this in java:
Class<?> clazz;
for(Field field : clazz.getDeclaredFields()) {
for(Annotation annotation : field.getDeclaredAnnotations()) {
for(Method method : annotation.annotationType().getMethods()) {
if(method.getName().equals("nullable")) {
try {
// note this line
System.out.println(method.invoke(annotation, (Object[])null) );
}
catch(Exception e) {
System.out.println("Ex");
}
}
}
}
}
above codes want to check that if one of annotations of fields have nullable
method, log it's value. assume this field some class:
public class someClass {
@Column(nullable = false)
private Long number;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "entity_id", nullable = true)
private Entity entity;
}
That above code for this class logs false
, then true
as a value of nullable
attribute in field annotations. Now i want to iterate over class fields and annotations in freemarker template, but the problem is that i cant call method.invoke(annotation, (Object[])null)
.
<#list fields as field>
<#list field.annotations as annotation>
<#if annotation??>
<#if annotation.annotationType().getSimpleName() == "Column" || annotation.annotationType().getSimpleName() == "JoinColumn">
<#list annotation.annotationType().getMethods() as annotationMethod>
<#attempt>
<#if annotationMethod.getName() == "nullable">
// this line get error
${annotationMethod.invoke(annotation, (Object[])null)}
</#if>
<#recover>
</#attempt>
</#list>
<#break>
</#if>
</#if>
</#list>
</#list>
Is there another way to achieve this goal in freemarker template? I want to use these code for code generator.
Aucun commentaire:
Enregistrer un commentaire