Skip to content

Commit 215b271

Browse files
committed
replace deprecated classscanner
1 parent a8bb5cf commit 215b271

4 files changed

Lines changed: 61 additions & 19 deletions

File tree

pom.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@
4242
<artifactId>lombok</artifactId>
4343
<optional>true</optional>
4444
</dependency>
45-
46-
<!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
45+
4746
<!-- used to scan the classpath -->
47+
<!-- https://mvnrepository.com/artifact/io.github.classgraph/classgraph -->
4848
<dependency>
49-
<groupId>org.reflections</groupId>
50-
<artifactId>reflections</artifactId>
51-
<version>0.10.2</version>
49+
<groupId>io.github.classgraph</groupId>
50+
<artifactId>classgraph</artifactId>
51+
<version>4.8.175</version>
5252
</dependency>
5353

54+
5455
<!-- Validation -->
5556
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
5657
<dependency>
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
package it.aboutbits.springboot.toolbox.boot.persistence;
22

3+
import it.aboutbits.springboot.toolbox.reflection.util.ClassScannerUtil;
34
import lombok.SneakyThrows;
45
import org.hibernate.boot.model.TypeContributions;
56
import org.hibernate.boot.model.TypeContributor;
67
import org.hibernate.service.ServiceRegistry;
78
import org.hibernate.type.descriptor.java.JavaType;
8-
import org.reflections.Reflections;
9-
import org.reflections.util.ConfigurationBuilder;
109

1110
import java.lang.reflect.InvocationTargetException;
1211
import java.util.Set;
1312

1413
public abstract class AbstractCustomTypeContributor implements TypeContributor {
15-
private final Reflections reflections;
14+
private final ClassScannerUtil.ClassScanner classScanner;
1615

1716
protected AbstractCustomTypeContributor(String... packageNames) {
18-
var packageToScan = new ConfigurationBuilder().forPackages(packageNames);
19-
reflections = new Reflections(packageToScan);
17+
classScanner = ClassScannerUtil.getScannerForPackages(packageNames);
2018
}
2119

2220
@SneakyThrows({InstantiationException.class, IllegalAccessException.class, InvocationTargetException.class})
2321
@Override
2422
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
25-
var types = findAllTypes();
23+
var types = findAllRelevantTypes();
2624

2725
for (var type : types) {
2826
typeContributions.contributeJavaType(
2927
(JavaType<?>) type.getConstructors()[0].newInstance()
3028
);
3129
}
30+
31+
classScanner.close();
3232
}
3333

3434
@SuppressWarnings("rawtypes")
35-
private Set<Class<? extends AutoRegisteredJavaType>> findAllTypes() {
36-
return reflections.getSubTypesOf(AutoRegisteredJavaType.class);
35+
private Set<Class<? extends AutoRegisteredJavaType>> findAllRelevantTypes() {
36+
return classScanner.getSubTypesOf(AutoRegisteredJavaType.class);
3737
}
3838
}

src/main/java/it/aboutbits/springboot/toolbox/boot/type/CustomTypeConfiguration.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package it.aboutbits.springboot.toolbox.boot.type;
22

3+
import it.aboutbits.springboot.toolbox.reflection.util.ClassScannerUtil;
34
import it.aboutbits.springboot.toolbox.type.CustomType;
45
import it.aboutbits.springboot.toolbox.type.jackson.CustomTypeDeserializer;
56
import it.aboutbits.springboot.toolbox.type.jackson.CustomTypeSerializer;
67
import it.aboutbits.springboot.toolbox.type.mvc.CustomTypePropertyEditor;
78
import jakarta.annotation.PostConstruct;
89
import lombok.extern.slf4j.Slf4j;
9-
import org.reflections.Reflections;
10-
import org.reflections.util.ConfigurationBuilder;
1110
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
1211
import org.springframework.context.annotation.Bean;
1312
import org.springframework.context.annotation.Configuration;
@@ -26,7 +25,7 @@ public class CustomTypeConfiguration {
2625
public static final String LIBRARY_BASE_PACKAGE_NAME = "it.aboutbits.springboot.toolbox";
2726

2827
private String[] packageNamesToScan;
29-
private Reflections reflections;
28+
private ClassScannerUtil.ClassScanner classScanner;
3029

3130
public void setAdditionalTypePackages(String[] additionalTypePackages) {
3231
var tmp = new ArrayList<String>();
@@ -35,8 +34,7 @@ public void setAdditionalTypePackages(String[] additionalTypePackages) {
3534

3635
this.packageNamesToScan = tmp.toArray(new String[0]);
3736

38-
var packageToScan = new ConfigurationBuilder().forPackages(packageNamesToScan);
39-
reflections = new Reflections(packageToScan);
37+
classScanner = ClassScannerUtil.getScannerForPackages(packageNamesToScan);
4038
}
4139

4240
@PostConstruct
@@ -64,6 +62,7 @@ public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
6462
.toList()
6563
.toArray(new CustomTypeDeserializer[types.size()]);
6664

65+
classScanner.close();
6766

6867
return builder -> builder
6968
.serializers(new CustomTypeSerializer())
@@ -72,7 +71,7 @@ public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
7271

7372
@SuppressWarnings("rawtypes")
7473
private Set<Class<? extends CustomType>> findAllCustomTypeRecords() {
75-
return reflections.getSubTypesOf(CustomType.class).stream()
74+
return classScanner.getSubTypesOf(CustomType.class).stream()
7675
.filter(Record.class::isAssignableFrom)
7776
.collect(Collectors.toSet());
7877
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package it.aboutbits.springboot.toolbox.reflection.util;
2+
3+
import io.github.classgraph.ClassGraph;
4+
import io.github.classgraph.ScanResult;
5+
import lombok.NonNull;
6+
7+
import java.util.Set;
8+
import java.util.stream.Collectors;
9+
10+
public final class ClassScannerUtil {
11+
private ClassScannerUtil() {
12+
}
13+
14+
public static ClassScanner getScannerForPackages(String... packages) {
15+
return new ClassScanner(packages);
16+
}
17+
18+
public static final class ClassScanner {
19+
private final ScanResult scanResult;
20+
21+
private ClassScanner(String... packages) {
22+
try (var result = new ClassGraph()
23+
.enableAllInfo()
24+
.acceptPackages(packages)
25+
.scan()) {
26+
this.scanResult = result;
27+
}
28+
}
29+
30+
@SuppressWarnings("unchecked")
31+
public <T> Set<Class<? extends T>> getSubTypesOf(@NonNull Class<T> clazz) {
32+
return scanResult.getSubclasses(clazz).loadClasses()
33+
.stream()
34+
.map(item -> (Class<? extends T>) item)
35+
.collect(Collectors.toSet());
36+
}
37+
38+
public void close() {
39+
scanResult.close();
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)