1

我有一个非常简单的 java 类,它代表一个 2d 向量。然而,当我运行代码将其序列化为消息包时,它会产生运行时错误。序列化这个类应该是真正的基本行为,应该可以工作,对吧?我发现没有其他人有类似的问题。

代码和异常如下。我在 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
4

1 回答 1

1

回答我自己的问题: SerVec 缺少默认构造函数。为了能够序列化,MessagePack 需要默认构造函数可用。

于 2013-02-19T09:05:33.247 回答