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

Check if a Plugin main class is a Guice module #540

Open
dentmaged opened this issue Jul 10, 2021 · 1 comment
Open

Check if a Plugin main class is a Guice module #540

dentmaged opened this issue Jul 10, 2021 · 1 comment
Labels
type: feature New feature or request

Comments

@dentmaged
Copy link

dentmaged commented Jul 10, 2021

At the moment in order to create your own bindings, you have to create a child injector. This isn't ideal (due to bugs such as google/guice#973 - injecting an Injector will inject the parent, not the child that the module was bound with).

I believe the logic in JavaPluginLoader#loadPlugin should be something similar to:

Class<?> mainClass = ((JavaVelocityPluginDescription) description).getMainClass();
Object instance;
if (Module.class.isAssignableFrom(mainClass)) {
    instance = mainClass.newInstance();

    Module[] tmp = new Module[modules.length + 1];
    System.arraycopy(modules, 0, tmp, 0, modules.length);
    tmp[modules.length] = (Module) instance;

    Guice.createInjector(tmp);
} else {
    Injector injector = Guice.createInjector(modules);
    instance = injector.getInstance(mainClass);
}

if (instance == null) {
    throw new IllegalStateException("Got nothing from injector for plugin " + description.getId());
}
((VelocityPluginContainer) container).setInstance(instance);
@astei
Copy link
Contributor

astei commented Jul 10, 2021

I actually disagree with your proposed solution - since Velocity injects all the dependencies that a plugin will receive from the proxy through the injector, the programming model is vulnerable to depending on dependencies that haven't been made available.

We might consider providing a @PluginModule annotation, and record classes with this annotation to be explicitly initialized (possibly with injection) and adding an instance of the module class to the injector Velocity will use to initialize the plugin main class though. I'm going to be marking this as an enhancement for a future Velocity release.

As a workaround, you could always create your own injector once you receive the resources provided by Velocity and inject the dependencies you need.

@astei astei added the type: feature New feature or request label Jul 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants