HeapsIO / hxbit

Haxe Binary serialization and network synchronization library
155 stars 30 forks source link

exception:Too late to register class on cs and java #24

Closed AdrianV closed 6 years ago

AdrianV commented 6 years ago

I don't know if it is a bug of the cs and java target or of hxbit. But the following code throws an exception on both targets "Too late to register class" at hxbit.Serializer.registerClass() So I post the issue here.

class SomeBase implements hxbit.Serializable {
    @:s public var name: String = "";
    @:s public var className: String = "";
    @:s public var module: String = "";
    @:s public var headerTAN: Int = 0;

    public function new(name, className, module) {
        this.name = name;
        this.className = className;
        this.module = module;
    }
}

class Person implements hxbit.Serializable {
    @:s public var name: String = "";
    @:s public var age: Int = 0;

    public function new(name, age) {
        this.name = name;
        this.age = age;
    }
}

class SomePerson extends Person {

    public function new(name, age) {
        super(name, age);
    }
}

class Bug {

    static function main() {
        var p = new SomePerson("Adrian", 54);
        var s = new hxbit.Serializer();
        var data = s.serialize(p);
        var test = s.unserialize(data, Person);
        trace(test); 
        var sb = new SomeBase("Hallo", "Haxe", ""); // here it throws
        var data = s.serialize(sb);
        var sb2 = s.unserialize(data, SomeBase);
        trace(sb2);

    }
}
posxposy commented 6 years ago

Any updates on this? Got the similar behaviour on Java target.

class PlaceStruct implements hxbit.Serializable {
    @s public var name:String;
    @s public var address:String;

    public function new(name:String, address:String) {
        this.name = name;
        this.address = address;
    }
}

//--------------------------
//Call constructor anywhere:
new PlaceStruct("Name", "Address"); // java.lang.ExceptionInInitializerError

Did I miss something?

ncannasse commented 6 years ago

Seems strange, we insert with macros a static var __clid = hxbit.Serializer.registerClass(MyClass) in all classes implementing hxbit.Serializable, which should be called before the Serializer is created.

@waneck what is the status of static var initialization on java output?

ncannasse commented 6 years ago

Seems like a Haxe issue, not hxbit specific. Closing this issue for now (follow up on referenced haxe issue)