Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface MyMark {}
- class MarkedClazz implements MyMark {}
- class NonMarkedClazz {}
- class Test {
- public static void test(MyMark markedObject) {
- // do somthing.
- }
- public static void main(String[] args) {
- MarkedClazz marked = new MarkedClazz();
- NonMarkedClazz nonMarked = new NonMarkedClazz();
- test(marked);
- //test(nonMarked); // Ошибка компиляции.
- }
- }
- @Target(value = ElementType.TYPE)
- @Retention(value = RetentionPolicy.RUNTIME)
- public @interface MyAnnotation {}
- @MyAnnotation
- class Parent implements MyMark {}
- class Heir extends Parent {}
- class Test {
- public static void test(MyMark markedObject) {
- // do somthing.
- }
- public static void testAnno(Object object) {
- if(!object.getClass().isAnnotationPresent(MyAnnotation.class)) {
- throw new RuntimeException("object is not annotated by MyAnnotation")
- }
- // do something
- }
- public static void main(String[] args) {
- Parent parent = new Parent();
- Heir heir = new Heir();
- test(parent);
- test(heir); // ошибок нет
- testAnno(parent);
- testAnno(heir); // ошибка времени исполнения.
- }
- }
Add Comment
Please, Sign In to add comment