解决静态方法调用的DeclaredType泛型

时间:2019-03-15 13:12:46

标签: java annotations annotation-processing

我正在编写一个注释处理器,该处理器需要生成调用诸如Collectors.toList()(完整签名:public static <I> Collector<I, ?, List<I>> toList())之类的静态方法的代码

示例输入:

class Pojo<X> {
  public @CollectWith("Collectors.toList()") List<Thing<X>> things;
}

从上面生成的代码片段:

Collector<Thing<X>, ?, List<Thing<X>>> thingsCollector = Collectors.toList();
  • 方法调用的基本返回类型始终为Collector<T,A,R>
  • 从调用上下文中,代码知道R(上面是List<Thing<X>>
  • 类型变量TA(和I)未知

拼图中的部分

// Lookup specified Collector method
TypeElement collectors = elements.getTypeElement("java.util.stream.Collectors");
ExecutableElement toListMethod = findMethod(collectors, "toList"); 
DeclaredType toListMethodReturnType = (DeclaredType)toListMethod.getReturnType(); // java.util.stream.Collector<T,?,java.util.List<T>>

// R is already known.
DeclaredType R = ... // java.util.List<Thing<X>>

Q:是否有任何代码可以解决缺失的类型变量,并且可以将DeclaredType中的thingsCollector计算为Collector<Thing<X>, ?, List<Thing<X>>>

0 个答案:

没有答案