Popgalop
Popgalop

Reputation: 757

Where are annotations stored in class file

I am trying to instrument a class file, but I was just wondering where annotations are stored in the class file format. I tried putting them in the interface table however, when I did that it only recognized them as an interface not as an annotation.

Upvotes: 4

Views: 2657

Answers (2)

Mike Strobel
Mike Strobel

Reputation: 25623

They are stored in the RuntimeVisibleAnnotations and RuntimeInvisibleAnnotations attributes for classes and methods class members, and the RuntimeVisibleParameterAnnotations and RuntimeInvisibleParameterAnnotations attributes for method parameters. You can find more detailed information about the classfile format in the Java VM Specification.

Upvotes: 5

Guillaume Darmont
Guillaume Darmont

Reputation: 5018

When trying to understand Java bytecode, a good starting point is writing in plain Java the class you would like to create using bytecode. Compile it, then use tools to retrieve generated bytecode.

Here's some tools I can think of :

  • ASM's Textifier (this class has a main method to make it easy to run)
  • Intellij IDEA Show bytecode view
  • Bytecode outline plugin for Eclipse

Upvotes: 0

Related Questions