Using A Custom ClassLoader
Some companies use a custom ClassLoader
to, for example, speed up class loading. If you don’t replace Java’s default class loader (ClassLoader.getSystemClassLoader()
), you may have to make sure that the engines generated by AFC use your custom implementation.
This is because AFC’s engines are themselves class loaders which load the Java byte code generated by AFC’s spreadsheet compiler. To ensure that your class loader is used when the generated engine needs to access other classes, your class loader must be passed to the engine as a custom parent class loader.
To set the custom class loader for compiling engines, do as follows:
builder.setParentClassLoaderForEngine( myClassLoader ); SaveableEngine compiledEngine = builder.compile(); assertSame( myClassLoader, ((ClassLoader) compiledEngine).getParent() );
To set it for loading engines, you have to allocate a special configuration object for the engine loader. The code looks like this:
EngineLoader.Config cfg = new EngineLoader.Config(); cfg.parentClassLoader = myClassLoader; Engine loadedEngine = FormulaRuntime.loadEngine( cfg, new ByteArrayInputStream( bytes ) ); assertSame( myClassLoader, ((ClassLoader) loadedEngine).getParent() );
Warning
Please note that the cast from AFC’s Engine
to ClassLoader
is an implementation detail that you must not rely on. It is shown here only to let you see the current effect of setting the custom parent class loader.
In particular, AFC only guarantees that if you set the custom parent class loader, then it will be used as the parent class loader for any of class loader AFC might instantiate internally for an engine.