aspose file tools*
The moose likes Java in General and the fly likes Annotation in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Annotation in Java" Watch "Annotation in Java" New topic
Author

Annotation in Java

Tom Klug
Greenhorn

Joined: Nov 01, 2010
Posts: 4
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
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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].


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tom Klug
Greenhorn

Joined: Nov 01, 2010
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Nov 01, 2010
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Nov 01, 2010
Posts: 4
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Annotation in Java
 
Similar Threads
final variable declared in a loop
Naming conventions advice
@EJB static
boolean remove(Appointment appt)
setting parameters while redirecting to another action