使用 val 的 Scala 构造函数不能很好地与序列化框架配合使用
Scala constructors using val not playing nicely with serialization framework
我遇到了一些 Scala 代码的问题,在我开始将其视为网络问题之前,我试图排除错误的 class 设计(好吧,构造函数设计)。
所以我有一个名为 Hail
:
的 Scala class 模型
class Hail(val handle : String, val message : String) extends BaseMessage {
def this() {
this("default_user", "default_message")
}
}
abstract class BaseMessage extends AbstractMessage(true) {
}
// This is a 3rd party open source class written in Java
public abstract class AbstractMessage implements Message
{
private transient boolean reliable = true;
protected AbstractMessage()
{
}
protected AbstractMessage( boolean reliable )
{
this.reliable = reliable;
}
public Message setReliable(boolean f)
{
this.reliable = f;
return this;
}
public boolean isReliable()
{
return reliable;
}
}
// This is also a 3rd party open source class written in Java
public interface Message
{
public Message setReliable(boolean f);
public boolean isReliable();
}
在运行时,这个 class 的实例被序列化(到二进制),通过网络发送到服务器,在那里它们被反序列化(返回到 Hail
个实例)并被处理。
所以我的客户端代码如下所示:
val h1 : Hail = new Hail("user1", "Hello!")
val h2 : Hail = new Hail("user2", "Aloha!")
val h3 : Hail = new Hail("user3", "Bien venu mes amigos")
client.send(h1)
client.send(h2)
client.send(h3)
当服务器收到这些消息时,它会将它们的 handle/message 组合打印到 STDOUT。我收到的消息如下:
Server received a Hail: default_user, default_message
Server received a Hail: default_user, default_message
Server received a Hail: default_user, default_message
而不是我期望的:
Server received a Hail: user1, Hello!
Server received a Hail: user1, Aloha!
Server received a Hail: user3, Bien venu mes amigos
同样,这个可能是一个networking/serialization/server-side问题。但在我走那条路之前,我想确保我的 Hail
class 已经正确写入。
我正在使用的序列化框架要求所有消息(例如Hail
)具有无参数构造函数(因此我在上面提供了一个)。所以在我看来,我的其他构造函数有问题,也许服务器默认调用无参数构造函数,因为它不能使用其他任何东西。
我反编译了我的 Hail
class 并看到以下内容:
@ScalaSignature(bytes="<lots of bytes here omitted for brevity")
public class Hail
extends BaseMessage
{
private final String handle;
public String handle()
{
return this.handle;
}
public String message()
{
return this.message;
}
public Hail()
{
this("default_user", "default_message");
}
public Hail(String handle, String message) {}
}
马上有几件事对我来说 curious/suspicious:
- 虽然我看到
private final String handle
(这是我想要的!),但我没有看到倒数 private final String message
字段...
- 此外,第二个构造函数 (
public Hail(String handle, String message)
) 是 empty/undefined。 这可能是我问题的根源。
所以我问,我如何重构 Hail
的源代码,以便下面的最终结果是 将 反编译为的字节码:
@ScalaSignature(bytes="<lots of bytes here omitted for brevity")
public class Hail
extends BaseMessage
{
private final String handle;
public String handle()
{
return this.handle;
}
public String message()
{
return this.message;
}
public Hail()
{
this("default_user", "default_message");
}
public Hail(String handle, String message)
{
this.handle = handle;
this.message = message;
}
}
有什么想法吗?
问题是serializer server-side使用了no-arg constructor,然后改变class.
中可变参数的值
然而,scala val
s 是不可变的,因此它们被编译为 class 的 final
参数,因此不能被改变。因此对象使用默认值实例化,然后保留这些值,因为它们不能被改变。
我建议使用与 scala 兼容的序列化程序,但更简单的解决方案是通过将属性声明为 var
s 而不是 val
s 来允许更改属性。
我遇到了一些 Scala 代码的问题,在我开始将其视为网络问题之前,我试图排除错误的 class 设计(好吧,构造函数设计)。
所以我有一个名为 Hail
:
class Hail(val handle : String, val message : String) extends BaseMessage {
def this() {
this("default_user", "default_message")
}
}
abstract class BaseMessage extends AbstractMessage(true) {
}
// This is a 3rd party open source class written in Java
public abstract class AbstractMessage implements Message
{
private transient boolean reliable = true;
protected AbstractMessage()
{
}
protected AbstractMessage( boolean reliable )
{
this.reliable = reliable;
}
public Message setReliable(boolean f)
{
this.reliable = f;
return this;
}
public boolean isReliable()
{
return reliable;
}
}
// This is also a 3rd party open source class written in Java
public interface Message
{
public Message setReliable(boolean f);
public boolean isReliable();
}
在运行时,这个 class 的实例被序列化(到二进制),通过网络发送到服务器,在那里它们被反序列化(返回到 Hail
个实例)并被处理。
所以我的客户端代码如下所示:
val h1 : Hail = new Hail("user1", "Hello!")
val h2 : Hail = new Hail("user2", "Aloha!")
val h3 : Hail = new Hail("user3", "Bien venu mes amigos")
client.send(h1)
client.send(h2)
client.send(h3)
当服务器收到这些消息时,它会将它们的 handle/message 组合打印到 STDOUT。我收到的消息如下:
Server received a Hail: default_user, default_message
Server received a Hail: default_user, default_message
Server received a Hail: default_user, default_message
而不是我期望的:
Server received a Hail: user1, Hello!
Server received a Hail: user1, Aloha!
Server received a Hail: user3, Bien venu mes amigos
同样,这个可能是一个networking/serialization/server-side问题。但在我走那条路之前,我想确保我的 Hail
class 已经正确写入。
我正在使用的序列化框架要求所有消息(例如Hail
)具有无参数构造函数(因此我在上面提供了一个)。所以在我看来,我的其他构造函数有问题,也许服务器默认调用无参数构造函数,因为它不能使用其他任何东西。
我反编译了我的 Hail
class 并看到以下内容:
@ScalaSignature(bytes="<lots of bytes here omitted for brevity")
public class Hail
extends BaseMessage
{
private final String handle;
public String handle()
{
return this.handle;
}
public String message()
{
return this.message;
}
public Hail()
{
this("default_user", "default_message");
}
public Hail(String handle, String message) {}
}
马上有几件事对我来说 curious/suspicious:
- 虽然我看到
private final String handle
(这是我想要的!),但我没有看到倒数private final String message
字段... - 此外,第二个构造函数 (
public Hail(String handle, String message)
) 是 empty/undefined。 这可能是我问题的根源。
所以我问,我如何重构 Hail
的源代码,以便下面的最终结果是 将 反编译为的字节码:
@ScalaSignature(bytes="<lots of bytes here omitted for brevity")
public class Hail
extends BaseMessage
{
private final String handle;
public String handle()
{
return this.handle;
}
public String message()
{
return this.message;
}
public Hail()
{
this("default_user", "default_message");
}
public Hail(String handle, String message)
{
this.handle = handle;
this.message = message;
}
}
有什么想法吗?
问题是serializer server-side使用了no-arg constructor,然后改变class.
中可变参数的值然而,scala val
s 是不可变的,因此它们被编译为 class 的 final
参数,因此不能被改变。因此对象使用默认值实例化,然后保留这些值,因为它们不能被改变。
我建议使用与 scala 兼容的序列化程序,但更简单的解决方案是通过将属性声明为 var
s 而不是 val
s 来允许更改属性。