gpt4 book ai didi

java - 在注解处理过程中获取泛型参数的具体值

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:44 26 4
gpt4 key购买 nike

我有:

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

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

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

最佳答案

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

答案是:Types#memberType:

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

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

关于java - 在注解处理过程中获取泛型参数的具体值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35702270/

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