如何创建新的JCExpression以提供注释值

时间:2017-06-12 10:51:14

标签: java javac

我对javac编程有点新意,我想创建一个JCExpression来给出注释值。我搜遍了所有地方,但没有运气。

这是我到目前为止所尝试的

if (templates.size() > 0) {
 List < JCTree.JCAnnotation > annotations = templates;

 for (JCTree.JCAnnotation jcAnnotation: annotations) {
  JCTree.JCExpression jcExpression = new JCTree.JCExpression() {
   @Override public Tag getTag() {
    return null;
   }

   @Override public void accept(Visitor visitor) {

   }

   @Override public < R, D > R accept(TreeVisitor < R, D > treeVisitor, D d) {
    return null;
   }

   @Override public Kind getKind() {
    return null;
   }
  };

  List < JCTree.JCExpression > jcExpressions = jcAnnotation.getArguments();

  for (JCTree.JCExpression jcExpression1: jcExpressions) {
   jcExpression1 = jcExpression;
  }
 } 

请帮帮我!!!

1 个答案:

答案 0 :(得分:1)

自己找到解决方案

for (JCTree.JCAnnotation annotation: templates) {
 List < JCTree.JCExpression > expressionList = annotation.args;
 for (JCTree.JCExpression expression: expressionList) {

  expression.accept(new TreeTranslator() {
   @Override public void visitLiteral(JCTree.JCLiteral jcLiteral) {
    super.visitLiteral(jcLiteral);
    JCTree.JCLiteral val = treeMaker.Literal("<custom value>");
    jcLiteral.value = val.value;
   }

   @Override public void visitIdent(JCTree.JCIdent jcIdent) {
    super.visitIdent(jcIdent);
    <track the parameters>
   }
  });
 }
}