10

I'm trying to create an annotation for a local variable. I know that I can't retain the annotation in the generated bytecode, but I should be able to have access to the information at compile time by doing something like this:

@Target({ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface Junk {
  String value();
}

only, this doesn't get processed by apt, or javac when I specify a ProcessorFactory that has "Junk" in it's supported types in the following:

class JunkTester {
    public static void main(String[] args) {
        @Junk String tmp = "Hello World";
        System.out.println(tmp);
    }
}

It will however work when I move the @Junk annotation before public static

Thoughts and or workarounds?

apg
  • 2,611
  • 1
  • 18
  • 19
  • curious: what do you want to do with local variables annotated with @Junk? – irreputable Jul 19 '10 at 22:42
  • 1
    @Junk is obviously just an example, but eventually I'd like to do code generation from the real annotation. – apg Jul 19 '10 at 23:24
  • yes, but if you do that, you are manipulating code inside the method, right? that seems pretty difficult in annotation processor. – irreputable Jul 20 '10 at 00:16
  • I want to pull out some information at compile time, to create a static object with information pertinent at runtime. – apg Jul 20 '10 at 01:35
  • 1
    @irreputable - you can't manipulate the existing code, but you can generate new code based on the annotation. – kschneid Jul 20 '10 at 03:49
  • @Andrew - since you mention javac, are you using JDK 1.6? – kschneid Jul 20 '10 at 03:50
  • new code in a new method, based on a local variable in another method? if this is top secret project, you don't have to explain. but it is very curious. – irreputable Jul 20 '10 at 10:23
  • @irreputable The annotation processor should pull out all the @Junk annotations on local variables in the source tree, and create a new java file that lists them all, and their properties as a static array. – apg Jul 20 '10 at 10:29

3 Answers3

8

Did some quick tests and searched a little, and it's looking like hooking into LOCAL_VARIABLE isn't really supported...yet:

http://forums.sun.com/thread.jspa?threadID=775449
http://www.cs.rice.edu/~mgricken/research/laptjavac/
https://checkerframework.org/jsr308/

Could be totally wrong, but that's how it's looking...

mernst
  • 7,437
  • 30
  • 45
kschneid
  • 5,626
  • 23
  • 31
  • That has been my findings as well. I'm not sure if there are any workarounds other than a new compiler such as laptjavac (well, patched). I've also seen that `@SuppressWarnings`, which according to Effective Java work as `LOCAL_VARIABLE` annotations, are sort of special cased in javac, so it's not looking good. – apg Jul 20 '10 at 21:42
5

It seems that the Type Annotations Specification (JSR 308), will hopefully address this subject in the future (JDK 8 ?).

mernst
  • 7,437
  • 30
  • 45
Xavi López
  • 27,550
  • 11
  • 97
  • 161
2

As of Java 8, local variable annotations are stored in the classfile.

A standard Java annotation processor does not process the bodies of methods. However, the Checker Framework enables you to write an annotation processor that processes every annotation including on local variables. Its annotation processors can even examine every statement of the program, whether annotated or not.

mernst
  • 7,437
  • 30
  • 45