简体   繁体   English

Java注释是否可序列化?

[英]Are Java annotations serializable?

The Java Annotation interface does not extend Serializable . Java Annotation接口不扩展Serializable However, Java annotation values are serializable (implemented using java.lang.reflect.Proxy , with a serializable invocation handler). 但是,Java注释值是可序列化的(使用java.lang.reflect.Proxy实现,带有可序列化的调用处理程序)。

Is this guaranteed anywhere? 这有保证吗? My search-foo is failing to find a reference. 我的search-foo未能找到参考。 Or if I need to serialize annotation instances safely, do I need to create my own serialization proxies? 或者如果我需要安全地序列化注释实例,我是否需要创建自己的序列化代理?

Annotation objects returned by methods of java.lang.reflect.AnnotatedElement are Serializable . java.lang.reflect.AnnotatedElement方法返回的注释对象是Serializable This is what API says : This interface allows annotations to be read reflectively. 这就是API所说的此接口允许以反射方式读取注释。 All annotations returned by methods in this interface are immutable and serializable . 此接口中的方法返回的所有注释都是不可变的和可序列化的

All classes capable of returning annotation objects (Class, Constructor, Field, Method, Package, Parameter) implement AnnotatedElement and are obliged to create / return Serializable objects by the above contract. 所有能够返回注释对象的类(类,构造函数,字段,方法,包,参数)都实现了AnnotatedElement,并且必须通过上述契约创建/返回Serializable对象。

Annotations are part of the class definition, and thus would never be written to a serialization stream (at least not with standard java serialization) when serializing an instance of a class which has annotations. 注释是类定义的一部分,因此在序列化具有注释的类的实例时,永远不会将其写入序列化流(至少不使用标准java序列化)。

UPDATE: I guess i missed the point of the original question which was referring to specifically serializing an instance of an actual Annotation. 更新:我想我错过了原始问题的意思,它指的是专门序列化实际注释的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM