FXML and Classloading

Tom Schindl tom.schindl at bestsolution.at
Thu May 31 06:20:40 PDT 2012


Looks like this is a never ending story, when it comes to OSGi ;-) This
time it is the JavaFXBuilderFactory which as it looks like is NOT using
the classloader set using FXMLLoader#setClassloader() hence if one
defined a custom object (which requires constructor parameters) and a
builder - it is not found because the wrong classpath is used.

Now there's a JavaFXBuilderFactory which accepts a classloader but it is
not considered public API.

My feeling is though that the builder factory should use the classloader
of the type class passed as a first search path and only fallback to the
other one it is not found in there.

I think the logic should be something like this:

private Class<?> findBuilder(
  Classloader defaultClassloader,
  Class<?> objectClass)
  throws ClassNotFoundException {
   String builderName = objectClass.getName() + "Builder";
   try {
     return objectClass.getClassloader().loadClass(builderName);
   } catch( ClassNotFoundException e ) {
     // search only if classloaders are different
     if( objectClass.getClassloader() != defaultClassloader) {
       return defaultClassloader.loadClass(builderName);
     } else {
       throw e;

because the objectClass is already loaded through the classloader
defined on the FXMLLoader this fixes all problems of class mismatches, ... .

The other options are:
* provide a constructur on FXMLLoader that allows to pass the
  classloader and pass this one on to JavaFXBuilderFactory for
* make the JavaFXBuilderFactory-constructor that accepts a Classloader

My feeling is that using the object-types classloader is the most user
friendly and logic solution - though it has a small performance overhead
because 2 classloaders are searched potentially.


B e s t S o l u t i o n . a t                        EDV Systemhaus GmbH
tom schindl                 geschäftsführer/CEO
eduard-bodem-gasse 5-7/1   A-6020 innsbruck     fax      ++43 512 935833
http://www.BestSolution.at                      phone    ++43 512 935834

More information about the openjfx-dev mailing list