remove ClassPathScanner explicit bindings
authorMichael Vorburger <mike@vorburger.ch>
Wed, 19 Dec 2018 00:09:47 +0000 (01:09 +0100)
committerMichael Vorburger <mike@vorburger.ch>
Wed, 1 Jul 2020 23:37:54 +0000 (01:37 +0200)
Signed-off-by: Michael Vorburger <mike@vorburger.ch>
src/main/java/org/opendaylight/infrautils/inject/ClassPathScanner.java
src/main/java/org/opendaylight/infrautils/inject/guice/GuiceClassPathBinder.java
src/test/java/org/opendaylight/infrautils/inject/tests/ClassPathScannerTest.java

index 16ffa3c7b5f3720481d0ceb7397682df9d94d0c8..f9352382e5d50ff00365e98af3431e913c500f4c 100644 (file)
@@ -10,8 +10,6 @@ package org.opendaylight.infrautils.inject;
 import io.github.classgraph.ClassGraph;
 import io.github.classgraph.ClassInfo;
 import io.github.classgraph.ScanResult;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -86,35 +84,4 @@ public class ClassPathScanner {
         });
         // we do not want nor have to scan the @Singleton's @Inject annotated constructor; will also auto-discover.
     }
-
-    /**
-     * Binds the given interfaces, using implementations discovered by scanning the class path.
-     *
-     * @param binder The binder (modeled as a generic consumer).
-     * @param interfaces The requested interfaces.
-     */
-    public <T> void bind(BiConsumer<Class<T>, Class<? extends T>> binder, Class... interfaces) {
-        for (Class requestedInterface : interfaces) {
-            bindImplementationFor(binder, requestedInterface);
-        }
-        // TODO Perhaps return interfaces which weren’t bound?
-    }
-
-    @SuppressWarnings("unchecked")
-    private <T> void bindImplementationFor(BiConsumer<Class<T>, Class<? extends T>> binder, Class requestedInterface) {
-        Class implementation = implementations.get(requestedInterface.getName());
-        if (implementation != null) {
-            binder.accept(requestedInterface, implementation);
-            // TODO later probably lower this info to debug, but for now it's very useful..
-            LOG.info("Bound {} to {}", requestedInterface, implementation);
-            for (Constructor constructor : implementation.getDeclaredConstructors()) {
-                Annotation injectAnnotation = constructor.getAnnotation(Inject.class);
-                if (injectAnnotation != null) {
-                    for (Class parameterType : constructor.getParameterTypes()) {
-                        bindImplementationFor(binder, parameterType);
-                    }
-                }
-            }
-        }
-    }
 }
index e4cc74cc00becbb04c832943680079228d509602..73971581f3f58ee5250d542f8f8808efbbea35dc 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.infrautils.inject.guice;
 
 import com.google.inject.Binder;
+import javax.inject.Singleton;
 import org.opendaylight.infrautils.inject.ClassPathScanner;
 
 /**
@@ -21,15 +22,11 @@ public class GuiceClassPathBinder {
     }
 
     /**
-     * Binds the implementation of the given interface, if any, in the given binder, along with all dependencies.
+     * Binds all {@link Singleton} annotated classes discovered by scanning the class path to all their interfaces.
      *
+     * @param prefix the package prefix of Singleton implementations to consider
      * @param binder The binder to set up.
-     * @param requestedInterface The requested interface.
      */
-    public void bind(Binder binder, Class<?> requestedInterface) {
-        scanner.bind((contract, implementation) -> binder.bind(contract).to(implementation), requestedInterface);
-    }
-
     public void bindAllSingletons(String prefix, Binder binder) {
         scanner.bindAllSingletons(prefix, (contract, implementation) -> binder.bind(contract).to(implementation));
     }
index b3aea67ef7c82844e419ec74feb6a7cd96789341..f2cfb056f71f841e7ad12a02b848e2dbe21ad3de 100644 (file)
@@ -18,15 +18,6 @@ public class ClassPathScannerTest {
 
     private static final String PREFIX = "org.opendaylight.infrautils.inject.tests";
 
-    @Test
-    public void testExplicitBinding() {
-        Map<Class<?>, Class<?>> bindings = new HashMap<>();
-        new ClassPathScanner(PREFIX).bind(bindings::put,
-                ClassPathScannerTestTopInterface.class);
-        assertThat(bindings).containsExactly(
-                ClassPathScannerTestTopInterface.class, ClassPathScannerTestImplementation.class);
-    }
-
     @Test
     public void testImplicitBinding() {
         Map<Class<?>, Class<?>> bindings = new HashMap<>();