英文:
Why do Java Anonymous Classes retain generic information at runtime?
问题
在我寻找关于在使用Jackson进行JSON(反)序列化的情境下如何在运行时保留有关通用类型信息的方法时,我发现确实可以使用TypeReference<T>
来实现。
然后,我想要理解它是如何以及为什么起作用的,因此我按照文档中的链接,链接在这里。
我理解,在某些情况下,例如使用匿名内部类,甚至在运行时也可以知道类的通用类型(我认为可以说类型擦除在这里并不完全适用),但是我不明白 为什么 :为什么要增加这个(相当晦涩)的特性,如果它只适用于这种特定情况的话?不要误会我的意思,拥有这个特性确实非常方便,但它为什么首先存在呢?
英文:
While I was looking for a way to retain information about generic types at runtime in the context of JSON (de)serialization with Jackson, I found out that it's indeed possible to do so using TypeReference<T>
.
I then wanted to understand how and why it works, so I followed the link in the documentation, which leads here.
I understand that there are some circumstances, such as with anonymous inner classes, where it is possible to know the generic type of a class even at runtime (I think it can be said the type erasure does not fully apply here), but I don't understand why : why bother adding this (fairly obscure) feature if it is only appliable to this particular case? Don't get me wrong, it is fairly handy to have it, but why does it exist in the first place?
专注分享java语言的经验与见解,让所有开发者获益!
评论