Fix deepCopy for arrays.

Isn't used in project.
This commit is contained in:
Michael Lipp 2025-03-06 12:59:06 +01:00
parent 21d2fe2dbd
commit 1a8412d767
2 changed files with 23 additions and 4 deletions

View file

@ -18,6 +18,7 @@
package org.jdrupes.vmoperator.util;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@ -157,11 +158,12 @@ public final class DataPath {
return (T) copy;
}
if (object.getClass().isArray()) {
var copy = new ArrayList<>();
for (var item : (Object[]) object) {
copy.add(deepCopy(item));
var copy = Array.newInstance(object.getClass().getComponentType(),
Array.getLength(object));
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) {
try {

View file

@ -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]);
}
}
}