Local Variable annotation

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

[853 byte] By [mohsenmma] at [2007-10-2 0:00:18]
# 1
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.
subartsha at 2007-7-15 15:54:28 > top of Java-index,Core,Core APIs...
# 2

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

fischerta at 2007-7-15 15:54:28 > top of Java-index,Core,Core APIs...
# 3

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

seligmana at 2007-7-15 15:54:28 > top of Java-index,Core,Core APIs...
# 4
> "An annotation on a local variable declaration is> never> retained in the binary representation."You are right. I didn't find this sentence.
fischerta at 2007-7-15 15:54:28 > top of Java-index,Core,Core APIs...
# 5

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

mgrickena at 2007-7-15 15:54:28 > top of Java-index,Core,Core APIs...
# 6

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

mgrickena at 2007-7-15 15:54:28 > top of Java-index,Core,Core APIs...