In an multi-layered application, a good thing would be to have a way for a caller to pass its (or maybe even a custom) Classloader instance to BXMLSerializer, so anytime a dynamic load of a class is needed, when that classloader is not null BXMLSerializer can use it instead of the default one (good for all standard cases).
Examples of Classloaders (for callers, to pass):
// ClassLoader classLoader = ClassLoader.getSystemClassLoader(); // clSystem
// ClassLoader classLoader = this.getClass().getClassLoader(); // clCurrent
// ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // clContext
Currently BXMLSerializer by default uses this:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Note that probably this is a good feature even in other places inside Pivot, whenever a dynamic load of a class has to be done, but this could be a later evolution (if needed here).
For example, I had a dedicated class for this, with the following methods (extract):
public DynamicLoader(String className, String variantName, int namingStrategy, boolean exceptionOnNull)
that was much more complex than needed here because I had to handle different naming strategies for class names.
In my custom bean factory OptionalVariantBeanFactory i was loading classes using using DynamicLoader, and before loading the requested class I tried to load the optional variant version (specific for the given customer code) and if not found try to load the default class.
Probably all this is too business-specific for inclusion in Pivot, but a simplified version of DynamicLoader could be useful (maybe in the future).
protected transient boolean initClassToLoad = true;
protected transient ClassLoader classloader = null;
public void useClassloaderEnhanced(boolean initClass, ClassLoader cl)
this was using to enable the "enhanced" classloading, so later (inside the real load method) I could use
if (classloader != null)
clazz = Class.forName(fullClassName, initClassToLoad, classloader);
clazz = Class.forName(fullClassName);
just as a sample ...