在使用Mory yLayout::varHandle构造varHandle时,我传入了一个自组装的对象数组,以实现构造内存段的公共方法.但我发现它似乎阻止了我使用对象数组而不是可变array.
代码片段如下所示.
public static MemorySegment buildMemorySegment(Object object, MemorySegment memorySegment, MemoryLayout originMemoryLayout, MemoryLayout memoryLayout, List<PathElement> pathElements, List<Object> indexParams) throws NoSuchFieldException, IllegalAccessException {
Class<?> aClass = object.getClass();
if (memoryLayout instanceof SequenceLayout sequenceLayout) {
pathElements.add(PathElement.sequenceElement());
for (int i = 0; i < sequenceLayout.elementCount(); i++) {
Object[] array;
if (Collection.class.isAssignableFrom(aClass)) {
assert object instanceof Collection<?>;
Collection<?> collection = (Collection<?>) object;
array = collection.toArray();
} else {
assert object instanceof Object[];
array = ((Object[]) object);
}
Object o = array[i];
indexParams.add((long) i);
buildMemorySegment(o, memorySegment, originMemoryLayout, sequenceLayout.elementLayout(), pathElements, indexParams);
indexParams.removeLast();
}
pathElements.removeLast();
} else if (memoryLayout instanceof StructLayout structLayout) {
List<MemoryLayout> memoryLayouts = structLayout.memberLayouts();
for (MemoryLayout layout : memoryLayouts) {
String layoutName = layout.name().orElseThrow();
pathElements.add(PathElement.groupElement(layoutName));
Field declaredField = aClass.getDeclaredField(layoutName);
Object fieldValue = getFieldValue(object, declaredField);
buildMemorySegment(fieldValue, memorySegment, originMemoryLayout, layout, pathElements, indexParams);
pathElements.removeLast();
}
} else if (memoryLayout instanceof ValueLayout) {
Object[] varParams = new Object[indexParams.size() + 2];
varParams[0] = memorySegment;
for (int i = 0; i < indexParams.size(); i++) {
varParams[i + 1] = indexParams.get(i);
}
varParams[varParams.length - 1] = object;
VarHandle varHandle = originMemoryLayout.varHandle(pathElements.toArray(new PathElement[0]));
varHandle.set(varParams);
}
return MemorySegment.NULL;
}
关键问题是我成功地构建了内存布局,并通过解析内存布局来控制varHandle参数,从而构建了varHandle.出现了以下问题.
这就是我的调试程序所说的,我想要实现的是
varHandler.set(segment, (long)i, i);
个
我能实施我的 idea 吗?我该怎么做呢?