我正在编写一个注释处理器,该处理器需要生成调用诸如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>>
)T
和A
(和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>>>
。