Skip to content

For custom ClassLoader support, use non-static ExtensionLoader #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions validator/src/main/java/io/avaje/validation/core/DValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,12 @@ public Builder messageInterpolator(MessageInterpolator interpolator) {
return this;
}

private void registerComponents() {
ExtensionLoader.init(classLoader);
private void registerComponents(ExtensionLoader loader) {
// first register all user defined ValidatorCustomizer
for (final ValidatorCustomizer next : ExtensionLoader.customizers()) {
for (final ValidatorCustomizer next : loader.customizers()) {
next.customize(this);
}
for (final GeneratedComponent next : ExtensionLoader.generatedComponents()) {
for (final GeneratedComponent next : loader.generatedComponents()) {
next.customize(this);
}
}
Expand All @@ -304,12 +303,13 @@ public Validator build() {
return DEFAULT;
}

registerComponents();
final var loader = new ExtensionLoader(classLoader);
registerComponents(loader);

final var localeResolver = new LocaleResolver(defaultLocale, otherLocales);
final var interpolator =
Optional.ofNullable(this.userInterpolator)
.or(ExtensionLoader::interpolator)
.or(loader::interpolator)
.orElseGet(BasicMessageInterpolator::new);

return new DValidator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
/** Load all the services using the common service interface. */
final class ExtensionLoader {

private static final List<GeneratedComponent> generatedComponents = new ArrayList<>();
private static final List<ValidatorCustomizer> customizers = new ArrayList<>();
private static final List<AdapterFactory> adapterFactories = new ArrayList<>();
private static final List<AnnotationFactory> annotationFactories = new ArrayList<>();
private static Optional<MessageInterpolator> interpolator = Optional.empty();
private final List<GeneratedComponent> generatedComponents = new ArrayList<>();
private final List<ValidatorCustomizer> customizers = new ArrayList<>();
private final List<AdapterFactory> adapterFactories = new ArrayList<>();
private final List<AnnotationFactory> annotationFactories = new ArrayList<>();
private Optional<MessageInterpolator> interpolator = Optional.empty();

static void init(ClassLoader classLoader) {
ExtensionLoader(ClassLoader classLoader) {
for (var spi : ServiceLoader.load(ValidationExtension.class, classLoader)) {
if (spi instanceof GeneratedComponent gc) {
generatedComponents.add(gc);
Expand All @@ -37,23 +37,23 @@ static void init(ClassLoader classLoader) {
}
}

static Optional<MessageInterpolator> interpolator() {
Optional<MessageInterpolator> interpolator() {
return interpolator;
}

static List<GeneratedComponent> generatedComponents() {
List<GeneratedComponent> generatedComponents() {
return generatedComponents;
}

static List<ValidatorCustomizer> customizers() {
List<ValidatorCustomizer> customizers() {
return customizers;
}

static List<AdapterFactory> adapterFactories() {
List<AdapterFactory> adapterFactories() {
return adapterFactories;
}

static List<AnnotationFactory> annotationFactories() {
List<AnnotationFactory> annotationFactories() {
return annotationFactories;
}
}