@ -48,37 +48,32 @@ final class PolymorphicSerializer implements Serializer<Object, Map<?, ?>> {
privatevoidrequireDistinctAliases(Stringalias){
if(typeByAlias.containsKey(alias)){
Stringmsg="The @PolymorphicTypes annotation must not use the same alias for "+
"multiple types. Alias '%s' appears more than once."
.formatted(alias);
thrownewConfigurationException(msg);
thrownewConfigurationException("The @PolymorphicTypes annotation must not use the same alias for multiple types. Alias '"+alias+"' appears more than once.");
}
}
privatevoidrequireDistinctTypes(Class<?>type){
if(aliasByType.containsKey(type)){
Stringmsg="The @PolymorphicTypes annotation must not contain multiple "+
"definitions for the same subtype. Type '%s' appears more than once."
.formatted(type.getName());
thrownewConfigurationException(msg);
thrownewConfigurationException("The @PolymorphicTypes annotation must not contain multiple definitions for the same subtype. Type '"+type.getName()+"' appears more than once.");
}
}
@Override
@SuppressWarnings("rawtypes")
publicMap<?,?>serialize(Objectelement){
finalClass<?>elementType=element.getClass();
// this cast won't cause any exceptions as we only pass objects of types the
@ -120,41 +113,23 @@ final class PolymorphicSerializer implements Serializer<Object, Map<?, ?>> {
try{
returnReflect.getClassByName(className);
}catch(RuntimeExceptione){
Stringmsg=("Polymorphic deserialization for type '%s' failed. "+
"The class '%s' does not exist.")
.formatted(polymorphicType.getName(),className);
thrownewConfigurationException(msg,e);
thrownewConfigurationException("Polymorphic deserialization for type '"+polymorphicType.getName()+"' failed. The class '"+className+"' does not exist.",e);
Stringmsg=("Polymorphic deserialization for type '%s' failed. The type identifier '%s' "+
"which should hold the type is not a string but of type '%s'.")
.formatted(
polymorphicType.getName(),
typeIdentifier,
typeIdentifier.getClass().getName()
);
thrownewConfigurationException(msg);
thrownewConfigurationException("Polymorphic deserialization for type '"+polymorphicType.getName()+"' failed. The type identifier '"+typeIdentifier+"' "+
"which should hold the type is not a string but of type '"+typeIdentifier.getClass().getName()+"'.");
return"Deserialization of value '"+value+"' with type '"+value.getClass()+"' for component '"+element.element()+"' of record '"+element.declaringType()+"' failed.";
thrownewRuntimeException("Constructor of type '"+cls.getSimpleName()+"' with parameters '"+argumentTypeNamesJoined(argumentTypes)+"' threw an exception.",e);
}
}
@ -79,22 +77,15 @@ final class Reflect {
constructor.setAccessible(true);
returnconstructor.newInstance();
}catch(NoSuchMethodExceptione){
Stringmsg="Type '%s' doesn't have a no-args constructor."
.formatted(cls.getSimpleName());
thrownewRuntimeException(msg,e);
thrownewRuntimeException("Type '"+cls.getSimpleName()+"' doesn't have a no-args constructor.",e);
}catch(IllegalAccessExceptione){
/*Thisexceptionshouldnotbethrownbecause
*wesettheconstructortobeaccessible.*/
Stringmsg="No-args constructor of type '%s' not accessible."
.formatted(cls.getSimpleName());
thrownewRuntimeException(msg,e);
thrownewRuntimeException("No-args constructor of type '"+cls.getSimpleName()+"' not accessible.",e);
}catch(InstantiationExceptione){
Stringmsg="Type '%s' is not instantiable.".formatted(cls.getSimpleName());
thrownewRuntimeException(msg,e);
thrownewRuntimeException("Type '"+cls.getSimpleName()+"' is not instantiable.",e);
}catch(InvocationTargetExceptione){
Stringmsg="No-args constructor of type '%s' threw an exception."
.formatted(cls.getSimpleName());
thrownewRuntimeException(msg,e);
thrownewRuntimeException("No-args constructor of type '"+cls.getSimpleName()+"' threw an exception.",e);
}
}
@ -115,9 +106,7 @@ final class Reflect {
// cannot happen because records are instantiable
thrownewRuntimeException(e);
}catch(InvocationTargetExceptione){
Stringmsg="The canonical constructor of record type '%s' threw an exception."
.formatted(recordType.getSimpleName());
thrownewRuntimeException(msg,e);
thrownewRuntimeException("The canonical constructor of record type '"+recordType.getSimpleName()+"' threw an exception.",e);