File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Annotations and reuse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Annotations and reuse" Watch "Annotations and reuse" New topic
Author

Annotations and reuse

Julio Faerman
Greenhorn

Joined: Sep 01, 2006
Posts: 3
Hi,

I have an annotation i whish to use in several methods, with the same parameters. For example:



So i tried to reuse the parameters, as i do not want to duplicate code:





Which, to my surprise, resulted in a NullPointerException. Can anyone explain me why?

Full code follows:
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
After fixing your code (missing import statements), I am unable to produce a NullPointerException:


Tony Morris
Java Q&A (FAQ, Trivia)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
As a side note, the issue of solving the "duplication problem for method parameters" is unsolvable in Java. In FP languages, this is solved with function currying, but Java does not have an equivalent. I have read various articles and examples of emulation of function currying but they are all very flawed (unless you consider a case by case implementation not flawed - an absurdity in light of the objectives of currying).

Even in functional languages, there is an assumption that the data type on its own is a complete representation of the requirements of the function contract - which is not necessarily true - a problem that I think is solvable and predict it will be soon (I know of various people with much more time than I who are working on it and solicit feedback often).
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Julio, if you are somehow able to observe this NullPointerException, it would be very useful for you to study the stack trace and find out what line of code is throwing the exception.


"I'm not back." - Bill Harding, Twister
Julio Faerman
Greenhorn

Joined: Sep 01, 2006
Posts: 3
The code that throws the NPE is commented in the original post.

Here it goes the bad code:



I think it is because the annotation and its parms are processed before the static final array.
I pay a beer if someone can reuse the params
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Your code must compile before it can throw a NPE.
Please make your code compile.

This code compiles and executes without a NPE:

[ September 05, 2006: Message edited by: Tony Morris ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:
There's really no shortcut for you here. Sorry.


Besides code generation, of course, which might be a viable option.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Julio Faerman
Greenhorn

Joined: Sep 01, 2006
Posts: 3
Well... it compiled using eclipse and jdk 1.5_08.
In any case i understood why it is not possible. Thank you all very much.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Julio Faerman:
Well... it compiled using eclipse and jdk 1.5_08.


Seconded, it compiles in Eclipse 3.3 M1 and produces a NPE. Might be worth a bug report...
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Ilja Preuss:


Seconded, it compiles in Eclipse 3.3 M1 and produces a NPE. Might be worth a bug report...


Can this be reproduced outside of Eclipse? If so, can you provide any more details? e.g.
> javac -J-version
> java -version
> uname -a
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Annotations and reuse