18

I stumbled upon the following code in NestedRuntimeException in org.springframework.core:

static {
    NestedExceptionUtils.class.getName();
}

What is the use of having such a block?

Dariusz
  • 21,561
  • 9
  • 74
  • 114
Novice User
  • 3,552
  • 6
  • 31
  • 56
  • 3
    My guess is that this is a trick to force the JVM class loader to load the class `NestedExceptionUtils`. But maybe there are other Spring specific reasons as well. – Tim Biegeleisen May 15 '18 at 05:59
  • 7
    I was wondering why there would be no comment on a line like this, so I looked up the source. Of course there is a comment right before that statement that pretty much explains it, it's just not included in the question: "Eagerly load the NestedExceptionUtils class to avoid classloader deadlock issues on OSGi when calling getMessage(). Reported by Don Brown; SPR-5607" – kapex May 15 '18 at 09:49

1 Answers1

27

It will eagerly load the NestedExceptionUtils class to avoid classloader deadlock. There was a bug reported (SPR-5607) "Non-trivial NestedRuntimeException.getMessage() can cause deadlocks on OSGi" and this is the solution for the same issue.

Edited:

It is mentioned also in the source code as a comment. For full source code docs please follow the link. Here is the part of the source code of NestedRuntimeException class.

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();
}
Amit Bera
  • 7,075
  • 1
  • 19
  • 42