Class.getName() 的静态调用

static invocation of Class.getName()

我在 org.springframework.core 中的 NestedRuntimeException 中偶然发现了以下代码:

static {
    NestedExceptionUtils.class.getName();
}

有这样的块有什么用?

它将eagerly加载NestedExceptionUtilsclass以避免class加载程序死锁。报告了一个错误 (SPR-5607) "Non-trivial NestedRuntimeException.getMessage() can cause deadlocks on OSGi",这是同一问题的解决方案。

已编辑:

它也在源代码中作为注释提到。有关完整的源代码文档,请遵循 link。这里是NestedRuntimeExceptionclass.

的部分源码
static {
    // Eagerly load the NestedExceptionUtils class to avoid classloader deadlock
    // issues on OSGi when calling getMessage(). Reported by Don Brown; SPR-5607.
    NestedExceptionUtils.class.getName();
}