【问题标题】:Save Jcodemodel Object after exit退出后保存 Jcodemodel 对象
【发布时间】:2014-01-22 22:52:13
【问题描述】:

我对 JCodeModel (SUN) 有疑问。我的程序每天都在运行,我想为当前运行之前创建的类添加一些功能。

JcodeModel 支持这个吗?如果没有,是否可以选择将 JCodemodel 对象保存在外部文件中,加载之前的 JcodeModel,然后添加新功能?

谢谢。

【问题讨论】:

  • 您的程序是否生成了您想要通过 JCodeModel 添加功能的原始类?
  • 您的程序最初是从什么生成的? IE:你的程序是否读入了一个模型或描述符来指导它如何生成代码?

标签: java code-generation generated-code sun-codemodel jcodemodel


【解决方案1】:

您可以使用ObjectOutputStream 将实例保存到文件中,然后使用ObjectInputStream 读取并实例化它。只要你控制了系统并且可以确保版本不会在一夜之间改变,这应该是安全的(虽然不寻常)。

This tutorial 演示如何使用它:

import java.io.*;
public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
     String s = "Hello world!";
     int i = 897648764;
     try {

       // create a new file with an ObjectOutputStream
       FileOutputStream out = new FileOutputStream("test.txt");
       ObjectOutputStream oout = new ObjectOutputStream(out);

       // write something in the file
       oout.writeObject(s);
       oout.writeObject(i);

       // close the stream
       oout.close();

       // create an ObjectInputStream for the file we created before
       ObjectInputStream ois =
             new ObjectInputStream(new FileInputStream("test.txt"));

       // read and print what we wrote before
       System.out.println("" + (String) ois.readObject());
       System.out.println("" + ois.readObject());

  } catch (Exception ex) {
     ex.printStackTrace();
  }
}
}

【讨论】:

  • 我遇到异常:java.io.NotSerializableException: com.sun.codemodel.JCodeModel
  • .... JCodeModel 不可序列化...这很糟糕,那么这将不起作用...对不起!
猜你喜欢
  • 2016-08-14
  • 1970-01-01
  • 2015-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 2016-12-11
相关资源
最近更新 更多