[JSR308] Runtime access to JSR-308 annotations

David Goodenough david.goodenough at linkchoose.co.uk
Wed Sep 17 06:33:46 EDT 2008

On Tuesday 16 September 2008, Michael Ernst wrote:
> Artemus Harper gave some feedback about specific techniques you can use for
> traversing the AST during type-checking.  I want to add a reply in case you
> were asking a different question, about reflection at run-time.
> > I have a need to find the annotations that apply to an object (or rather
> > to the definition of that object, rather than finding the annotations
> > that apply to named methods/classes/packages/fields.
> In Java, there is no run-time representation of the types of the variables
> that have previously referred to an object.  JSR 308 enriches types, but it
> does not change that fundamental property.
> Consider the following code.
>   Number n = new Integer(22);
>   Object o = n;
> If you asked for the type of the object that is pointed to by "o", would
> you expect a different answer than that for the object that is pointed to
> by "n"?  The class of that object can be queried -- it is Integer -- but
> that's not the declared type of either of the variables that refers to it.
> More generally, Java doesn't maintain a run-time representation of types.
> If you wanted to add such a representation, you could do so, but JSR 308
> doesn't impose that run-time burden on all Java programs.  Its type
> annotations are intentionally designed to be implementable without any
> run-time representation.
> > I suppose what is needed is a getAnnotation method on Object which would
> > find the annotation on wherever this object came from.  Alternatively I
> > need a means to get back to the field/parameter etc that this object came
> > from.
> An object may come from many fields/parameters, so I'm not sure which one
> you mean.  The most recent?  The first variable that referred to the
> variable?  Unless you are asking about the declaration of a variable as
> opposed to anything about objects, in which case Artemus's answer is to the
> point.
> Perhaps an example of what you want to do would help in clearing up the
> confusion arising from your question.
> > Reading the JSR-308 document (java-annotation-design.pdf) section C.3
> > only extends the run time annotation only in minor ways, which does not
> > really seem to reflect (sorry) the other additions you have made with
> > this JSR.
> Can you give an example here, too?
>                     -Mike

I was trying to cheat.  I desparately need proper property support in Java
(for bean binding - I hate having to duplicate field names in strings as it is
error prone and impossible for IDEs or compilers to check).  So I thought that
I could add an annotation to the field and hoped that 308 would have to 
provide a means to get back to the annotation from the object.  But it would
appear that this is not easily attained and I grant you is not what 308 is
designed to do.

As it happens I think I have found another way of solving that problem so
I will pursue that instead.

As an aside, one thing I considered while thinking about 308 was adding 
annotations at run time.  Why you ask would I want to do such a thing, well
getting from classes to fields is easy (getDeclaredFields()), so I thought I 
would have a method (annotate) which added a property annotation to each
field of the classes I needed properties for and then use what I assumed 
(wrongly) must exist - i.e the means to get backwards from the object) to 
get at the annotation and thus the property.  Actually adding the annotation
seemed simple enough, using reflection although messing with the private
parts of someone else's class is never nice.  This got me thinking about 
whether this was a reasonable thing to ask to do (I think it might be) and
therefore whether all that was needed was a means (a new set of methods) 
in the various places that hold annotations.



More information about the JSR308 mailing list