posted 18 years ago
If you (Julio) really can get that code to compile and throw an NPE, then you woud seem be using a rather unusual JDK. Perhaps some early version of JDK 5, like a beta? I recommend upgrading to the latest stable release. I get a compile error for your code using Sun's JDK 1.5.0_08 as well as 1.6.0_beta2 for Windows. Same for comparable versions on the Mac. The error is always:
This error is rather misleading as both params and myparams are declared of type SomeAnnotParam[], so it seems like this should work. Why doesn't it?
The thing is, you are simply not allowed to initialize an array in an annotation type to another array. This is intentional. The reason is that they want to only allow yout to initialize annotation fields using "constant", immutable values. Here "constant" is in quotes because it's not exactly the same as the definition given in JLS 15.28, but it's similar. They don't want you to initialize with
because other code could change the contents of the array, which would create confusion:
(Remember, arrays are never immutable.)
Instead, the syntax is designed to force you to use an array initializer that spells out each value in the array:
Now there's no way to change the value of params after it's initialized. (As far as I know, anyway - there are a couple ways you could still try, but for various reasons, they don't work.)
Anyway, what it all boils down to is that no, you're not allowed to do what you're trying to do here. You've really got to list the multiple array values in an array initializer (using {}) in the annotation. There's really no shortcut for you here. Sorry.
[ September 05, 2006: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister