Skip to content

Commit

Permalink
The ListArrayTypeDescriptor deepCopy method should not convert a List…
Browse files Browse the repository at this point in the history
… to an Java array vladmihalcea#187
  • Loading branch information
dimalu85 authored and vladmihalcea committed Feb 26, 2020
1 parent b629eb6 commit 98ee9bc
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vladmihalcea.hibernate.type.array.internal;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -341,4 +342,21 @@ public static <T> Class<T[]> toArrayClass(Class<T> arrayElementClass) {
return (Class<T[]>) array.getClass();
}
}

/**
* Transforms an array to a {@link List}. The reason why {@link Arrays#asList(Object[])}
* is not used is because on Java 6 it wraps the {@link List} so we end up
* with two nested {@link List} objects.
*
* @param array array to transform
* @param <T> array element type
* @return the {@link List} representation of the array
*/
public static <T> List<T> asList(T[] array) {
List<T> list = new ArrayList<T>(array.length);
for (int i = 0; i < array.length; i++) {
list.add(i, array[i]);
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ListArrayTypeDescriptor() {
protected Object deepCopyNotNull(Object value) {
if (value instanceof List) {
Object[] array = ((List) value).toArray();
return ArrayUtil.deepCopy(array);
return ArrayUtil.asList((Object[]) ArrayUtil.deepCopy(array));
} else if (value.getClass().isArray()) {
Object[] array = (Object[]) value;
return ArrayUtil.deepCopy(array);
Expand Down Expand Up @@ -83,14 +83,14 @@ public boolean areEqual(Object one, Object another) {
if (one == null || another == null) {
return false;
}
if(one instanceof Collection) {
one = ((Collection) one).toArray();
if (one instanceof List && another instanceof List) {
return ArrayUtil.isEquals(((List) one).toArray(), ((List) another).toArray());
}
if(another instanceof Collection) {
another = ((Collection) another).toArray();
if (one instanceof Object[] && another instanceof Object[]) {
return ArrayUtil.isEquals(one, another);
} else {
throw new UnsupportedOperationException("The provided " + one + " and " + another + " are not Object[] or List!");
}

return ArrayUtil.isEquals(one, another);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vladmihalcea.hibernate.type.array.internal;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -341,4 +342,21 @@ public static <T> Class<T[]> toArrayClass(Class<T> arrayElementClass) {
return (Class<T[]>) array.getClass();
}
}

/**
* Transforms an array to a {@link List}. The reason why {@link Arrays#asList(Object[])}
* is not used is because on Java 6 it wraps the {@link List} so we end up
* with two nested {@link List} objects.
*
* @param array array to transform
* @param <T> array element type
* @return the {@link List} representation of the array
*/
public static <T> List<T> asList(T[] array) {
List<T> list = new ArrayList<T>(array.length);
for (int i = 0; i < array.length; i++) {
list.add(i, array[i]);
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ListArrayTypeDescriptor() {
protected Object deepCopyNotNull(Object value) {
if (value instanceof List) {
Object[] array = ((List) value).toArray();
return ArrayUtil.deepCopy(array);
return ArrayUtil.asList((Object[]) ArrayUtil.deepCopy(array));
} else if (value.getClass().isArray()) {
Object[] array = (Object[]) value;
return ArrayUtil.deepCopy(array);
Expand Down Expand Up @@ -83,14 +83,14 @@ public boolean areEqual(Object one, Object another) {
if (one == null || another == null) {
return false;
}
if(one instanceof Collection) {
one = ((Collection) one).toArray();
if (one instanceof List && another instanceof List) {
return ArrayUtil.isEquals(((List) one).toArray(), ((List) another).toArray());
}
if(another instanceof Collection) {
another = ((Collection) another).toArray();
if (one instanceof Object[] && another instanceof Object[]) {
return ArrayUtil.isEquals(one, another);
} else {
throw new UnsupportedOperationException("The provided " + one + " and " + another + " are not Object[] or List!");
}

return ArrayUtil.isEquals(one, another);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vladmihalcea.hibernate.type.array.internal;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -341,4 +342,21 @@ public static <T> Class<T[]> toArrayClass(Class<T> arrayElementClass) {
return (Class<T[]>) array.getClass();
}
}

/**
* Transforms an array to a {@link List}. The reason why {@link Arrays#asList(Object[])}
* is not used is because on Java 6 it wraps the {@link List} so we end up
* with two nested {@link List} objects.
*
* @param array array to transform
* @param <T> array element type
* @return the {@link List} representation of the array
*/
public static <T> List<T> asList(T[] array) {
List<T> list = new ArrayList<T>(array.length);
for (int i = 0; i < array.length; i++) {
list.add(i, array[i]);
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ListArrayTypeDescriptor() {
protected Object deepCopyNotNull(Object value) {
if (value instanceof List) {
Object[] array = ((List) value).toArray();
return ArrayUtil.deepCopy(array);
return ArrayUtil.asList((Object[]) ArrayUtil.deepCopy(array));
} else if (value.getClass().isArray()) {
Object[] array = (Object[]) value;
return ArrayUtil.deepCopy(array);
Expand Down Expand Up @@ -83,14 +83,14 @@ public boolean areEqual(Object one, Object another) {
if (one == null || another == null) {
return false;
}
if(one instanceof Collection) {
one = ((Collection) one).toArray();
if (one instanceof List && another instanceof List) {
return ArrayUtil.isEquals(((List) one).toArray(), ((List) another).toArray());
}
if(another instanceof Collection) {
another = ((Collection) another).toArray();
if (one instanceof Object[] && another instanceof Object[]) {
return ArrayUtil.isEquals(one, another);
} else {
throw new UnsupportedOperationException("The provided " + one + " and " + another + " are not Object[] or List!");
}

return ArrayUtil.isEquals(one, another);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.vladmihalcea.hibernate.type.array.internal;

import com.vladmihalcea.hibernate.type.util.ReflectionUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
Expand Down Expand Up @@ -344,4 +342,21 @@ public static <T> Class<T[]> toArrayClass(Class<T> arrayElementClass) {
return (Class<T[]>) array.getClass();
}
}

/**
* Transforms an array to a {@link List}. The reason why {@link Arrays#asList(Object[])}
* is not used is because on Java 6 it wraps the {@link List} so we end up
* with two nested {@link List} objects.
*
* @param array array to transform
* @param <T> array element type
* @return the {@link List} representation of the array
*/
public static <T> List<T> asList(T[] array) {
List<T> list = new ArrayList<T>(array.length);
for (int i = 0; i < array.length; i++) {
list.add(i, array[i]);
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ListArrayTypeDescriptor() {
protected Object deepCopyNotNull(Object value) {
if (value instanceof List) {
Object[] array = ((List) value).toArray();
return ArrayUtil.deepCopy(array);
return ArrayUtil.asList((Object[]) ArrayUtil.deepCopy(array));
} else if (value.getClass().isArray()) {
Object[] array = (Object[]) value;
return ArrayUtil.deepCopy(array);
Expand Down Expand Up @@ -83,14 +83,14 @@ public boolean areEqual(Object one, Object another) {
if (one == null || another == null) {
return false;
}
if(one instanceof Collection) {
one = ((Collection) one).toArray();
if (one instanceof List && another instanceof List) {
return ArrayUtil.isEquals(((List) one).toArray(), ((List) another).toArray());
}
if(another instanceof Collection) {
another = ((Collection) another).toArray();
if (one instanceof Object[] && another instanceof Object[]) {
return ArrayUtil.isEquals(one, another);
} else {
throw new UnsupportedOperationException("The provided " + one + " and " + another + " are not Object[] or List!");
}

return ArrayUtil.isEquals(one, another);
}

@Override
Expand Down

0 comments on commit 98ee9bc

Please sign in to comment.