j2objc 对象序列化 -- java.lang.NoSuchMethodException: writeReplace

j2objc object serialization -- java.lang.NoSuchMethodException: writeReplace

更新 - 您可以在此处找到一个示例项目来重现问题:https://github.com/benf1977/j2objc-serialization-example/releases/tag/1.0

我正在尝试序列化 Java 中的对象并将字节传递到我的 Objective-C 层以写入文件系统。我的相关 Java 代码是这样的:

/**
 * Created by benjamin.flynn on 10/7/15.
 */
public class TestData implements Serializable {

    private String mName;
    private int mAge;

    public TestData(String pName, int pAge) {
        mName = pName;
        mAge = pAge;
    }

    public String getName() {
        return mName;
    }

    public int getAge() {
        return mAge;
    }

    public byte[] bytes() {
        byte[] bytes = null;
        try (ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
             ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream);) { // Application will break on this line when run on Xcode
            objectOutputStream.writeObject(this);
            bytes = byteOutputStream.toByteArray();
        } catch (IOException ioe) {
            System.err.println("Aww snap: " + ioe);
        }
        return bytes;
    }

}

我已经在 Java 中对这个 class 进行了单元测试(我也有一个反序列化器)并且它通过了。在 Objective-C,我这样做:

ComMycompanyTestData *testData = [[ComMycompanyTestData alloc] initWithNSString:@"Daryl" withInt:47];
IOSByteArray *bytes = testData.bytes; // Exception here

例外情况是:

(lldb) po $arg1
java.lang.NoSuchMethodException: writeReplace

相关轨迹为:

frame #0: 0x000000019a2cbf48 libobjc.A.dylib`objc_exception_throw
    frame #1: 0x0000000100214bb0 MyApp `-[IOSClass getDeclaredMethod:parameterTypes:] + 124
    frame #2: 0x00000001000c9188 MyApp`JavaIoObjectStreamClass_findMethodWithIOSClass_withNSString_ + 144
    frame #3: 0x00000001000c71dc MyApp`JavaIoObjectStreamClass_createClassDescWithIOSClass_ + 984
    frame #4: 0x00000001000c909c MyApp`JavaIoObjectStreamClass_lookupStreamClassWithIOSClass_ + 88
    frame #5: 0x00000001000c8f88 MyApp`JavaIoObjectStreamClass_lookupWithIOSClass_ + 44
    frame #6: 0x00000001000c6f30 MyApp`JavaIoObjectStreamClass_createClassDescWithIOSClass_ + 300
    frame #7: 0x00000001000c909c MyApp`JavaIoObjectStreamClass_lookupStreamClassWithIOSClass_ + 88
    frame #8: 0x00000001000c8f88 MyApp`JavaIoObjectStreamClass_lookupWithIOSClass_ + 44
    frame #9: 0x00000001000c284c MyApp`JavaIoObjectOutputStream_initWithJavaIoOutputStream_ + 56
    frame #10: 0x00000001000c6a20 MyApp`new_JavaIoObjectOutputStream_initWithJavaIoOutputStream_ + 48
  * frame #11: 0x00000001000a666c MyApp`-[ComMycompanyTestData bytes](self=0x000000013fd51a50, _cmd="bytes") + 76 at TestData.java:49

关于可能发生的事情的想法?

您使用的是什么版本的 j2objc?我们最近对序列化进行了改进,运行 添加此测试后您的示例有效:

public static void main(String[] args) { TestData td = new TestData("Jane Doe", 30); byte[] serialized = td.bytes(); System.out.println("serialized: " + Arrays.toString(serialized)); }

我 运行 使用 translator/runtime 在当前 j2objc 源上使用 "make dist" 构建:

$ javac TestData.java $ java TestData serialized: [-84, -19, 0, ... $ j2objc TestData.java translating TestData.java Translated 1 file: 0 errors, 0 warnings $ j2objcc TestData.m $ ./a.out TestData serialized: [-84, -19, 0, ...