Skip to content

Instantly share code, notes, and snippets.

@khotyn
Created December 2, 2011 03:19
Show Gist options
  • Save khotyn/1421578 to your computer and use it in GitHub Desktop.
Save khotyn/1421578 to your computer and use it in GitHub Desktop.
RuntimeVisibleAnnotations and RuntimeInvisibleAnnotations
package com.khotyn.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface Anno {
}
package com.khotyn.test;
@Anno
public class AnnoTest {
@Deprecated
public void test() {
}
}
Compiled from "AnnoTest.java"
public class com.khotyn.test.AnnoTest extends java.lang.Object
SourceFile: "AnnoTest.java"
RuntimeInvisibleAnnotations: length = 0x6
00 01 00 12 00 00
minor version: 0
major version: 50
Constant pool:
const #1 = Method #3.#19; // java/lang/Object."<init>":()V
const #2 = class #20; // com/khotyn/test/AnnoTest
const #3 = class #21; // java/lang/Object
const #4 = Asciz <init>;
const #5 = Asciz ()V;
const #6 = Asciz Code;
const #7 = Asciz LineNumberTable;
const #8 = Asciz LocalVariableTable;
const #9 = Asciz this;
const #10 = Asciz Lcom/khotyn/test/AnnoTest;;
const #11 = Asciz test;
const #12 = Asciz Deprecated;
const #13 = Asciz RuntimeVisibleAnnotations;
const #14 = Asciz Ljava/lang/Deprecated;;
const #15 = Asciz SourceFile;
const #16 = Asciz AnnoTest.java;
const #17 = Asciz RuntimeInvisibleAnnotations;
const #18 = Asciz Lcom/khotyn/test/Anno;;
const #19 = NameAndType #4:#5;// "<init>":()V
const #20 = Asciz com/khotyn/test/AnnoTest;
const #21 = Asciz java/lang/Object;
{
public com.khotyn.test.AnnoTest();
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 4: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/khotyn/test/AnnoTest;
public void test();
Code:
Stack=0, Locals=1, Args_size=1
0: return
LineNumberTable:
line 8: 0
LocalVariableTable:
Start Length Slot Name Signature
0 1 0 this Lcom/khotyn/test/AnnoTest;
Deprecated: true
RuntimeVisibleAnnotations: length = 0x6
00 01 00 0E 00 00
}
@khotyn
Copy link
Author

khotyn commented Dec 2, 2011

In a Java Annotation, you can mark the Annotation's RetentionPolicy as @Retention(RetentionPolicy.CLASS), @Retention(RetentionPolicy.RUNTIME or @Retention(RetentionPolicy.SOURCE):

  • @Retention(RetentionPolicy.CLASS) means that the annotation will be recorded in the class file but not retained by the VM in runtime, that is RuntimeInvisibleAnnotations and RuntimeInvisibleParameterAnnotations in the class file.
  • @Retention(RetentionPolicy.RUNTIME means that the annotation will be recorded in the class file and will be retained by the VM in the runtime, that is RuntimeVisibleAnnotaions and RuntimeVisibleParameterAnnotations int the class file.
  • @Retention(RetentionPolicy.SOURCE) means that the annotation will not be recorded in the class file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment