MatteoJoliveau / PlugFace

Next generation Java general purpose plugin framework
https://plugface.matteojoliveau.com
MIT License
42 stars 11 forks source link

Use javax.inject.Named to specify dependencies by name #12

Open MatteoJoliveau opened 6 years ago

MatteoJoliveau commented 6 years ago

Since we want to be able to return multiple plugins having the same supertype, we need a way to specify a plugin for dependency injection via his name. Using javax.inject.Named can do the job.

Currently:

import javax.inject.Inject;

public class MyPlugin {

    @Inject
    public MyPlugin(Callable otherPlugin) { // I can't inject a second, different Callable
        // stuff
    }
}

Goal:

import javax.inject.Inject;
import javax.inject.Named;

public class MyPlugin {

    @Inject
    public MyPlugin(
        @Named("firstPlugin") 
        Callable firstPlugin, 
        @Named("secondPlugin") 
        Callable secondPlugin
    ) {
        // stuff
    }
}

The resolution will happen as follow:

  1. Check if a parameter has the @Named annotation. If so, retrieve the dependency by name. If not found, throw MissingDependencyException.
  2. If it doesn't have the annotation, check the parameter type. Retrieve the dependency by type. Following #11, a list will be returned. This list should contain only one element, otherwise, throw NoUniquePluginFoundException (new annotation).