-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend] use spring class path scanning to get classes' subtypes
- Loading branch information
1 parent
3d923fa
commit 247c7bd
Showing
3 changed files
with
31 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
openbas-framework/src/main/java/io/openbas/utils/SubclassScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.openbas.utils; | ||
|
||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; | ||
import org.springframework.core.type.filter.AssignableTypeFilter; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class SubclassScanner { | ||
public static Set<Class<?>> getSubclasses(String basePackage, Class<?> clazz) { | ||
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); | ||
provider.addIncludeFilter(new AssignableTypeFilter(clazz)); | ||
return provider.findCandidateComponents(basePackage).stream() | ||
.map(beanDefinition -> { | ||
try { | ||
return Class.forName(beanDefinition.getBeanClassName()); | ||
} catch (ClassNotFoundException e) { | ||
return null; | ||
} | ||
}) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters