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?

8
  • curious: what do you want to do with local variables annotated with @Junk? Jul 19, 2010 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, 2010 at 23:24
  • yes, but if you do that, you are manipulating code inside the method, right? that seems pretty difficult in annotation processor. Jul 20, 2010 at 0:16
  • I want to pull out some information at compile time, to create a static object with information pertinent at runtime.
    – apg
    Jul 20, 2010 at 1:35
  • 1
    @irreputable - you can't manipulate the existing code, but you can generate new code based on the annotation.
    – kschneid
    Jul 20, 2010 at 3:49

3 Answers 3

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

1
  • 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, 2010 at 21:42
5

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

1
  • 1
    Correct. As of Java 8, local variable annotations are stored in the classfile.
    – mernst
    Sep 30, 2014 at 7:24
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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.