ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:1026回复:0

GenericTypeResolver.resolveTypeArguments returns null

楼主#
更多 发布于:2016-05-12 14:42
I have the following code: List strings = new ArrayList<String>();
strings.add("string");
System.out.println(GenericTypeResolver.resolveTypeArguments(String.class, strings.getClass()));
 It prints null. I cannot understand this method signature. I want to check that strings is parameterized with String. How do you use resolveTypeArguments properly ? P.S. My example just dummy. P.S.2 I know that spring can do something like this with Convertor interfaceThis utility is made to look up reified type parameters. What that means in this case is that the information must be available in the class itself. The first parameter is the class you're trying to resolve the types of. The second parameter is the superclass or interface the first class implements, which has generic type parameters. Here would be one example for Integer (which implements Comparable<Integer>): Class[] typeParams = GenericTypeResolver.resolveTypeArguments(
                            Integer.class, Comparable.class);
//typeParams would be [Integer.class]
 In your example, the type parameter of strings has not been reified (it has been erased) and so this utility will return null, even if you were to fix how you were calling it (which would be resolveTypeArguments(strings.getClass(), Collection.class))

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部