如何在freemarker模板中调用反射方法?

时间:2018-02-19 17:49:25

标签: java reflection code-generation freemarker

我们可以在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");
                }
            }
        }
    }
}

上面的代码想要检查如果其中一个字段的注释具有nullable方法,请记录它的值。假设这个领域有一些类:

public class someClass {
    @Column(nullable = false)
    private Long number;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "entity_id", nullable = true)
    private Entity entity;
}

此类的上述代码会在字段注释中记录false,然后true作为nullable属性的值。现在我想在freemarker模板中迭代类字段和注释,但问题是我无法调用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>

还有另一种方法可以在freemarker模板中实现这一目标吗? 我想将这些代码用于代码生成器。

UDPATE

这是我给出的例外:

Syntax error in template "entity-detail.ftl" in line 32, column 111:
Encountered "]", but was expecting one of:
    <STRING_LITERAL>
    <RAW_STRING>
    "false"
    "true"
    <INTEGER>
    <DECIMAL>
    "."
    "+"
    "-"
    "!"
    "["
    "("
    "{"
    <ID>
freemarker.core.ParseException: Syntax error in template "entity-detail.ftl" in line 32, column 111:
Encountered "]", but was expecting one of:
    <STRING_LITERAL>
    <RAW_STRING>
    "false"
    "true"
    <INTEGER>
    <DECIMAL>
    "."
    "+"
    "-"
    "!"
    "["
    "("
    "{"
    <ID>
    at freemarker.core.FMParser.generateParseException(FMParser.java:5749)
    at freemarker.core.FMParser.jj_consume_token(FMParser.java:5608)
    at freemarker.core.FMParser.UnaryExpression(FMParser.java:658)
    at freemarker.core.FMParser.MultiplicativeExpression(FMParser.java:768)
    at freemarker.core.FMParser.AdditiveExpression(FMParser.java:720)
    at freemarker.core.FMParser.RangeExpression(FMParser.java:900)
    at freemarker.core.FMParser.RelationalExpression(FMParser.java:848)
    at freemarker.core.FMParser.EqualityExpression(FMParser.java:811)
    at freemarker.core.FMParser.AndExpression(FMParser.java:967)
    at freemarker.core.FMParser.OrExpression(FMParser.java:989)
    at freemarker.core.FMParser.Expression(FMParser.java:548)
    at freemarker.core.FMParser.DynamicKey(FMParser.java:1341)
    at freemarker.core.FMParser.AddSubExpression(FMParser.java:1107)
    at freemarker.core.FMParser.PrimaryExpression(FMParser.java:607)
    at freemarker.core.FMParser.UnaryExpression(FMParser.java:653)
    at freemarker.core.FMParser.MultiplicativeExpression(FMParser.java:768)
    at freemarker.core.FMParser.AdditiveExpression(FMParser.java:720)
    at freemarker.core.FMParser.RangeExpression(FMParser.java:900)
    at freemarker.core.FMParser.RelationalExpression(FMParser.java:848)
    at freemarker.core.FMParser.EqualityExpression(FMParser.java:811)
    at freemarker.core.FMParser.AndExpression(FMParser.java:967)
    at freemarker.core.FMParser.OrExpression(FMParser.java:989)
    at freemarker.core.FMParser.Expression(FMParser.java:548)
    at freemarker.core.FMParser.Parenthesis(FMParser.java:616)
    at freemarker.core.FMParser.PrimaryExpression(FMParser.java:588)
    at freemarker.core.FMParser.UnaryExpression(FMParser.java:653)
    at freemarker.core.FMParser.MultiplicativeExpression(FMParser.java:768)
    at freemarker.core.FMParser.AdditiveExpression(FMParser.java:720)
    at freemarker.core.FMParser.RangeExpression(FMParser.java:900)
    at freemarker.core.FMParser.RelationalExpression(FMParser.java:848)
    at freemarker.core.FMParser.EqualityExpression(FMParser.java:811)
    at freemarker.core.FMParser.AndExpression(FMParser.java:967)
    at freemarker.core.FMParser.OrExpression(FMParser.java:989)
    at freemarker.core.FMParser.Expression(FMParser.java:548)
    at freemarker.core.FMParser.PositionalArgs(FMParser.java:2985)
    at freemarker.core.FMParser.MethodArgs(FMParser.java:1357)
    at freemarker.core.FMParser.AddSubExpression(FMParser.java:1111)
    at freemarker.core.FMParser.PrimaryExpression(FMParser.java:607)
    at freemarker.core.FMParser.UnaryExpression(FMParser.java:653)
    at freemarker.core.FMParser.MultiplicativeExpression(FMParser.java:768)
    at freemarker.core.FMParser.AdditiveExpression(FMParser.java:720)
    at freemarker.core.FMParser.RangeExpression(FMParser.java:900)
    at freemarker.core.FMParser.RelationalExpression(FMParser.java:848)
    at freemarker.core.FMParser.EqualityExpression(FMParser.java:811)
    at freemarker.core.FMParser.AndExpression(FMParser.java:967)
    at freemarker.core.FMParser.OrExpression(FMParser.java:989)
    at freemarker.core.FMParser.Expression(FMParser.java:548)
    at freemarker.core.FMParser.StringOutput(FMParser.java:1522)
    at freemarker.core.FMParser.MixedContentElements(FMParser.java:3721)
    at freemarker.core.FMParser.If(FMParser.java:1625)
    at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:3384)
    at freemarker.core.FMParser.MixedContentElements(FMParser.java:3772)
    at freemarker.core.FMParser.Attempt(FMParser.java:1671)
    at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:3511)
    at freemarker.core.FMParser.MixedContentElements(FMParser.java:3772)
    at freemarker.core.FMParser.List(FMParser.java:1746)
    at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:3388)
    at freemarker.core.FMParser.MixedContentElements(FMParser.java:3772)
    at freemarker.core.FMParser.If(FMParser.java:1625)
    at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:3384)
    at freemarker.core.FMParser.MixedContentElements(FMParser.java:3772)
    at freemarker.core.FMParser.If(FMParser.java:1625)
    at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:3384)
    at freemarker.core.FMParser.MixedContentElements(FMParser.java:3772)
    at freemarker.core.FMParser.List(FMParser.java:1746)
    at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:3388)
    at freemarker.core.FMParser.MixedContentElements(FMParser.java:3772)
    at freemarker.core.FMParser.Attempt(FMParser.java:1671)
    at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:3511)
    at freemarker.core.FMParser.MixedContentElements(FMParser.java:3772)
    at freemarker.core.FMParser.List(FMParser.java:1746)
    at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:3388)
    at freemarker.core.FMParser.MixedContentElements(FMParser.java:3772)
    at freemarker.core.FMParser.Root(FMParser.java:4411)
    at freemarker.template.Template.<init>(Template.java:252)
    at freemarker.cache.TemplateCache.loadTemplate(TemplateCache.java:549)
    at freemarker.cache.TemplateCache.getTemplateInternal(TemplateCache.java:439)
    at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:292)
    at freemarker.template.Configuration.getTemplate(Configuration.java:2654)
    at freemarker.template.Configuration.getTemplate(Configuration.java:2503)
    at com.vira.framework.common.generator.CodeGenerator.generate(CodeGenerator.java:70)
    at com.vira.framework.common.generator.CodeGeneratorTest.main(CodeGeneratorTest.java:7)

1 个答案:

答案 0 :(得分:0)

如果你得到了可以自由的&#34;您的注释中的值。您不需要此代码

       <#list annotation.annotationType().getMethods() as annotationMethod>
                <#attempt>
                    <#if annotationMethod.getName() == "nullable">
                        // this line get error 
                        ${annotationMethod.invoke(annotation, (Object[])null)}
                    </#if>
                    <#recover>
                </#attempt>
            </#list>

写这个!!

${annotation.nullable()}