2013-02-18 45 views
1

我有一個非常簡單的java類,它代表了一個2D矢量。但是,當我運行代碼將其序列化爲消息包時,它會創建運行時錯誤。序列化這個類應該是真正的基本行爲,應該工作,對嗎?我發現沒有其他人有類似的問題。帶有浮動崩潰的Java msgpack類

代碼和例外情況如下。我在eclipse的Windows 7上使用java 1.7 SDK,我嘗試了maven的消息包1.6.6和1.6.7。

import org.jbox2d.common.Vec2; 
import org.msgpack.annotation.Message; 

@Message 
public class SerVec2 
{ 
     public float x,y; 

     public SerVec2(Vec2 v) 
     { 
       this.x = v.x; 
       this.y = v.y; 
     } 

     public Vec2 toV() 
     { 
       return new Vec2(x,y); 
     } 

} 

void someWhereElse(SerVec2 sv) 
{ 
    MessagePack msgpack = new MessagePack(); 
    byte[] bytes = msgpack.write(sv); 
} 

運行時錯誤:

Feb 18, 2013 8:48:49 AM org.msgpack.template.builder.BuildContext build 
SEVERE: builder: 
{ 
    if (!$3 && $1.trySkipNil()) { 
    return null; 
    } 
    skylights.util.SerVec2 _$$_t; 
    if ($2 == null) { 
    _$$_t = new skylights.util.SerVec2(); 
    } else { 
    _$$_t = (skylights.util.SerVec2) $2; 
    } 
    $1.readArrayBegin(); 
    _$$_t.x = $1.readFloat(); 
    _$$_t.y = $1.readFloat(); 
    $1.readArrayEnd(); 
    return _$$_t; 
} 

org.msgpack.MessageTypeException: org.msgpack.template.builder.TemplateBuildException: Cannot compile: 
{ 
    if (!$3 && $1.trySkipNil()) { 
    return null; 
    } 
    skylights.util.SerVec2 _$$_t; 
    if ($2 == null) { 
    _$$_t = new skylights.util.SerVec2(); 
    } else { 
    _$$_t = (skylights.util.SerVec2) $2; 
    } 
    $1.readArrayBegin(); 
    _$$_t.x = $1.readFloat(); 
    _$$_t.y = $1.readFloat(); 
    $1.readArrayEnd(); 
    return _$$_t; 
} 

     at org.msgpack.template.TemplateRegistry.buildAndRegister(TemplateRegistry.java:516) 
     at org.msgpack.template.TemplateRegistry.lookupAfterBuilding(TemplateRegistry.java:413) 
     at org.msgpack.template.TemplateRegistry.lookup(TemplateRegistry.java:246) 
     at org.msgpack.template.builder.JavassistTemplateBuilder.toTemplate(JavassistTemplateBuilder.java:128) 
     at org.msgpack.template.builder.JavassistTemplateBuilder.buildTemplate(JavassistTemplateBuilder.java:116) 
     at org.msgpack.template.builder.AbstractTemplateBuilder.buildTemplate(AbstractTemplateBuilder.java:61) 
     at org.msgpack.template.TemplateRegistry.buildAndRegister(TemplateRegistry.java:503) 
     at org.msgpack.template.TemplateRegistry.lookupAfterBuilding(TemplateRegistry.java:413) 
     at org.msgpack.template.TemplateRegistry.lookup(TemplateRegistry.java:246) 
     at org.msgpack.MessagePack.write(MessagePack.java:195) 
     at skylights.core.Entity.saveToBytes(Entity.java:260) 
     at skylights.level.Level.saveToFile(Level.java:106) 
     at skylights.core.gui.KeyHandler.keyPressed(KeyHandler.java:29) 
     at skylights.core.Game.keyPressed(Game.java:219) 
     at org.newdawn.slick.Input.poll(Input.java:1182) 
     at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:656) 
     at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456) 
     at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361) 
     at skylights.core.Game.run(Game.java:264) 
     at skylights.core.Game.main(Game.java:287) 
Caused by: org.msgpack.template.builder.TemplateBuildException: Cannot compile: 
{ 
    if (!$3 && $1.trySkipNil()) { 
    return null; 
    } 
    skylights.util.SerVec2 _$$_t; 
    if ($2 == null) { 
    _$$_t = new skylights.util.SerVec2(); 
    } else { 
    _$$_t = (skylights.util.SerVec2) $2; 
    } 
    $1.readArrayBegin(); 
    _$$_t.x = $1.readFloat(); 
    _$$_t.y = $1.readFloat(); 
    $1.readArrayEnd(); 
    return _$$_t; 
} 

     at org.msgpack.template.builder.BuildContext.build(BuildContext.java:73) 
     at org.msgpack.template.builder.DefaultBuildContext.buildTemplate(DefaultBuildContext.java:56) 
     at org.msgpack.template.builder.JavassistTemplateBuilder.buildTemplate(JavassistTemplateBuilder.java:118) 
     at org.msgpack.template.builder.AbstractTemplateBuilder.buildTemplate(AbstractTemplateBuilder.java:61) 
     at org.msgpack.template.TemplateRegistry.buildAndRegister(TemplateRegistry.java:503) 
     ... 19 more 
Caused by: javassist.CannotCompileException: [source error] no such constructor 
     at javassist.CtBehavior.setBody(CtBehavior.java:417) 
     at javassist.CtBehavior.setBody(CtBehavior.java:383) 
     at javassist.CtNewMethod.make(CtNewMethod.java:138) 
     at org.msgpack.template.builder.BuildContext.buildReadMethod(BuildContext.java:144) 
     at org.msgpack.template.builder.BuildContext.build(BuildContext.java:65) 
     ... 23 more 
Caused by: compile error: no such constructor 
     at javassist.compiler.MemberCodeGen.atMethodCallCore2(MemberCodeGen.java:593) 
     at javassist.compiler.MemberCodeGen.atMethodCallCore(MemberCodeGen.java:575) 
     at javassist.compiler.MemberCodeGen.atNewExpr(MemberCodeGen.java:302) 
     at javassist.compiler.ast.NewExpr.accept(NewExpr.java:73) 
     at javassist.compiler.CodeGen.atAssignCore(CodeGen.java:860) 
     at javassist.compiler.CodeGen.atVariableAssign(CodeGen.java:793) 
     at javassist.compiler.CodeGen.atAssignExpr(CodeGen.java:747) 
     at javassist.compiler.CodeGen.atStmnt(CodeGen.java:332) 
     at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50) 
     at javassist.compiler.CodeGen.atStmnt(CodeGen.java:351) 
     at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50) 
     at javassist.compiler.CodeGen.atIfStmnt(CodeGen.java:391) 
     at javassist.compiler.CodeGen.atStmnt(CodeGen.java:355) 
     at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50) 
     at javassist.compiler.CodeGen.atStmnt(CodeGen.java:351) 
     at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50) 
     at javassist.compiler.CodeGen.atMethodBody(CodeGen.java:292) 
     at javassist.compiler.Javac.compileBody(Javac.java:223) 
     at javassist.CtBehavior.setBody(CtBehavior.java:409) 
     ... 27 more 

回答

1

要回答我的問題:默認的構造器缺少SerVec。爲了能夠序列化,MessagePack需要默認的構造函數可用。

+0

你的意思是說你從類聲明中刪除了所有的構造函數,只留下了公共字段? – danem 2014-10-29 02:15:58