- [ ] MINOR: why does "java -jar lib/ch.vorburger.opendaylight.simple.poc-1.5.0-SNAPSHOT.jar" not work? (MANIFEST.MF has all lib/*)
- [ ] create a Binding Generator? (reflecting upon annotated classes)
-- [ ] [re-implement ClassPathScanner](https://github.com/vorburger/opendaylight-simple/pull/18#issuecomment-426859615) using [ClassGraph](https://github.com/classgraph/classgraph) used in [INFRAUTILS-52](https://jira.opendaylight.org/browse/INFRAUTILS-52) (and rename it to something more appropriate)
+- [X] [re-implement ClassPathScanner](https://github.com/vorburger/opendaylight-simple/pull/18#issuecomment-426859615) using [ClassGraph](https://github.com/classgraph/classgraph) used in [INFRAUTILS-52](https://jira.opendaylight.org/browse/INFRAUTILS-52) (and rename it to something more appropriate)
- [ ] read YANG XML configuration files using [DataStoreAppConfigDefaultXMLReader](https://git.opendaylight.org/gerrit/#/c/76416/3/opendaylight/blueprint/src/test/java/org/opendaylight/controller/blueprint/tests/DataStoreAppConfigDefaultXMLReaderTest.java)
package org.opendaylight.infrautils.inject;
import com.google.inject.Binder;
+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.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
-import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClassPathScanner {
private static final Logger LOG = LoggerFactory.getLogger(ClassPathScanner.class);
- private final Map<Class, Class> implementations = new HashMap<>();
+ private final Map<String, Class> implementations = new HashMap<>();
/**
* Create a class path scanner, scanning packages with the given prefix.
* @param prefix The package prefix.
*/
public ClassPathScanner(String prefix) {
- Reflections reflections = new Reflections(prefix);
- Set<Class<?>> duplicateInterfaces = new HashSet<>();
- for (Class<?> singleton : reflections.getTypesAnnotatedWith(Singleton.class)) {
- for (Class<?> declaredInterface : singleton.getInterfaces()) {
- if (!duplicateInterfaces.contains(declaredInterface)) {
- if (implementations.put(declaredInterface, singleton) != null) {
- LOG.debug("{} is declared multiple times, ignoring it", declaredInterface);
- implementations.remove(declaredInterface);
- duplicateInterfaces.add(declaredInterface);
+ try (ScanResult scanResult =
+ new ClassGraph()
+ .enableClassInfo()
+ .enableAnnotationInfo()
+ .whitelistPackages(prefix)
+ .scan()) {
+ Set<String> duplicateInterfaces = new HashSet<>();
+ for (ClassInfo singletonInfo : scanResult.getClassesWithAnnotation(Singleton.class.getName())) {
+ for (ClassInfo interfaceInfo : singletonInfo.getInterfaces()) {
+ String interfaceName = interfaceInfo.getName();
+ if (!duplicateInterfaces.contains(interfaceName)) {
+ if (implementations.put(interfaceName, singletonInfo.loadClass()) != null) {
+ LOG.debug("{} is declared multiple times, ignoring it", interfaceName);
+ implementations.remove(interfaceName);
+ duplicateInterfaces.add(interfaceName);
+ }
}
}
}
@SuppressWarnings("unchecked")
private void bindImplementationFor(Binder binder, Class requestedInterface) {
- Class implementation = implementations.get(requestedInterface);
+ Class implementation = implementations.get(requestedInterface.getName());
if (implementation != null) {
binder.bind(requestedInterface).to(implementation);
for (Constructor constructor : implementation.getDeclaredConstructors()) {