ClassValue緩存關於類的東西。 這裏是(在Lucene的5.0 AttributeSource.java)的代碼的一部分
/** a cache that stores all interfaces for known implementation classes for performance (slow reflection) */
private static final ClassValue<Class<? extends Attribute>[]> implInterfaces = new ClassValue<Class<? extends Attribute>[]>() {
@Override
protected Class<? extends Attribute>[] computeValue(Class<?> clazz) {
final Set<Class<? extends Attribute>> intfSet = new LinkedHashSet<>();
// find all interfaces that this attribute instance implements
// and that extend the Attribute interface
do {
for (Class<?> curInterface : clazz.getInterfaces()) {
if (curInterface != Attribute.class && Attribute.class.isAssignableFrom(curInterface)) {
intfSet.add(curInterface.asSubclass(Attribute.class));
}
}
clazz = clazz.getSuperclass();
} while (clazz != null);
@SuppressWarnings({"unchecked", "rawtypes"}) final Class<? extends Attribute>[] a =
intfSet.toArray(new Class[intfSet.size()]);
return a;
}
};
閱讀您鏈接到API建議對我說,'得到()'方法是線索的最佳場所。然而,我不明白爲什麼'get()'方法有一個'Class'對象。 – Raedwald
目前關於core-libs郵件列表(http://mail.openjdk.java.net/pipermail/mlvm-dev/2013-April/005321.html)的討論關於Groovy中ClassValue的使用及其與班級卸貨。 –