Skip to content

Support Custom Classloader #306

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 2 commits 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
4 changes: 3 additions & 1 deletion validator/src/main/java/io/avaje/validation/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ interface Builder {
/** Adds additional Locales for this validator */
Builder addLocales(Locale... locales);

/** Set the ClassLoader to use when loading adapters. */
Builder classLoader(ClassLoader classLoader);

/**
* Contract for obtaining the Clock used as the reference for now when validating the
* {@code @Future} and {@code @Past} constraints.
Expand Down Expand Up @@ -172,7 +175,6 @@ interface Builder {
* Build and return the Validator instance with all the given adapters and factories registered.
*/
Validator build();

}

/** Function to build a ValidationAdapter from a Validation Context */
Expand Down
15 changes: 11 additions & 4 deletions validator/src/main/java/io/avaje/validation/core/DValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
/** Default implementation of Validator. */
final class DValidator implements Validator, ValidationContext {

private static final ExtensionLoader SPI_LOADER = new ExtensionLoader();
private final CoreAdapterBuilder builder;
private final Map<Type, ValidationType<?>> typeCache = new ConcurrentHashMap<>();
private final MessageInterpolator interpolator;
Expand Down Expand Up @@ -188,6 +187,7 @@ static final class DBuilder implements Validator.Builder {
private final List<String> bundleNames = new ArrayList<>();
private final List<ResourceBundle> bundles = new ArrayList<>();
private final List<Locale> otherLocales = new ArrayList<>();
private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
private Locale defaultLocale = Locale.getDefault();
private Supplier<Clock> clockSupplier = DEFAULT_CLOCK;
private Duration temporalTolerance = ZERO;
Expand Down Expand Up @@ -256,6 +256,12 @@ public Builder addLocales(Locale... locals) {
return this;
}

@Override
public Builder classLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
return this;
}

@Override
public Builder clockProvider(Supplier<Clock> clockSupplier) {
this.clockSupplier = clockSupplier;
Expand All @@ -281,11 +287,12 @@ public Builder messageInterpolator(MessageInterpolator interpolator) {
}

private void registerComponents() {
ExtensionLoader.init(classLoader);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm ... not that keen on this lifecycle here

// first register all user defined ValidatorCustomizer
for (final ValidatorCustomizer next : SPI_LOADER.customizers()) {
for (final ValidatorCustomizer next : ExtensionLoader.customizers()) {
next.customize(this);
}
for (final GeneratedComponent next : SPI_LOADER.generatedComponents()) {
for (final GeneratedComponent next : ExtensionLoader.generatedComponents()) {
next.customize(this);
}
}
Expand All @@ -302,7 +309,7 @@ public Validator build() {
final var localeResolver = new LocaleResolver(defaultLocale, otherLocales);
final var interpolator =
Optional.ofNullable(this.userInterpolator)
.or(SPI_LOADER::interpolator)
.or(ExtensionLoader::interpolator)
.orElseGet(BasicMessageInterpolator::new);

return new DValidator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
import io.avaje.validation.spi.AnnotationFactory;
import io.avaje.validation.spi.GeneratedComponent;
import io.avaje.validation.spi.MessageInterpolator;
import io.avaje.validation.spi.ValidatorCustomizer;
import io.avaje.validation.spi.ValidationExtension;
import io.avaje.validation.spi.ValidatorCustomizer;

/** Load all the services using the common service interface. */
final class ExtensionLoader {

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();
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();

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

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

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

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

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

List<AnnotationFactory> annotationFactories() {
static List<AnnotationFactory> annotationFactories() {
return annotationFactories;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.avaje.validation.spi;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Marks source code that has been generated.
*/
/** Marks source code that has been generated. */
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface Generated {
Expand Down