Fix deepCopy for arrays.
Isn't used in project.
This commit is contained in:
parent
21d2fe2dbd
commit
1a8412d767
2 changed files with 23 additions and 4 deletions
|
|
@ -18,6 +18,7 @@
|
||||||
|
|
||||||
package org.jdrupes.vmoperator.util;
|
package org.jdrupes.vmoperator.util;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -157,11 +158,12 @@ public final class DataPath {
|
||||||
return (T) copy;
|
return (T) copy;
|
||||||
}
|
}
|
||||||
if (object.getClass().isArray()) {
|
if (object.getClass().isArray()) {
|
||||||
var copy = new ArrayList<>();
|
var copy = Array.newInstance(object.getClass().getComponentType(),
|
||||||
for (var item : (Object[]) object) {
|
Array.getLength(object));
|
||||||
copy.add(deepCopy(item));
|
for (int i = 0; i < Array.getLength(object); i++) {
|
||||||
|
Array.set(copy, i, deepCopy(Array.get(object, i)));
|
||||||
}
|
}
|
||||||
return (T) copy.toArray();
|
return (T) copy;
|
||||||
}
|
}
|
||||||
if (object instanceof Cloneable) {
|
if (object instanceof Cloneable) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
package org.jdrupes.vmoperator.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class DataPathTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testArray() {
|
||||||
|
int[] orig
|
||||||
|
= { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) };
|
||||||
|
var copy = DataPath.deepCopy(orig);
|
||||||
|
for (int i = 0; i < orig.length; i++) {
|
||||||
|
assertEquals(orig[i], copy[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue