views:

70

answers:

1

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?

A: 

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/
http://types.cs.washington.edu/jsr308/

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

kschneid
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.
Andrew Gwozdziewycz