为什么 java.io.Serialized 在 Java 5 中没有被弃用?

发布于 2024-11-16 03:44:23 字数 848 浏览 3 评论 0原文

在 Java 5 之前,没有注释。因此,您无法将元数据添加到类中。

要将类标记为可序列化,您必须实现 Serialized 接口(这只是一个标记),并在需要时使用进一步的 transient 关键字将字段标记为不可序列化,例如:

public class MyClass implements Serializable {
   ...
   private transient Bla field;
   ...
}

现在理论上您可以使用注释(这是一个完美的使用对于他们来说)并且有:

@Serializable
public class MyClass {
   ...
   @Transient
   private Bla field;
   ...
}

但是Interface 和关键字并未被废弃,Java 5 中也没有添加注释来替代它们。

这次决定保留接口和关键字是出于什么考虑?

当然,存在与 Java 5 之前的代码的兼容性问题,但在某些时候,这个问题将会结束(例如,与泛型的新功能相关,JLS 指定 Java 编程语言的未来版本可能会禁止使用原始类型)。那么为什么不为序列化注释也做好准备呢?

有什么想法吗? (尽管我更喜欢具体的参考资料:D,无法找到)

In pre Java 5, there were no annotations. As a result you could not add metadata to a class.

To mark a class as serializable, you had to implement the Serializable interface (which is just that, a marker) and use further transient keywords to mark a field as non serializable if needed, something like:

public class MyClass implements Serializable {
   ...
   private transient Bla field;
   ...
}

Now you could theoretically make use of annotations (this is a perfect use for them) and have:

@Serializable
public class MyClass {
   ...
   @Transient
   private Bla field;
   ...
}

But the interface and the keyword were not deprecated and no annotation was added in Java 5 to replace them.

What are the considerations for this decision to keep the interface and the keyword?

Off course there is the issue of compatibility with pre Java 5 code, but at some point that will end (e.g. related to the new features of generics, the JLS specifies that It is possible that future versions of the Java programming language will disallow the use of raw types). So why not prepare the way for serialized annotations also?

Any thoughts? (although I would prefer concrete references :D which I was unable to find)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魔法唧唧 2024-11-23 03:44:23

该接口存在,因此可以定义方法来接受 Serialized 类型的对象:

public void registerObject(Serializable obj);

The interface is there so methods can be defined to accept objects of type Serializable:

public void registerObject(Serializable obj);
停滞 2024-11-23 03:44:23

当然存在与 Java 5 之前的代码的兼容性问题......

是的。这就是问题的根源。

  • 如果他们@deprecated(例如)Java 5 中的Serialized 接口,那么许多旧的 Java 5 之前的代码将发出警告(或错误,具体取决于编译器开关) )。

  • 使用Serialized实际上并没有什么问题,“强迫”人们替换它很烦人。 (人们有更好的事情要做...)

  • 当人们在代码中“修复”了这个问题时,它将不再使用 Java 5 之前的编译器进行编译,也不会在 Java 5 之前的 JVM 上运行。

  • 做一些让编译器系统地“狼来了”的事情是一个坏主意。

...但在某个时候这将会结束。

事实上,这种情况实际发生的可能性(IMO)很小。据我所知,Sun / Oracle从未删除过已弃用的功能。甚至没有像 Thread.stop() 之类的危险的。


作为脚注,Go 开发人员正在采取不同的方法来解决这个问题。当他们想要更改语言或库功能时,他们就会这么做。他们提供了一个转换器,它将根据需要自动重写您的代码。

Off course there is the issue of compatibility with pre Java 5 code ...

Yes. This is the root of the problem.

  • If they @deprecated the Serializable interface in (say) Java 5, then lots of old pre-Java 5 code would give warnings (or errors depending on compiler switches).

  • There's nothing actually wrong with using Serializable and "forcing" people to replace it is annoying. (People have better things to do ...)

  • When people have "fixed" this in their code, it will no longer compile using a pre-Java 5 compiler, or run on a pre-Java 5 JVM.

  • It is a bad idea to do things that make the compiler systematically "cry wolf".

... but at some point that will end.

In reality, the chance of this actually happening is (IMO) small. As far as I'm aware, Sun / Oracle have never removed a deprecated feature. Not even dangerous ones like Thread.stop() and friends.


As a footnote, the Go developers are taking a different approach to this problem. When they want to change a language or library feature, they just do it. They provide a converter that will automatically rewrite your code as required.

心碎无痕… 2024-11-23 03:44:23

Serializedtransient 确实是两个可以被注释取代的东西。

它们尚未被弃用,可能是因为有很多程序使用 Serialized,如果编译器突然开始吐出数千个代码,那么对于数百万人来说,这将是令人烦恼的弃用警告。

标准 Java API 中还有很多其他东西可能早就被弃用了 - 例如,遗留集合类 VectorHashtable (我相信你可以轻松找到更多东西)。还有其他一些东西可以使用注释来实现(例如关键字 volatile、strictfp 和 synchronized)。

Serializable and transient are indeed two things that could have been replaced by annotations.

They haven't been deprecated probably because there are a lot of programs that use Serializable and it would have been annoying for millions if developers if the compiler would suddenly start spewing out thousands of deprecation warnings.

There are lots of other things in the standard Java API that could have been deprecated long ago - for example, the legacy collection classes Vector and Hashtable (and I'm sure you can easily find many more things). And there are other things that could have been implemented using annotations (for example the keyword volatile, strictfp and synchronized).

佞臣 2024-11-23 03:44:23

弃用是针对那些积极有害的事物。你的建议是强迫八年现有 Java 代码(当时)的作者重写它,没有任何好处,只是为了关闭弃用编译器警告,回到他们在 Java 1.4 中拥有的完全正确的代码。那是没有用的。

Deprecation is for things that are actively harmful. What you're suggesting is forcing authors of eight years of existing Java code (at the time) to rewrite it, for no advantage, just to shut up the deprecation compiler warnings, to get back to the fully correct code they had with Java 1.4. That would not be useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文