Skip to content

Commit a2ff8a6

Browse files
authored
Merge pull request #1843 from steve-community/1838-regression-abstracttypestore-is-empty-after-spring-boot-migration
Regression: AbstractTypeStore is empty after spring boot migration
2 parents 8b482fd + 452a22f commit a2ff8a6

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/main/java/de/rwth/idsg/steve/ocpp/ws/AbstractTypeStore.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
package de.rwth.idsg.steve.ocpp.ws;
2020

2121
import com.google.common.base.Preconditions;
22-
import com.google.common.collect.ImmutableSet;
23-
import com.google.common.reflect.ClassPath;
2422
import de.rwth.idsg.ocpp.jaxb.RequestType;
2523
import de.rwth.idsg.ocpp.jaxb.ResponseType;
2624
import de.rwth.idsg.steve.ocpp.ws.data.ActionResponsePair;
25+
import org.springframework.beans.factory.config.BeanDefinition;
26+
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
27+
import org.springframework.core.type.filter.RegexPatternTypeFilter;
2728

28-
import java.io.IOException;
2929
import java.util.HashMap;
3030
import java.util.Map;
31+
import java.util.regex.Pattern;
3132

3233
/**
3334
* @author Sevket Goekay <[email protected]>
@@ -87,25 +88,36 @@ private void populateActionResponseMap(String packageName) {
8788
}
8889

8990
/**
91+
* https://stackoverflow.com/a/21430849
9092
* @return <simple name of class, class>
9193
*/
9294
@SuppressWarnings("unchecked")
9395
private static <INTERFACE, IMPL extends INTERFACE> Map<String, Class<IMPL>> getClassesWithInterface(
9496
String packageName, Class<INTERFACE> interfaceClass) {
9597
try {
96-
ImmutableSet<ClassPath.ClassInfo> classInfos =
97-
ClassPath.from(Thread.currentThread().getContextClassLoader())
98-
.getTopLevelClasses(packageName);
98+
// create scanner and disable default filters (that is the 'false' argument)
99+
var provider = new ClassPathScanningCandidateComponentProvider(false);
100+
// add include filters which matches all the classes (or use your own)
101+
provider.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*")));
102+
// get matching classes defined in the package
103+
var classes = provider.findCandidateComponents(packageName);
99104

100105
Map<String, Class<IMPL>> map = new HashMap<>();
101-
for (ClassPath.ClassInfo classInfo : classInfos) {
102-
Class<?> clazz = classInfo.load();
106+
107+
// this is how you can load the class type from BeanDefinition instance
108+
for (BeanDefinition bean: classes) {
109+
Class<?> clazz = Class.forName(bean.getBeanClassName());
103110
if (interfaceClass.isAssignableFrom(clazz)) {
104111
map.put(clazz.getSimpleName(), (Class<IMPL>) clazz);
105112
}
106113
}
114+
115+
if (map.isEmpty()) {
116+
throw new IllegalStateException("No classes with interface " + interfaceClass.getSimpleName() + " found in package " + packageName);
117+
}
118+
107119
return map;
108-
} catch (IOException e) {
120+
} catch (ClassNotFoundException e) {
109121
throw new RuntimeException(e);
110122
}
111123
}

0 commit comments

Comments
 (0)