• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Annotations and reuse

 
Julio Faerman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After fixing your code (missing import statements), I am unable to produce a NullPointerException:
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Julio Faerman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Julio Faerman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic