【问题标题】:Get concrete value of generic parameter during annotation processing在注释处理期间获取泛型参数的具体值
【发布时间】:2016-06-12 15:39:45
【问题描述】:

我有:

class Base<E> {
  E e;
  abstract void f(E other);
}

class Ext extends Base<String> {
  // HERE
}

我想在Ext 中生成f(E other) 的实现,但不知道如何告诉生成器E 是此类中的String。基类也可能是接口。 我有JCClassDecl,所以可以检查decl.type.extendingimplementing,但是这本手册很快就会变成一团糟,而且我目前所拥有的并不是普遍适用的,所以我想跳过发布错误的代码和以当前的一般形式提出问题。

【问题讨论】:

    标签: java metaprogramming javac annotation-processing


    【解决方案1】:

    啊,我明白了。花了一天的时间寻找答案,然后,在提出问题后,大约需要 30 分钟才能弄清楚。

    答案是:Types#memberType:

    getTypes().memberType(
      classDecl.sym.type,                // this is the extending JCClassDecl
      varDecl.vartype.type.tsym          // this is the parameter JCVarDecl
    )
    

    看到javac API 比较难掌握,希望对大家有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多