Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassLoader order is broken #54

Open
Aklakan opened this issue Nov 23, 2016 · 0 comments
Open

ClassLoader order is broken #54

Aklakan opened this issue Nov 23, 2016 · 0 comments

Comments

@Aklakan
Copy link

Aklakan commented Nov 23, 2016

By default, JCL uses the local loader for loading classes. Hence, JCL will by default always prefer its own loader to load classes from the jar. However, this makes it impossible for the application code to pass arguments to dynamically loaded classes, as the arguments use the application's class loader, where as the dynamic class' method parameter types use the JCL class loader:

Let's assume these classes are both in the application code and the imported Jar:

class Engine { }
interface Car { void setEngine(Engine engine); }

Then this does not work:

JarClassLoader jcl = new JarClassLoader();
jcl.add("fancyCars.jar"); // Contains class FancyCar implements Car {}
Object o = factory.create(jcl, "FancyCar");
Car car = JclUtils.cast(o, Car.class);

Engine engine = new DefaultEngine(); // Create some engine

// This statement breaks:
car.setEngine(engine);

// Reason:
// engine.getClass().getClassLoader() uses the application's class loader, whereas
// the Engine class that appears as the parameter type of FancyCar::setEngine is loaded via JCL

Per documenation, a work around should be to simply use the local class loader last, however this leads to another required workaround:

jcl.getLocalLoader().setOrder(100);

// This is a workaround to force updating the internal order of loaders;
// as of jcl 2.9-SNAPSHOT, there is no e.g. jcl.updateLoaderOrder() method
// I suggest to introduce it.

jcl.addLoader(jcl.getLocalLoader()); // Adding a loader updates the order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant