|
19 | 19 | package de.rwth.idsg.steve.ocpp.ws; |
20 | 20 |
|
21 | 21 | import com.google.common.base.Preconditions; |
22 | | -import com.google.common.collect.ImmutableSet; |
23 | | -import com.google.common.reflect.ClassPath; |
24 | 22 | import de.rwth.idsg.ocpp.jaxb.RequestType; |
25 | 23 | import de.rwth.idsg.ocpp.jaxb.ResponseType; |
26 | 24 | 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; |
27 | 28 |
|
28 | | -import java.io.IOException; |
29 | 29 | import java.util.HashMap; |
30 | 30 | import java.util.Map; |
| 31 | +import java.util.regex.Pattern; |
31 | 32 |
|
32 | 33 | /** |
33 | 34 | * @author Sevket Goekay <[email protected]> |
@@ -87,25 +88,36 @@ private void populateActionResponseMap(String packageName) { |
87 | 88 | } |
88 | 89 |
|
89 | 90 | /** |
| 91 | + * https://stackoverflow.com/a/21430849 |
90 | 92 | * @return <simple name of class, class> |
91 | 93 | */ |
92 | 94 | @SuppressWarnings("unchecked") |
93 | 95 | private static <INTERFACE, IMPL extends INTERFACE> Map<String, Class<IMPL>> getClassesWithInterface( |
94 | 96 | String packageName, Class<INTERFACE> interfaceClass) { |
95 | 97 | 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); |
99 | 104 |
|
100 | 105 | 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()); |
103 | 110 | if (interfaceClass.isAssignableFrom(clazz)) { |
104 | 111 | map.put(clazz.getSimpleName(), (Class<IMPL>) clazz); |
105 | 112 | } |
106 | 113 | } |
| 114 | + |
| 115 | + if (map.isEmpty()) { |
| 116 | + throw new IllegalStateException("No classes with interface " + interfaceClass.getSimpleName() + " found in package " + packageName); |
| 117 | + } |
| 118 | + |
107 | 119 | return map; |
108 | | - } catch (IOException e) { |
| 120 | + } catch (ClassNotFoundException e) { |
109 | 121 | throw new RuntimeException(e); |
110 | 122 | } |
111 | 123 | } |
|
0 commit comments