0

更新- 您可以在此处找到一个示例项目来重新创建问题: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;
    }

}

我已经在 J​​ava 中对这个类进行了单元测试(我也有一个反序列化器)并且它通过了。在 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

对可能发生的事情的想法?

4

1 回答 1

0

您使用的是什么版本的 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)); }

我使用在当前 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, ...

于 2015-10-08T19:11:43.913 回答