gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:01 24 4
gpt4 key购买 nike

我们可以使用 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

这是我给出的异常(exception):

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)

最佳答案

如果您只是从注释中获取“可为空”值。则不需要此代码

       <#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()}

关于java - 如何在freemarker模板中调用反射方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48871598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com