AFC - Abacus Formula Compiler for Java

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();
ComputationFactory compiledFactory = compiledEngine.getComputationFactory();
assertSame( myClassLoader, compiledFactory.getClass().getClassLoader().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 ) );
ComputationFactory loadedFactory = loadedEngine.getComputationFactory();
assertSame( myClassLoader, loadedFactory.getClass().getClassLoader().getParent() );