fork(1) download
  1. import java.lang.annotation.*;
  2. import java.lang.reflect.*;
  3.  
  4. //criando a anotação
  5. @Retention(RetentionPolicy.RUNTIME) //são anotações usadas para criar anotações
  6. @Target(ElementType.METHOD) //só permite usar em métodos
  7. @interface MyAnnotation { //note o @
  8. int value();
  9. }
  10.  
  11. //usando-a
  12. class Hello {
  13. @MyAnnotation(value = 10)
  14. public void sayHello() {
  15. System.out.println("hello annotation");
  16. }
  17. }
  18.  
  19. //pegando dados dela
  20. class TestCustomAnnotation1 {
  21. public static void main(String args[]) throws Exception {
  22. Hello h = new Hello();
  23. Method m = h.getClass().getMethod("sayHello");
  24. MyAnnotation manno = m.getAnnotation(MyAnnotation.class);
  25. System.out.println("value is: " + manno.value());
  26. }
  27. }
  28.  
  29. //https://pt.stackoverflow.com/q/571176/101
Success #stdin #stdout 0.13s 49296KB
stdin
Standard input is empty
stdout
value is: 10