Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Commit

Permalink
Switch to ClassGraph
Browse files Browse the repository at this point in the history
Closes: #38
Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt authored and vorburger committed Oct 11, 2018
1 parent 32dc3c8 commit 5f80fa3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
- [ ] 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)

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@
</dependency>

<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.2.7</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
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;
Expand All @@ -16,7 +19,6 @@
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -33,23 +35,30 @@
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);
}
}
}
}
Expand All @@ -71,7 +80,7 @@ public void bind(Binder binder, Class... interfaces) {

@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()) {
Expand Down

0 comments on commit 5f80fa3

Please sign in to comment.