![](/img/trans.png)
[英]Spring Kafka "cannot access com.fasterxml.jackson.core.type.TypeReference"
[英]why com.fasterxml.jackson.core.type.TypeReference implements Comparable interface?
為什么 com.fasterxml.jackson.core.type.TypeReference 實現 Comparable 接口? 我不明白實施它的意圖。
public abstract class TypeReference<T> implements Comparable<TypeReference<T>>
{
protected final Type _type;
protected TypeReference()
{
Type superClass = getClass().getGenericSuperclass();
if (superClass instanceof Class<?>) { // sanity check, should never happen
throw new IllegalArgumentException("Internal error: TypeReference constructed without actual type information");
}
/* 22-Dec-2008, tatu: Not sure if this case is safe -- I suspect
* it is possible to make it fail?
* But let's deal with specific
* case when we know an actual use case, and thereby suitable
* workarounds for valid case(s) and/or error to throw
* on invalid one(s).
*/
_type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
}
public Type getType() { return _type; }
/**
* The only reason we define this method (and require implementation
* of <code>Comparable</code>) is to prevent constructing a
* reference without type information.
*/
@Override
public int compareTo(TypeReference<T> o) { return 0; }
// just need an implementation, not a good one... hence ^^^
}
這是源代碼描述,但我仍然無法理解。
TL;DR:它什么都不做,是一個錯誤。
你讀過源代碼嗎? 因為它很好地解釋了它。
具體來說,它鏈接到Neal Gafter 關於 Super Type Tokens 的帖子,並明確解釋 Comparable 的東西是“受評論啟發”的。 所以,打開那個鏈接,向下滾動到評論,果然:
Brian Oxley 說...與其在構造函數中檢查類型參數,不如將其設為語法錯誤而忽略:
public abstract TypeReference<R> implements Comparable<TypeReference<R>> { //... public int compareTo(TypeReference<R> o) { // Need a real implementation. // Only saying "return 0" for illustration. return 0; } }
現在這是合法的:
new TypeReference<Object>() { };
但這是未實現
Comparable
的語法錯誤:
new TypeReference() { }
;2007 年 2 月 20 日上午 6 點 12 分
然而,這是不正確的。
它實際上什么也沒做。 在這里,讓我們嘗試一下:
> cat Test.java
import java.lang.reflect.*;
abstract class TypeReference<T> implements Comparable<TypeReference<T>> {
protected final Type _type;
protected TypeReference() {
Type superClass = getClass().getGenericSuperclass();
if (superClass instanceof Class<?>) { // sanity check, should never happen
throw new IllegalArgumentException("Internal error: TypeReference constructed without actual type information");
}
_type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
}
@Override public int compareTo(TypeReference<T> o) { return 0; }
public static void main(String[] args) {
TypeReference raw = new TypeReference() {};
}
}
> javac Test.java
看? 編譯良好。 如果你運行它,你會得到“健全性檢查”異常。 比較器的東西什么都不做。
編輯:
我在 jackson 提交了一個關於問題 #697的錯誤。
試圖解釋這一切背后的想法是什么(即使它不起作用):
原始類型具有傳染性,與它相關的所有方面也是原始的。 原始類型以獨特的方式行為,但通常這些方式與所有類型變量的Object
非常相似,即使存在下限。
因此, new TypeReference() {}
是原始的,意味着它實現的Comparable
也是原始的,就像 Object 一樣,這意味着 impl 必須具有方法public int compareTo(Object o)
。
提供的TypeReference
實現沒有int compareTo(Object)
,它只有compareTo(TypeReference)
,因此new TypeReference() {}
不會編譯。
一個不錯的故事。 但不幸的是,這不是 javac 的工作方式。 您可以看到邏輯並且它是有道理的,但不幸的是compareTo
,因為它與下限匹配,因此獲得了一個合成的附加方法。 你可以檢查這個:如果你javap
TypeReference:
> javap TypeReference
javap TypeReference
Compiled from "Test.java"
abstract class TypeReference<T> implements java.lang.Comparable<TypeReference<T>> {
protected final java.lang.reflect.Type _type;
protected TypeReference();
public int compareTo(TypeReference<T>);
public static void main(java.lang.String[]);
public int compareTo(java.lang.Object);
請注意 class 文件中有兩個compareTo
方法,即使我們在源代碼中只寫了一個。 因此,任何extends TypeReference
的類都將始終包含一個用於compareTo(Object)
的 impl,無論您嘗試使用其中的 generics 部分來破解什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.