Guest User

Untitled

a guest
Nov 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. interface MyMark {}
  2.  
  3. class MarkedClazz implements MyMark {}
  4. class NonMarkedClazz {}
  5.  
  6. class Test {
  7. public static void test(MyMark markedObject) {
  8. // do somthing.
  9. }
  10.  
  11. public static void main(String[] args) {
  12. MarkedClazz marked = new MarkedClazz();
  13. NonMarkedClazz nonMarked = new NonMarkedClazz();
  14. test(marked);
  15. //test(nonMarked); // Ошибка компиляции.
  16. }
  17. }
  18.  
  19. @Target(value = ElementType.TYPE)
  20. @Retention(value = RetentionPolicy.RUNTIME)
  21. public @interface MyAnnotation {}
  22.  
  23. @MyAnnotation
  24. class Parent implements MyMark {}
  25. class Heir extends Parent {}
  26. class Test {
  27. public static void test(MyMark markedObject) {
  28. // do somthing.
  29. }
  30.  
  31. public static void testAnno(Object object) {
  32. if(!object.getClass().isAnnotationPresent(MyAnnotation.class)) {
  33. throw new RuntimeException("object is not annotated by MyAnnotation")
  34. }
  35. // do something
  36. }
  37.  
  38. public static void main(String[] args) {
  39. Parent parent = new Parent();
  40. Heir heir = new Heir();
  41. test(parent);
  42. test(heir); // ошибок нет
  43. testAnno(parent);
  44. testAnno(heir); // ошибка времени исполнения.
  45. }
  46. }
Add Comment
Please, Sign In to add comment