Re: Reference to a class that does not exist
Steven Simpson wrote in news:f4e3g8$sig$1@news.freedom2surf.net:
You're concerned that the link error might be allowed to occur so
early as to prevent an application, which is using your library,
from running, right?
Right.
In your library, when you need to do this operation (or to
determine whether it can be done), identify the class by String:
EdgeDetector ed = null;
try {
Class edc = Class.forName("SomeLibEdgeDetector");
ed = edc.newInstance();
} catch ( various exceptions... ) {
// Ignore.
}
I see that we are completely hiding every mention of the classes that
might not be available so that they cannot cause a problem unless
they are actually needed. I have no doubt that would work, thanks.
However, I was hoping that it wouldn't have to come to something so
radical. My problem has three parts: an application which I'm not
programming, a library A which I am making myself, and a library B
which may or may not be there when the application is running. The
application may or may not have dependencies upon library B. The
problem is that if the application does not depend upon B then
library A should not cause an error, but if the application does
depend upon B then the application programmer will want library A to
supply references to classes of library B.
Library B is a graphics library other than the AWT. My library
creates a special widget. Suppose 'Widget' is a class from library B
that might or might not be loadable at run time. I am hoping it is
okay to let Java load an interface like this:
public interface WidgetMaker {
Widget makeWidget();
}
I promise that this is the only way that I will mention 'Widget'. The
implementation of makeWidget() will be entirely hidden behind a
Class.forName() call until it is needed. Surely a mere reference like
that should not require linking. Can I trust Java to never throw an
error simply because of the word 'Widget' when it tries to load
WidgetMaker?
I know I could get around this issue by having makeWidget() return an
Object, but I don't want to have to explain in the documentation why
a cast is required.