Created
October 26, 2012 16:04
-
-
Save mythosil/3959644 to your computer and use it in GitHub Desktop.
original annotation sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* $ 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