Are Java annotations serializable?

annotations, java, serialization

Solution

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.

Problem

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?

Original source