B
Brendan Guild
I am wondering when it is safe to mention a class that I know will be
impossible to load. Specifically, I am writing a Java library and
there is another library that has some very useful features, but I
don't expect that other library to always be available. I want my
library to use classes that are available while I am compiling, but I
don't want to get a NoClassDefFoundError when those classes are not
available.
These classes are not critical to what I am trying to do, so I don't
want to crash just because they are absent. A NoClassDefFoundError is
fine, but only if I try to call a method of a class that cannot be
loaded. I don't want to get an error just because I mention the
class.
I have been studying chapter 12 of the Java Language Specification
but it seems unclear on this issue. It says:
"An implementation may resolve symbolic references from a class or
interface that is being linked very early, even to the point of
resolving all symbolic references from the classes and interfaces
that are further referenced, recursively. (This resolution may result
in errors from these further loading and linking steps.)"
That seems bad for me. It seems to say that a class might be loaded
at any time if it is at all mentioned. But on the other hand, it also
says,
"The only requirement on when resolution is performed is that any
errors detected during resolution must be thrown at a point in the
program where some action is taken by the program that might,
directly or indirectly, require linkage to the class or interface
involved in the error."
I'm not entirely certain, but this seems to be saying that even if a
class is loaded early, the loading cannot cause an error until the
class is actually used. However, I don't know for certain what
requires linkage. What does it mean to indirectly require linkage?
I want to do this in a way that is guaranteed to be correct by Java,
not just a way that happens to work at the whim of the JVM that I am
using.
impossible to load. Specifically, I am writing a Java library and
there is another library that has some very useful features, but I
don't expect that other library to always be available. I want my
library to use classes that are available while I am compiling, but I
don't want to get a NoClassDefFoundError when those classes are not
available.
These classes are not critical to what I am trying to do, so I don't
want to crash just because they are absent. A NoClassDefFoundError is
fine, but only if I try to call a method of a class that cannot be
loaded. I don't want to get an error just because I mention the
class.
I have been studying chapter 12 of the Java Language Specification
but it seems unclear on this issue. It says:
"An implementation may resolve symbolic references from a class or
interface that is being linked very early, even to the point of
resolving all symbolic references from the classes and interfaces
that are further referenced, recursively. (This resolution may result
in errors from these further loading and linking steps.)"
That seems bad for me. It seems to say that a class might be loaded
at any time if it is at all mentioned. But on the other hand, it also
says,
"The only requirement on when resolution is performed is that any
errors detected during resolution must be thrown at a point in the
program where some action is taken by the program that might,
directly or indirectly, require linkage to the class or interface
involved in the error."
I'm not entirely certain, but this seems to be saying that even if a
class is loaded early, the loading cannot cause an error until the
class is actually used. However, I don't know for certain what
requires linkage. What does it mean to indirectly require linkage?
I want to do this in a way that is guaranteed to be correct by Java,
not just a way that happens to work at the whim of the JVM that I am
using.