如何生成使用JDT类级别的注释多个值 [英] How to generate multiple values for a class level annotation using JDT
本文介绍了如何生成使用JDT类级别的注释多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们正在使用JDT生成Java源$ C $ C。我们都卡在那里生成的类本身被注释为初级讲座类:
We are using JDT for generating java source code. We are stuck in generating a class where the class itself is annotated as belows:
@SomeAnnotation({Class1.class, Class2.class})
请让我知道如何可以做到这一点。我使用NormalAnnotation类这一点,但不能设置相应的前pression。虽然字符串字面可以设置,但类不能。
Please let me know how this can be achieved. I am using NormalAnnotation class for this but could not set the expression accordingly. Though String literals can be set but Class cannot be.
推荐答案
我能够做到这一点使用NormalAnnotation类,这里是code:
I was able to do this using NormalAnnotation class, here is the code:
//值包含了类名。
NormalAnnotation normalAnnotation = ast.newNormalAnnotation();
Name name = ast.newName(annotationName);
normalAnnotation.setTypeName(name);
ArrayInitializer arrayInit = ast.newArrayInitializer();
for(String value : values){
TypeLiteral tL = ast.newTypeLiteral();
tL.setType(ast.newSimpleType(ast.newName(value)));
arrayInit.expressions().add(tL);
}
MemberValuePair memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
memberValuePair.setValue(arrayInit);
normalAnnotation.values().add(memberValuePair);
这篇关于如何生成使用JDT类级别的注释多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文