简体   繁体   中英

Are Java annotations serializable?

The Java Annotation interface does not extend Serializable . However, Java annotation values are serializable (implemented using java.lang.reflect.Proxy , with a serializable invocation handler).

Is this guaranteed anywhere? My search-foo is failing to find a reference. 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 . This is what API says : This interface allows annotations to be read reflectively. 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.

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.

UPDATE: I guess i missed the point of the original question which was referring to specifically serializing an instance of an actual Annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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