How do annotations prevent mutations of an array parameter?
Is there simply an array copy happening during each call to value(), or is it something more complex?
Yes, the array is copied.
Annotations are a special kind of interface
type. (JLS)
They are implemented by some Proxy
classes at runtime.
You can debug it if you set breakpoint at Proxy.newProxyInstance()
.
Invocations on annotation are intercepted by AnnotationInvocationHandler
which copies arrays:
if (result.getClass().isArray() && Array.getLength(result) != 0)
result = cloneArray(result);
You are right, it returns a copy each time to ensure it is not changed.
In a future version of Java, this copy might be optimised away.