|
|
@ -67,7 +67,7 @@ sealed abstract class TypeSerializer<T, E extends ConfigurationElement<?>>
|
|
|
|
if ((elementValue == null) && !properties.outputNulls())
|
|
|
|
if ((elementValue == null) && !properties.outputNulls())
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
final Object serializedValue = serialize(element, elementValue);
|
|
|
|
final Object serializedValue = serializeElement(element, elementValue);
|
|
|
|
final String formattedName = formatter.format(element.name());
|
|
|
|
final String formattedName = formatter.format(element.name());
|
|
|
|
result.put(formattedName, serializedValue);
|
|
|
|
result.put(formattedName, serializedValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -75,13 +75,25 @@ sealed abstract class TypeSerializer<T, E extends ConfigurationElement<?>>
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected final Object serialize(E element, Object value) {
|
|
|
|
protected final Object serializeElement(E element, Object value) {
|
|
|
|
// The following cast won't cause a ClassCastException because the serializers
|
|
|
|
// This cast can lead to a ClassCastException if an element of type X is
|
|
|
|
// are selected based on the element type.
|
|
|
|
// serialized by a custom serializer that expects a different type Y.
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
final var serializer = (Serializer<Object, Object>)
|
|
|
|
final var serializer = (Serializer<Object, Object>) serializers.get(element.name());
|
|
|
|
serializers.get(element.name());
|
|
|
|
try {
|
|
|
|
return (value != null) ? serializer.serialize(value) : null;
|
|
|
|
return (value != null) ? serializer.serialize(value) : null;
|
|
|
|
|
|
|
|
} catch (ClassCastException e) {
|
|
|
|
|
|
|
|
String msg = ("Serialization of value '%s' for element '%s' of type '%s' failed.\n" +
|
|
|
|
|
|
|
|
"The type of the object to be serialized does not match the type " +
|
|
|
|
|
|
|
|
"the custom serializer of type '%s' expects.")
|
|
|
|
|
|
|
|
.formatted(
|
|
|
|
|
|
|
|
value,
|
|
|
|
|
|
|
|
element.element(),
|
|
|
|
|
|
|
|
element.declaringType(),
|
|
|
|
|
|
|
|
serializer.getClass()
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
throw new ConfigurationException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected final Object deserialize(E element, Object value) {
|
|
|
|
protected final Object deserialize(E element, Object value) {
|
|
|
@ -119,12 +131,14 @@ sealed abstract class TypeSerializer<T, E extends ConfigurationElement<?>>
|
|
|
|
if (!serializedConfiguration.containsKey(formattedName)) {
|
|
|
|
if (!serializedConfiguration.containsKey(formattedName)) {
|
|
|
|
final Object defaultValue = getDefaultValueOf(element);
|
|
|
|
final Object defaultValue = getDefaultValueOf(element);
|
|
|
|
result[i] = applyPostProcessorForElement(element, defaultValue);
|
|
|
|
result[i] = applyPostProcessorForElement(element, defaultValue);
|
|
|
|
// TODO: if (result[i] == null) requireNonPrimitiveType(element);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final var serializedValue = serializedConfiguration.get(formattedName);
|
|
|
|
final var serializedValue = serializedConfiguration.get(formattedName);
|
|
|
|
|
|
|
|
|
|
|
|
if ((serializedValue == null) && properties.inputNulls()) {
|
|
|
|
if ((serializedValue == null) && properties.inputNulls()) {
|
|
|
|
|
|
|
|
// This statement (and hence the whole block) could be removed,
|
|
|
|
|
|
|
|
// but in my opinion the code is clearer this way.
|
|
|
|
result[i] = null;
|
|
|
|
result[i] = null;
|
|
|
|
} else if (serializedValue == null) {
|
|
|
|
} else if (serializedValue == null) {
|
|
|
|
result[i] = getDefaultValueOf(element);
|
|
|
|
result[i] = getDefaultValueOf(element);
|
|
|
@ -132,9 +146,7 @@ sealed abstract class TypeSerializer<T, E extends ConfigurationElement<?>>
|
|
|
|
result[i] = deserialize(element, serializedValue);
|
|
|
|
result[i] = deserialize(element, serializedValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (result[i] == null) requireNonPrimitiveType(element);
|
|
|
|
|
|
|
|
result[i] = applyPostProcessorForElement(element, result[i]);
|
|
|
|
result[i] = applyPostProcessorForElement(element, result[i]);
|
|
|
|
// TODO: PostProcessor could return null, check should be done after
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
@ -145,34 +157,77 @@ sealed abstract class TypeSerializer<T, E extends ConfigurationElement<?>>
|
|
|
|
Object deserializeValue
|
|
|
|
Object deserializeValue
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
Object result = deserializeValue;
|
|
|
|
Object result = deserializeValue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean postProcessed = false;
|
|
|
|
for (final var entry : properties.getPostProcessorsByCondition().entrySet()) {
|
|
|
|
for (final var entry : properties.getPostProcessorsByCondition().entrySet()) {
|
|
|
|
final var condition = entry.getKey();
|
|
|
|
final var condition = entry.getKey();
|
|
|
|
|
|
|
|
|
|
|
|
if (condition.test(element)) {
|
|
|
|
if (!condition.test(element)) continue;
|
|
|
|
|
|
|
|
|
|
|
|
final var postProcessor = entry.getValue();
|
|
|
|
final var postProcessor = entry.getValue();
|
|
|
|
result = tryApplyPostProcessorForElement(postProcessor, result);
|
|
|
|
result = tryApplyPostProcessorForElement(element, postProcessor, result);
|
|
|
|
}
|
|
|
|
postProcessed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((result == null) && postProcessed)
|
|
|
|
|
|
|
|
requirePostProcessorDoesNotReturnNullForPrimitiveElement(element);
|
|
|
|
|
|
|
|
else if (result == null)
|
|
|
|
|
|
|
|
requireNonPrimitiveType(element);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static Object tryApplyPostProcessorForElement(
|
|
|
|
private static Object tryApplyPostProcessorForElement(
|
|
|
|
|
|
|
|
ConfigurationElement<?> element,
|
|
|
|
UnaryOperator<?> postProcessor,
|
|
|
|
UnaryOperator<?> postProcessor,
|
|
|
|
Object value
|
|
|
|
Object value
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
// TODO: Properly throw a ClassCastException
|
|
|
|
try {
|
|
|
|
// TODO: Add test: type of element does not match type postprocessor expects
|
|
|
|
// This cast can lead to a ClassCastException if an element of type X is
|
|
|
|
|
|
|
|
// annotated with a post-processor that takes values of some other type Y.
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
final var pp = (UnaryOperator<Object>) postProcessor;
|
|
|
|
final var pp = (UnaryOperator<Object>) postProcessor;
|
|
|
|
return pp.apply(value);
|
|
|
|
return pp.apply(value);
|
|
|
|
|
|
|
|
} catch (ClassCastException e) {
|
|
|
|
|
|
|
|
String msg = ("Deserialization of value '%s' for element '%s' of type '%s' failed.\n" +
|
|
|
|
|
|
|
|
"The type of the object to be deserialized does not match the type " +
|
|
|
|
|
|
|
|
"post-processor '%s' expects.")
|
|
|
|
|
|
|
|
.formatted(value, element.element(), element.declaringType(), postProcessor);
|
|
|
|
|
|
|
|
throw new ConfigurationException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void requireNonPrimitiveType(ConfigurationElement<?> element) {
|
|
|
|
private static void requirePostProcessorDoesNotReturnNullForPrimitiveElement(
|
|
|
|
|
|
|
|
ConfigurationElement<?> element
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
if (!element.type().isPrimitive()) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (element instanceof RecordComponentElement recordComponentElement) {
|
|
|
|
if (element instanceof RecordComponentElement recordComponentElement) {
|
|
|
|
final RecordComponent component = recordComponentElement.element();
|
|
|
|
final RecordComponent component = recordComponentElement.element();
|
|
|
|
|
|
|
|
String msg = """
|
|
|
|
|
|
|
|
Post-processors must not return null for primitive record \
|
|
|
|
|
|
|
|
components but some post-processor of component '%s' of \
|
|
|
|
|
|
|
|
record type '%s' does.\
|
|
|
|
|
|
|
|
""".formatted(component, component.getDeclaringRecord());
|
|
|
|
|
|
|
|
throw new ConfigurationException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (element instanceof FieldElement fieldElement) {
|
|
|
|
|
|
|
|
final Field field = fieldElement.element();
|
|
|
|
|
|
|
|
String msg = ("Post-processors must not return null for primitive fields " +
|
|
|
|
|
|
|
|
"but some post-processor of field '%s' does.")
|
|
|
|
|
|
|
|
.formatted(field);
|
|
|
|
|
|
|
|
throw new ConfigurationException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!component.getType().isPrimitive()) return;
|
|
|
|
throw new ConfigurationException("Unhandled ConfigurationElement: " + element);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void requireNonPrimitiveType(ConfigurationElement<?> element) {
|
|
|
|
|
|
|
|
if (!element.type().isPrimitive()) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (element instanceof RecordComponentElement recordComponentElement) {
|
|
|
|
|
|
|
|
final RecordComponent component = recordComponentElement.element();
|
|
|
|
String msg = ("Cannot set component '%s' of record type '%s' to null. " +
|
|
|
|
String msg = ("Cannot set component '%s' of record type '%s' to null. " +
|
|
|
|
"Primitive types cannot be assigned null values.")
|
|
|
|
"Primitive types cannot be assigned null values.")
|
|
|
|
.formatted(component, component.getDeclaringRecord());
|
|
|
|
.formatted(component, component.getDeclaringRecord());
|
|
|
@ -181,9 +236,6 @@ sealed abstract class TypeSerializer<T, E extends ConfigurationElement<?>>
|
|
|
|
|
|
|
|
|
|
|
|
if (element instanceof FieldElement fieldElement) {
|
|
|
|
if (element instanceof FieldElement fieldElement) {
|
|
|
|
final Field field = fieldElement.element();
|
|
|
|
final Field field = fieldElement.element();
|
|
|
|
|
|
|
|
|
|
|
|
if (!field.getType().isPrimitive()) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String msg = ("Cannot set field '%s' to null value. " +
|
|
|
|
String msg = ("Cannot set field '%s' to null value. " +
|
|
|
|
"Primitive types cannot be assigned null.")
|
|
|
|
"Primitive types cannot be assigned null.")
|
|
|
|
.formatted(field);
|
|
|
|
.formatted(field);
|
|
|
@ -197,7 +249,7 @@ sealed abstract class TypeSerializer<T, E extends ConfigurationElement<?>>
|
|
|
|
final List<Method> list = Arrays.stream(type.getDeclaredMethods())
|
|
|
|
final List<Method> list = Arrays.stream(type.getDeclaredMethods())
|
|
|
|
.filter(method -> method.isAnnotationPresent(PostProcess.class))
|
|
|
|
.filter(method -> method.isAnnotationPresent(PostProcess.class))
|
|
|
|
.filter(Predicate.not(Method::isSynthetic))
|
|
|
|
.filter(Predicate.not(Method::isSynthetic))
|
|
|
|
.filter(this::isNotAccessorMethod)
|
|
|
|
.filter(Predicate.not(this::isAccessorMethod))
|
|
|
|
.toList();
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
|
|
if (list.isEmpty())
|
|
|
|
if (list.isEmpty())
|
|
|
@ -242,19 +294,21 @@ sealed abstract class TypeSerializer<T, E extends ConfigurationElement<?>>
|
|
|
|
Reflect.invoke(method, object);
|
|
|
|
Reflect.invoke(method, object);
|
|
|
|
return object;
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The following cast won't fail because our last check above guarantees
|
|
|
|
// The following cast won't fail because our last two checks from above
|
|
|
|
// that the return type of the method equals T at this point.
|
|
|
|
// guarantee that the return type of the method equals T at this point.
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
T result = (T) Reflect.invoke(method, object);
|
|
|
|
T result = (T) Reflect.invoke(method, object);
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private boolean isNotAccessorMethod(Method method) {
|
|
|
|
final boolean isAccessorMethod(Method method) {
|
|
|
|
if (!type.isRecord()) return true;
|
|
|
|
if (!type.isRecord()) return false;
|
|
|
|
|
|
|
|
if (!method.getDeclaringClass().equals(type)) return false;
|
|
|
|
|
|
|
|
if (method.getParameterCount() > 0) return false;
|
|
|
|
return Arrays.stream(type.getRecordComponents())
|
|
|
|
return Arrays.stream(type.getRecordComponents())
|
|
|
|
.map(RecordComponent::getName)
|
|
|
|
.map(RecordComponent::getName)
|
|
|
|
.noneMatch(s -> s.equals(method.getName()));
|
|
|
|
.anyMatch(s -> s.equals(method.getName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract void requireSerializableElements();
|
|
|
|
protected abstract void requireSerializableElements();
|
|
|
|