Skip to content

Commit 1308639

Browse files
authored
Avoid Class forName when on module-path (#335)
1 parent ad52bce commit 1308639

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

validator-http-plugin/src/main/java/io/avaje/validation/http/HttpValidatorProvider.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
package io.avaje.validation.http;
22

3-
import io.avaje.inject.BeanScopeBuilder;
4-
53
import java.util.ArrayList;
64
import java.util.Arrays;
75
import java.util.Locale;
86

7+
import io.avaje.http.api.Validator;
8+
import io.avaje.inject.BeanScopeBuilder;
9+
910
/**
1011
* Plugin for avaje inject that provides a default Http Validator instance.
1112
*/
1213
public final class HttpValidatorProvider implements io.avaje.inject.spi.InjectPlugin {
1314

14-
private static final Class<?> VALIDATOR_HTTP_CLASS = avajeHttpOnClasspath();
15-
16-
private static Class<?> avajeHttpOnClasspath() {
17-
try {
18-
return Class.forName("io.avaje.http.api.Validator");
19-
} catch (ClassNotFoundException e) {
20-
return null;
21-
}
15+
private static final boolean WIRE_VALIDATOR = avajeHttpOnClasspath();
16+
17+
private static boolean avajeHttpOnClasspath() {
18+
var modules = ModuleLayer.boot();
19+
return modules
20+
.findModule("io.avaje.validation.http")
21+
.map(m -> modules.findModule("io.avaje.http.api").isPresent())
22+
.orElseGet(() -> {
23+
try {
24+
return Validator.class != null;
25+
} catch (NoClassDefFoundError e) {
26+
return false;
27+
}
28+
});
2229
}
2330

2431
@Override
2532
public Class<?>[] provides() {
26-
return VALIDATOR_HTTP_CLASS == null ? new Class<?>[]{} : new Class<?>[]{VALIDATOR_HTTP_CLASS};
33+
return WIRE_VALIDATOR ? new Class<?>[] {Validator.class} : new Class<?>[] {};
2734
}
2835

2936
@Override
3037
public void apply(BeanScopeBuilder builder) {
31-
if (VALIDATOR_HTTP_CLASS == null) {
38+
if (!WIRE_VALIDATOR) {
3239
return;
3340
}
3441

35-
builder.provideDefault(null, VALIDATOR_HTTP_CLASS, () -> {
42+
builder.provideDefault(null, Validator.class, () -> {
3643
final var props = builder.configPlugin();
3744
final var locales = new ArrayList<Locale>();
3845

validator-inject-plugin/src/main/java/io/avaje/validation/inject/spi/DefaultValidatorProvider.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Locale;
99

1010
import io.avaje.inject.BeanScopeBuilder;
11+
import io.avaje.inject.aop.Aspect;
1112
import io.avaje.inject.aop.AspectProvider;
1213
import io.avaje.inject.spi.GenericType;
1314
import io.avaje.inject.spi.InjectPlugin;
@@ -22,12 +23,17 @@ public final class DefaultValidatorProvider implements InjectPlugin {
2223
private static final boolean WIRE_ASPECTS = aspectsOnClasspath();
2324

2425
private static boolean aspectsOnClasspath() {
25-
try {
26-
Class.forName("io.avaje.inject.aop.Aspect");
27-
return true;
28-
} catch (ClassNotFoundException e) {
29-
return false;
30-
}
26+
var modules = ModuleLayer.boot();
27+
return modules
28+
.findModule("io.avaje.validation.plugin")
29+
.map(m -> modules.findModule("io.avaje.inject.aop").isPresent())
30+
.orElseGet(() -> {
31+
try {
32+
return Aspect.class != null;
33+
} catch (NoClassDefFoundError e) {
34+
return false;
35+
}
36+
});
3137
}
3238

3339
@Override

0 commit comments

Comments
 (0)