如何引用外部"这个" Java中反序列化后来自内部类的对象?

时间:2015-11-25 06:23:59

标签: java serialization

我有一个用内部类序列化的对象。在反序列化之后,内部类调用外部类的一个方法,该方法引用"这个",这会给我带来问题。我的代码:

// Outer Class
public AuctionItem getInstance(){
    return this;
}

private class ItemCallBack extends TimerTask {

    @Override
    public void run() {
        try {
            AuctionServerIntf aServer = (AuctionServerIntf) Naming.lookup("rmi://"+AuctionServer.HOST+":"+AuctionServer.PORT+"/AuctionServer");
            aServer.notifyItemEnded(getInstance()); // <--- Exception is thrown here
        } catch (RemoteException | MalformedURLException | NotBoundException e) {
            e.printStackTrace();
        }
    }

}

正如您所看到的,我想将对外部类的引用传递给aServer.notifyItemEnded()方法,它在序列化之前工作正常,但之后会抛出连接异常。不知道怎么解决这个问题?欢呼声。

2 个答案:

答案 0 :(得分:0)

尝试预先设置外部类的类名:

aServer.notifyItemEnded(AuctionItem.this.getInstance()); 

答案 1 :(得分:0)

尝试使用为作业提供的Java语法:

 public static String a(String paramString)
  {
    try
    {
      str = new String(a.a(paramString), "UTF-8");
      return str;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      while (true)
      {
        localUnsupportedEncodingException.printStackTrace();
        String str = "Error";
      }
    }
  }