participate


Annotation Processing - Local Variable annotation
<<   Back to Forum  |   Give us Feedback
This topic has 6 replies on 1 page.
mohsenmm
Posts:1
Registered: 5/3/05
Local Variable annotation   
Sep 16, 2005 2:35 AM

 
hi
I've written some lines of code:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.LOCAL_VARIABLE)
public @interface Testable {
String test() default "haha";
}

//-----------------------------------

public class TestAnnotation {
public static void main(String[] args) {
@Testable
String str = new String();

printAnnotation(str);
}

static void printAnnotation(Object str) {
// ... ?
}
}



in printAnnotation i want to see if the passed variable is annotated with @Testable or not.

But now I dont know how to complete printAnnotation()
could anyone help me ?
tanx
 
subartsh
Posts:90
Registered: 6/30/03
Re: Local Variable annotation   
Sep 19, 2005 1:38 PM (reply 1 of 6)  (In reply to original post )

 
You cannot:

Currently annotations on local variables do not exist beyond the source code.

And if they were I dout that accessing them in this way would be easy, for the same reason for determing what method called your method.
 
fischert
Posts:164
Registered: 3/5/00
Re: Local Variable annotation   
Oct 12, 2005 11:48 PM (reply 2 of 6)  (In reply to #1 )

 
Currently annotations on local variables do not exist
beyond the source code.

However, the JLS defines no special processing of the local variable annotation. So IMHO the fact that the local variable annotation are not processed according to their Retention Policy is a bug in a java compiler.
 
seligman
Posts:11
Registered: 8/15/05
Re: Local Variable annotation   
Oct 13, 2005 12:24 AM (reply 3 of 6)  (In reply to #2 )

 
There is a limitation being exposed here, but no bug. The limitation
is in the classfile format. There was no straightforward way to
represent local variables and their annotations, so no such
representation was defined. Accordingly, the JLS 3 says this about
annotation types with RUNTIME retention:

"An annotation on a local variable declaration is never
retained in the binary representation."
 
fischert
Posts:164
Registered: 3/5/00
Re: Local Variable annotation   
Oct 13, 2005 1:14 AM (reply 4 of 6)  (In reply to #3 )

 
"An annotation on a local variable declaration is
never
retained in the binary representation."

You are right. I didn't find this sentence.
 
mgricken
Posts:17
Registered: 9/2/02
Re: Local Variable annotation   
Sep 10, 2006 4:11 AM (reply 5 of 6)  (In reply to #3 )

 
There was no straightforward way to represent local variables and their
annotations, so no such representation was defined.

To me, that's a completely bogus argument. Sun easily could have defined another attribute, similar to the LocalVariableTable, that contains information about a local variable and its annotation.

I was really excited about all the possibilities of Java annotations... until I noticed this grave oversight.
 
mgricken
Posts:17
Registered: 9/2/02
Re: Local Variable annotation   
Oct 10, 2006 3:19 AM (reply 6 of 6)  (In reply to #5 )

 
Hi!

Using material Sun provided under the Java Research License (JRL), I have developed a modified version of the Java compiler that encodes annotations on local variables into class files.

For more information, I would like to direct your attention to the following thread on this forum:

http://forum.java.sun.com/thread.jspa?threadID=775449

Thank you.

Mathias Ricken
 
This topic has 6 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 129

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums