[jOOQ/jOOQ#18059] ArrayStoreException when using Converter<T[][], U[][]> with empty arrays

This commit is contained in:
Lukas Eder 2025-02-26 14:35:02 +01:00
parent dd1d06598d
commit afa4233b40
2 changed files with 24 additions and 6 deletions

View File

@ -38,9 +38,10 @@
package org.jooq;
import static org.jooq.impl.Internal.arrayType;
import static org.jooq.tools.reflect.Reflect.wrapper;
import org.jooq.impl.AbstractContextConverter;
import org.jooq.tools.Convert;
import org.jooq.impl.Internal;
/**
* A {@link Converter} that can convert arrays based on a delegate converter
@ -54,7 +55,10 @@ final class ArrayConverter<T, U> extends AbstractContextConverter<T[], U[]> {
final ContextConverter<U, T> inverse;
public ArrayConverter(ContextConverter<T, U> converter) {
super(arrayType(converter.fromType()), arrayType(converter.toType()));
// [#18059] Work with wrapper types, as we cannot represent Converter<int, U>,
// so we shouldn't work with Converter<int[], U[]> either
super(arrayType(wrapper(converter.fromType())), arrayType(wrapper(converter.toType())));
this.converter = converter;
this.inverse = Converters.inverse(converter);
@ -72,11 +76,11 @@ final class ArrayConverter<T, U> extends AbstractContextConverter<T[], U[]> {
@Override
public final U[] from(T[] t, ConverterContext scope) {
return Convert.convertArray(t, converter);
return Internal.convertArray(t, converter);
}
@Override
public final T[] to(U[] t, ConverterContext scope) {
return Convert.convertArray(t, inverse);
return Internal.convertArray(t, inverse);
}
}

View File

@ -137,6 +137,8 @@ import org.jooq.types.YearToSecond;
import org.jooq.util.postgres.PostgresUtils;
import org.jooq.util.xml.jaxb.InformationSchema;
import org.jetbrains.annotations.NotNull;
import jakarta.xml.bind.JAXB;
/**
@ -406,7 +408,7 @@ final class Convert {
static final Object convertArray(Object[] from, Class<?> toClass) throws DataTypeException {
if (from == null)
return null;
else if (!toClass.isArray())
else if (!equalArrayDegree(from.getClass(), toClass))
return convertArray(from, arrayType(toClass));
else if (toClass == from.getClass())
return from;
@ -492,7 +494,19 @@ final class Convert {
}
}
static final <U> U[] convertCollection(Collection from, Class<? extends U[]> to){
private static boolean equalArrayDegree(Class<?> c1, Class<?> c2) {
Class<?> ct1 = c1.getComponentType();
Class<?> ct2 = c2.getComponentType();
if (ct1 == null && ct2 == null)
return true;
else if (ct1 == null || ct2 == null)
return false;
else
return equalArrayDegree(ct1, ct2);
}
static final <U> U[] convertCollection(Collection from, Class<? extends U[]> to) {
return new ConvertAll<U[]>(to).from(from, converterContext());
}