Skip to content

Commit

Permalink
Fix #1247
Browse files Browse the repository at this point in the history
  • Loading branch information
zero88 committed Oct 28, 2022
1 parent 06dd613 commit 966fd7d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 183 deletions.
258 changes: 75 additions & 183 deletions vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ static Tuple tuple() {
*
* @return the list wrapped as a tuple
*/
@SuppressWarnings("unchecked")
static <T> Tuple from(List<T> list) {
return wrap(new ArrayList<>(list));
}
Expand All @@ -88,10 +87,8 @@ static <T> Tuple from(List<T> list) {
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
static Tuple from(Object[] array) {
ArrayList<Object> list = new ArrayList<>(array.length);
for (Object o : array) {
list.add(o);
}
List<Object> list = new ArrayList<>(array.length);
Collections.addAll(list, array);
return wrap(list);
}

Expand Down Expand Up @@ -205,7 +202,7 @@ static Tuple of(Object elt1, Object elt2, Object elt3, Object elt4, Object elt5)
* Create a tuple of six elements.
*
* @param elt1 the first value
* @param elt2 the second valueg
* @param elt2 the second value
* @param elt3 the third value
* @param elt4 the fourth value
* @param elt5 the fifth value
Expand Down Expand Up @@ -657,20 +654,12 @@ default BigDecimal getBigDecimal(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Boolean[] getArrayOfBooleans(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Boolean[]) {
return (Boolean[]) val;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Boolean[] booleanArray = new Boolean[array.length];
for (int i = 0;i < array.length;i++) {
booleanArray[i] = (Boolean) array[i];
}
return booleanArray;
} else {
return (Boolean[]) val; // Throw CCE
if (val == null) return null;
if (val instanceof Boolean[]) return (Boolean[]) val;
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val)).mapToObj(i -> Array.get(val, i)).map(value -> ((Boolean) value)).toArray(Boolean[]::new);
}
throw new ClassCastException("Invalid Boolean array value type " + val.getClass());
}

/**
Expand All @@ -685,22 +674,9 @@ default Boolean[] getArrayOfBooleans(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Short[] getArrayOfShorts(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Short[]) {
return (Short[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Short[] arr = new Short[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = elt.shortValue();
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Short[]) return (Short[]) val;
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Short[] arr = new Short[len];
Expand All @@ -711,16 +687,14 @@ default Short[] getArrayOfShorts(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Short[] shortArray = new Short[array.length];
for (int i = 0;i < array.length;i++) {
shortArray[i] = ((Number) array[i]).shortValue();
}
return shortArray;
} else {
return (Short[]) val; // Throw CCE
}
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : ((Number)value).shortValue())
.toArray(Short[]::new);
}
throw new ClassCastException("Invalid Short array value type " + val.getClass());
}

/**
Expand All @@ -735,22 +709,9 @@ default Short[] getArrayOfShorts(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Integer[] getArrayOfIntegers(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Integer[]) {
return (Integer[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Integer[] arr = new Integer[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = elt.intValue();
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Integer[]) return (Integer[]) val;
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Integer[] arr = new Integer[len];
Expand All @@ -761,16 +722,14 @@ default Integer[] getArrayOfIntegers(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Integer[] integerArray = new Integer[array.length];
for (int i = 0;i < array.length;i++) {
integerArray[i] = ((Number) array[i]).intValue();
}
return integerArray;
} else {
return (Integer[]) val; // Throw CCE
}
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : ((Number)value).intValue())
.toArray(Integer[]::new);
}
throw new ClassCastException("Invalid Integer array value type " + val.getClass());
}

/**
Expand All @@ -785,22 +744,9 @@ default Integer[] getArrayOfIntegers(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Long[] getArrayOfLongs(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Long[]) {
return (Long[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Long[] arr = new Long[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = elt.longValue();
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Long[]) return (Long[]) val;
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Long[] arr = new Long[len];
Expand All @@ -811,16 +757,14 @@ default Long[] getArrayOfLongs(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Long[] longArray = new Long[array.length];
for (int i = 0;i < array.length;i++) {
longArray[i] = ((Number) array[i]).longValue();
}
return longArray;
} else {
return (Long[]) val; // Throw CCE
}
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : ((Number)value).longValue())
.toArray(Long[]::new);
}
throw new ClassCastException("Invalid Long array value type " + val.getClass());
}

/**
Expand All @@ -835,22 +779,9 @@ default Long[] getArrayOfLongs(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Float[] getArrayOfFloats(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Float[]) {
return (Float[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Float[] arr = new Float[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = elt.floatValue();
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Float[]) return (Float[]) val;
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Float[] arr = new Float[len];
Expand All @@ -861,16 +792,14 @@ default Float[] getArrayOfFloats(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Float[] floatArray = new Float[array.length];
for (int i = 0;i < array.length;i++) {
floatArray[i] = ((Number) array[i]).floatValue();
}
return floatArray;
} else {
return (Float[]) val; // Throw CCE
}
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : ((Number)value).floatValue())
.toArray(Float[]::new);
}
throw new ClassCastException("Invalid Float array value type " + val.getClass());
}

/**
Expand All @@ -885,22 +814,9 @@ default Float[] getArrayOfFloats(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Double[] getArrayOfDoubles(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Double[]) {
return (Double[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Double[] arr = new Double[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = elt.doubleValue();
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Double[]) return (Double[]) val;
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Double[] arr = new Double[len];
Expand All @@ -911,16 +827,14 @@ default Double[] getArrayOfDoubles(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Double[] doubleArray = new Double[array.length];
for (int i = 0;i < array.length;i++) {
doubleArray[i] = ((Number) array[i]).doubleValue();
}
return doubleArray;
} else {
return (Double[]) val; // Throw CCE
}
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : ((Number)value).doubleValue())
.toArray(Double[]::new);
}
throw new ClassCastException("Invalid Double array value type " + val.getClass());
}

/**
Expand All @@ -932,22 +846,9 @@ default Double[] getArrayOfDoubles(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Numeric[] getArrayOfNumerics(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Numeric[]) {
return (Numeric[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Numeric[] arr = new Numeric[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = Numeric.create(elt);
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Numeric[]) return (Numeric[]) val;
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Numeric[] arr = new Numeric[len];
Expand All @@ -958,16 +859,11 @@ default Numeric[] getArrayOfNumerics(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Numeric[] doubleArray = new Numeric[array.length];
for (int i = 0;i < array.length;i++) {
doubleArray[i] = Numeric.create((Number) array[i]);
}
return doubleArray;
} else {
throw new ClassCastException();
}
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val)).mapToObj(i -> Array.get(val, i)).map(value -> Numeric.create((Number)value)).toArray(Numeric[]::new);
}
throw new ClassCastException("Invalid Numeric array value type " + val.getClass());
}

/**
Expand All @@ -982,11 +878,9 @@ default Numeric[] getArrayOfNumerics(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default String[] getArrayOfStrings(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof String[]) {
return (String[]) val;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof String[]) return (String[]) val;
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
String[] arr = new String[len];
Expand All @@ -997,16 +891,14 @@ default String[] getArrayOfStrings(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
String[] stringArray = new String[array.length];
for (int i = 0;i < array.length;i++) {
stringArray[i] = (String) array[i];
}
return stringArray;
} else {
return (String[]) val; // Throw CCE
}
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : String.valueOf(value))
.toArray(String[]::new);
}
throw new ClassCastException("Invalid String array value type " + val.getClass());
}

/**
Expand Down
Loading

0 comments on commit 966fd7d

Please sign in to comment.