• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Annotation in Java

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,

I`d like to create my own annotations to annotate some parameter and/or some local variable.
To write the annotation is not the problem, the problem is to get the information of them at the Runtime.
I could only get some informations from annotated methods, but not from local variable or parameter.

Has anybody experience with such kind of annotation and could help me?

thanks a lot
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Klug wrote:...To write the annotation is not the problem, the problem is to get the information of them at the Runtime.
I could only get some informations from annotated methods, but not from local variable or parameter.


What kind of information you expect and what is the exact problem you are trying to solve? Please, TellTheDetails.

And welcome to JavaRanch
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Klug wrote:Hi everybody,

I`d like to create my own annotations to annotate some parameter and/or some local variable.
To write the annotation is not the problem, the problem is to get the information of them at the Runtime.
I could only get some informations from annotated methods, but not from local variable or parameter.

Has anybody experience with such kind of annotation and could help me?

thanks a lot


Annotations can only be used at runtime using reflection. That already rules out local variables, these are not available in reflection. Method parameters are, through java.lang.reflect.Method (which in turn is available through java.lang.Class). You'll need method getParameterAnnotations(). This returns an Annotation[][]. The array is indexed with parameter order. So to get the annotations of the third parameter, you'll need getParamaterAnnotations()[2].
 
Tom Klug
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:
What kind of information you expect and what is the exact problem you are trying to solve? Please, TellTheDetails.

And welcome to JavaRanch



Thanks for welcome!

The information of the annotation is the something about the storage allocation. The programmer should use this annotation to decide by himself if the new object allocates to the stack or the heap (I do some garbage collection staff and try to discharge the garbage collector and the escape analysis). So I need a way to transport the decision from the source code to the runtime, where I need this.
For this I used the specific target and the rentention


To annotate is not the problem, only to get the information at runtime seems impossible.

Rob Prime wrote:
You'll need method getParameterAnnotations(). This returns an Annotation[][]. The array is indexed with parameter order. So to get the annotations of the third parameter, you'll need getParamaterAnnotations()[2].



Thanks, it works! So I cast the annotation and get my information.
So for what is ElementType.LOCAL_VARIABLE ? If I understood it correct, then it constrain this annotation to a local variable. It makes no appreciation to annotate an local variable and can not use this annotation?!
Do you have any other idea how to transport a information for a local variable form source to runtime?

thanks a lot
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is one annotation that can make sense for local variables: @SuppressWarnings. But I do believe that any non-system annotations, that will not be treated specially by the compiler (like @SuppressWarnings, @Override, @Deprecated do), will make any sense for local variables. There is nothing that can inspect them afterwards.
 
Tom Klug
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:There is one annotation that can make sense for local variables: @SuppressWarnings. But I do believe that any non-system annotations, that will not be treated specially by the compiler (like @SuppressWarnings, @Override, @Deprecated do), will make any sense for local variables. There is nothing that can inspect them afterwards.



Sorry, i should have to make it clear.
I want to write my own annoation, like


And now I want to use this, like


Now the compiler should read this informations at run time. It works with the parameter of the method m (which is a), but not with the local variable (which is b). So the question is: why it does not work and is there another possibility to transport some information from source code to run time?

sorry, if it was not so clearly formulated!

thanks for help
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Klug wrote:Now the compiler should read this informations at run time.


Say what now? The compiler is no longer involved at runtime. The JVM is, but the compiler is done once it's created the class files.

It works with the parameter of the method m (which is a), but not with the local variable (which is b). So the question is: why it does not work


Because reflection doesn't go into methods. It stops at the signatures. As far as I know debuggers are the only tools available to go into methods during runtime. No idea how these work though, but I still doubt they will be able to help you out.

and is there another possibility to transport some information from source code to run time?


As far as I know, no.
 
Tom Klug
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thanks for answer.
I think I found a solution at Mathias Ricken`s Homepage.
Mathias Ricken implements the annoation for local variables into Java 1.5, I will try it out in the next few days.

btw i mean a jit compiler, which compiles at run time and makes dynamic optimizations.

thanks for your help
 
Dinner will be steamed monkey heads with a side of tiny ads.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic