forked from public-mirrors/ConfigLib
Add SerializerContext interface
Instances of this interface contain information about the context in which a serializer was selected. They are passed to the constructors of custom serializers, if the serializer classes define such a constructor.dev
parent
c78f4b2901
commit
bb58025239
@ -0,0 +1,39 @@
|
||||
package de.exlll.configlib;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
|
||||
/**
|
||||
* Instances of this class provide contextual information for custom serializers.
|
||||
* <p>
|
||||
* Custom serializers classes are allowed to declare a constructor with one parameter of
|
||||
* type {@code SerializerContext}. If such a constructor exists, an instance of this class is
|
||||
* passed to it when the serializer is instantiated by this library.
|
||||
*/
|
||||
public interface SerializerContext {
|
||||
/**
|
||||
* Returns the {@code ConfigurationProperties} object in use when the serializer was selected.
|
||||
*
|
||||
* @return properties object in use when the serializer was selected
|
||||
*/
|
||||
ConfigurationProperties properties();
|
||||
|
||||
/**
|
||||
* Returns the {@code TypeComponent} (i.e. the field or record component) which led to the
|
||||
* selection of the serializer.
|
||||
*
|
||||
* @return component which led to the selection of the serializer
|
||||
*/
|
||||
TypeComponent<?> component();
|
||||
|
||||
/**
|
||||
* Returns the {@code AnnotatedType} which led to the selection of the serializer. The annotated
|
||||
* type returned by this method might be different from the one returned by
|
||||
* {@link TypeComponent#annotatedType()}. Specifically, the type is different when the
|
||||
* serializer is applied to a nested type via {@link SerializeWith} in which case the annotated
|
||||
* type represents the type at that nesting level.
|
||||
*
|
||||
* @return annotated type which led to the selection of the serializer
|
||||
*/
|
||||
AnnotatedType annotatedType();
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
package de.exlll.configlib;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
|
||||
import static de.exlll.configlib.Validator.requireNonNull;
|
||||
|
||||
record SerializerContextImpl(
|
||||
ConfigurationProperties properties,
|
||||
TypeComponent<?> component,
|
||||
AnnotatedType annotatedType
|
||||
) implements SerializerContext {
|
||||
SerializerContextImpl {
|
||||
properties = requireNonNull(properties, "configuration properties");
|
||||
component = requireNonNull(component, "type component");
|
||||
annotatedType = requireNonNull(annotatedType, "annotated type");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue