Skip to content

Instantly share code, notes, and snippets.

@jnizet
Created December 2, 2012 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnizet/4190246 to your computer and use it in GitHub Desktop.
Save jnizet/4190246 to your computer and use it in GitHub Desktop.
Annotation test
// 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