Java中匿名类的序列化 [英] Serialization of anonymous class in Java

查看:168
本文介绍了Java中匿名类的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Java中对匿名类进行searialize / desearialize?

Is it possible to searialize/desearialize anonymous class in Java?

示例:

ByteArrayOutputStream operationByteArrayStream = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(operationByteArrayStream);
oos.writeObject(new Task() {
    public void execute() {
        System.out.println("Do some custom task"));
    }
});

我的问题是我想做一些自定义管理任务,这样我就不需要发布了为每一项任务。所以我正在尝试做的是 - 通过Groovy脚本引擎通过HTTP端点发布自定义任务并将它们序列化为db以及时运行它们。

My problem is that I want to do some custom admin tasks so that i don't need a release for every task. So what i'm trying to do - is via Groovy scripting engine post custom task via HTTP endpoint and serialize them into db to run them in time.

推荐答案

危险可能。匿名类的名称/数量由编译器生成,并基于它们在文件中出现的顺序。例如如果你交换两个类的顺序,他们的名字也会交换。 (类按名称反序列化)

It is possible, by dangerous. The name/number of anonymous classes is generated by the compiler and is based on the order they appear in the file. e.g. if you swap the order of two classes, their names will swap as well. (Classes are deserialized by name)

这篇关于Java中匿名类的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆