Created
December 2, 2012 18:14
-
-
Save jnizet/4190246 to your computer and use it in GitHub Desktop.
Annotation test
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
// Annotation1.java | |
package bar.baz; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
public @interface Annotation1 { | |
} | |
// Annotation2.java | |
package bar.baz; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
public @interface Annotation2 { | |
} | |
// AnnotationTest.java | |
package bar.baz; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Method; | |
public class AnnotationTest { | |
@Annotation1 | |
public static @Annotation2 String hello() { | |
return "hello"; | |
} | |
public static void main(String[] args) throws Exception { | |
Method m = AnnotationTest.class.getMethod("hello"); | |
System.out.println("Annotations : "); | |
for (Annotation a : m.getAnnotations()) { | |
System.out.println(a); | |
} | |
System.out.println("Annotation 1 : " + m.getAnnotation(Annotation1.class)); | |
System.out.println("Annotation 2 : " + m.getAnnotation(Annotation2.class)); | |
} | |
} | |
// Output: | |
Annotations : | |
@bar.baz.Annotation1() | |
@bar.baz.Annotation2() | |
Annotation 1 : @bar.baz.Annotation1() | |
Annotation 2 : @bar.baz.Annotation2() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment