From afa4233b40c7f992df7c17c88ca51a5adb91af46 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 26 Feb 2025 14:35:02 +0100 Subject: [PATCH] [jOOQ/jOOQ#18059] ArrayStoreException when using Converter with empty arrays --- .../src/main/java/org/jooq/ArrayConverter.java | 12 ++++++++---- jOOQ/src/main/java/org/jooq/impl/Convert.java | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/ArrayConverter.java b/jOOQ/src/main/java/org/jooq/ArrayConverter.java index a70ffeaf56..d665eac86b 100644 --- a/jOOQ/src/main/java/org/jooq/ArrayConverter.java +++ b/jOOQ/src/main/java/org/jooq/ArrayConverter.java @@ -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 extends AbstractContextConverter { final ContextConverter inverse; public ArrayConverter(ContextConverter converter) { - super(arrayType(converter.fromType()), arrayType(converter.toType())); + + // [#18059] Work with wrapper types, as we cannot represent Converter, + // so we shouldn't work with Converter 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 extends AbstractContextConverter { @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); } } diff --git a/jOOQ/src/main/java/org/jooq/impl/Convert.java b/jOOQ/src/main/java/org/jooq/impl/Convert.java index c0ef4304f7..e4c86e264a 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Convert.java +++ b/jOOQ/src/main/java/org/jooq/impl/Convert.java @@ -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[] convertCollection(Collection from, Class 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[] convertCollection(Collection from, Class to) { return new ConvertAll(to).from(from, converterContext()); }