我正在尝试使用 ChronicleWire。如特性中所述,可选字段支持开箱即用。
我刚刚创建了一个简单的自描述实体,其中包含一个可选(可为空)字段:
public class Foo extends SelfDescribingMarshallable {
private String name;
private Baz baz;
}
wherebaz
可能或可能不是 null 并实现/扩展 Marshallable。
当我尝试将实体放入 ChronicleMap 时,出现错误:
Exception in thread "main" java.lang.NullPointerException
at net.openhft.chronicle.bytes.BytesMarshaller$BytesMarshallableFieldAccess.getValue(BytesMarshaller.java:211)
at net.openhft.chronicle.bytes.BytesMarshaller$FieldAccess.write(BytesMarshaller.java:152)
at net.openhft.chronicle.bytes.BytesMarshaller.writeMarshallable(BytesMarshaller.java:70)
at net.openhft.chronicle.bytes.BytesUtil.writeMarshallable(BytesUtil.java:295)
at net.openhft.chronicle.bytes.BytesMarshallable.writeMarshallable(BytesMarshallable.java:48)
at net.openhft.chronicle.bytes.BytesMarshaller$BytesMarshallableFieldAccess.getValue(BytesMarshaller.java:211)
at net.openhft.chronicle.bytes.BytesMarshaller$FieldAccess.write(BytesMarshaller.java:152)
当我尝试改用 java optional 并且我的实体更改为:
public class Foo extends SelfDescribingMarshallable {
private String name;
private Optional<Baz> baz = Optional.empty();
}
然后引发了另一个错误:
Caused by: java.lang.IllegalArgumentException: type=class java.util.Optional is unsupported, it must either be of type Marshallable, String or AutoBoxed primitive Object
at net.openhft.chronicle.wire.ValueOut.object(ValueOut.java:682)
at net.openhft.chronicle.wire.ValueOut.untypedObject(ValueOut.java:795)
at net.openhft.chronicle.wire.ValueOut.object(ValueOut.java:519)
at net.openhft.chronicle.wire.WireMarshaller$ObjectFieldAccess.getValue(WireMarshaller.java:669)
at net.openhft.chronicle.wire.WireMarshaller$FieldAccess.write(WireMarshaller.java:518)
at net.openhft.chronicle.wire.WireMarshaller.writeMarshallable(WireMarshaller.java:199)
at net.openhft.chronicle.wire.Marshallable.writeMarshallable(Marshallable.java:132)
我没有放弃并尝试实现我自己的可选。这是:
@AllArgsConstructor(staticName = "of")
@NoArgsConstructor(staticName = "empty")
public class OptionalValue<T extends Marshallable> implements Marshallable {
@Nullable
private T value;
@Override
public void readMarshallable(@NotNull WireIn wire) throws IORuntimeException {
var val = wire.read("value");
if (!val.isNull()) {
val.marshallable(value);
}
}
@Override
public void writeMarshallable(@NotNull WireOut wire) {
if (value == null) {
wire.write("value").nu11();
} else {
wire.write("value").marshallable(value);
}
}
boolean isEmpty() { return value == null; }
T get() { return value; }
}
在这种情况下,我看到了另一个错误:
Caused by: java.lang.ClassCastException
at net.openhft.chronicle.core.util.ObjectUtils.asCCE(ObjectUtils.java:294)
at net.openhft.chronicle.core.util.ObjectUtils$ConversionFunction.apply(ObjectUtils.java:624)
at net.openhft.chronicle.core.util.ObjectUtils$ConversionFunction.apply(ObjectUtils.java:592)
at net.openhft.chronicle.core.ClassLocal.computeValue(ClassLocal.java:54)
at java.base/java.lang.ClassValue.getFromHashMap(ClassValue.java:226)
at java.base/java.lang.ClassValue.getFromBackup(ClassValue.java:208)
at java.base/java.lang.ClassValue.get(ClassValue.java:114)
at net.openhft.chronicle.core.util.ObjectUtils.convertTo0(ObjectUtils.java:257)
... 28 more
Caused by: java.lang.NoSuchMethodException: com.redacted.entity.OptionalValue.<init>(java.lang.String)
Caused by: java.lang.NoSuchMethodException: com.redacted.entity.OptionalValue.<init>(java.lang.String)
at java.base/java.lang.Class.getConstructor0(Class.java:3349)
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553)
at net.openhft.chronicle.core.util.ObjectUtils$ConversionFunction.apply(ObjectUtils.java:620)
那么有人知道如何正确修复或使用它吗?编年史线 2.22ae6