Add bind2ToInstance to AbstractGuiceJsr250Module
authorMichael Vorburger <vorburger@redhat.com>
Thu, 1 Jun 2017 05:01:02 +0000 (07:01 +0200)
committerMichael Vorburger <vorburger@redhat.com>
Mon, 12 Jun 2017 15:25:30 +0000 (15:25 +0000)
This is already used in ElanServiceTestModule, and also needed in
FibManagerTestModule, so instead of copy/pasting it, being move up
refactored here.

Change-Id: I3b5e11236c1e756ba98fbe9ee10b1dcfe9dd6f98
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/AbstractGuiceJsr250Module.java
inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/GuiceRule.java

index 46f3a5a7a880b5039082a580e15bdd73f8f7b5a7..5f1d2da49fa59551f59c7187afb37f656884f086 100644 (file)
@@ -34,4 +34,23 @@ public abstract class AbstractGuiceJsr250Module extends AbstractModule {
 
     protected abstract void configureBindings() throws Exception;
 
+    /**
+     * Binds instance to both the interfaceClass as well as the implementationClass.
+     *
+     * @param interfaceClass class type of an interface
+     * @param implementationClass class type of implementing class
+     * @param instance an instance implementing both interfaceClass &amp; implementationClass
+     * @param <T> type of interfaceClass
+     */
+    @SuppressWarnings("unchecked")
+    protected <T> void bindTypesToInstance(Class<T> interfaceClass, Class<? extends T> implementationClass,
+            T instance) {
+        if (implementationClass.equals(interfaceClass)) {
+            throw new IllegalArgumentException("interfaceClass should not be the same as implementationClass: "
+                    + interfaceClass + "; " + implementationClass);
+        }
+        bind(interfaceClass).toInstance(instance);
+        bind((Class<T>) implementationClass).toInstance(instance);
+    }
+
 }
index 21cb74e47392191986c6e7b08c439627c408b06c..4f7fea921cb3a99ef47a20b17b0a372d355d1fd3 100644 (file)
@@ -39,7 +39,7 @@ public class GuiceRule implements MethodRule {
      * We do this to avoid having to declare bindings of Listeners asEagerSingleton(),
      * because in typical OpenDaylight projects there are Listener classes which are not @Inject,
      * but must still be created (so that they're registered).
-     * @see <a href="https://github.com/google/guice/wiki/Bootstrap">Guice documentation</a>.
+     * See <a href="https://github.com/google/guice/wiki/Bootstrap">Guice documentation</a>.
      */
     protected static final Stage DEFAULT_STAGE = Stage.PRODUCTION;