Skip to content

Instantly share code, notes, and snippets.

@mythosil
Created October 26, 2012 16:04
Show Gist options
  • Save mythosil/3959644 to your computer and use it in GitHub Desktop.
Save mythosil/3959644 to your computer and use it in GitHub Desktop.
original annotation sample
/**
*
* $ javac AnnotationSample.java -Xlint:unchecked
* $ java AnnotationSample
*
*/
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@interface MyTypeAnnotation {
String value() default "MyTypeAnnotation";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@interface MyMethodAnnotation {
String value() default "MyMethodAnnotation";
}
@MyTypeAnnotation("class annotation")
public class AnnotationSample {
@MyMethodAnnotation("method annotation")
public static void main(String[] args) throws Throwable {
Class<?> c = AnnotationSample.class;
Method m = c.getMethod("main", new Class[]{String[].class});
MyTypeAnnotation typeAnnotation = c.getAnnotation(MyTypeAnnotation.class);
System.out.println(typeAnnotation.value());
MyMethodAnnotation methodAnnotation = m.getAnnotation(MyMethodAnnotation.class);
System.out.println(methodAnnotation.value());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment