Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 12 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 25 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 25 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 26 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 26 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 27 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 27 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 28 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 28 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 29 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 25 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 25 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 26 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 26 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 27 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 27 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 28 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 28 Deprecated: Function eregi() is deprecated in /home/codingr/sites/codingrus.ru/maincore.php on line 29 .:: CodingRUS ::. программирование по-русски на Delphi, C++, PHP, Prolog, GPSS Метаданные. Аннотации-маркеры
Прислано Kest на January 13 2009 10:36:34
Аннотация-маркер (marker annotation) — это специальный тип аннотации, не содержащий методов-членов. Единственная цель такой аннотации — пометить объявление. В этом случае достаточно присутствия аннотации. Лучше всего для проверки наличия аннотации-маркера использовать метод isAnnotationPresent(), который определен в интерфейсе AnnotatedElement и, следовательно, доступен для объектов типа Class, Field, Method, Constructor и Package.
В листинге 7.5 приведен пример применения аннотации-маркера. Для определения Присутствия маркера используется метод isAnnotationPresent ().
Поскольку у интерфейса аннотации-маркера нет методов-членов, достаточно определить его наличие.

Листинг 7.5. Применение аннотации-маркера
import java.lang.annotation.*;
import java.lang.reflect.*;

// A marker annotation.
@Retention(RetentionPolicy.RUNTIME)
@interface MyMarker { }

class Marker {

// Annotate a method using a marker.
// Notice that no ( ) is needed.
@MyMarker
public static void myMeth() {
Marker ob = new Marker();

try {
Method m = ob.getClass().getMethod("myMeth");

// Determine if the annotation is present.
if(m.isAnnotationPresent(MyMarker.class))
System.out.println("MyMarker is present.");

} catch (NoSuchMethodException exc) {
System.out.println("Method Not Found.");
}
}
public static void main(String args[]) {
myMeth();
}
}




В приведенном далее выводе результатов работы программы из листинга 7.5 подтверждается наличие аннотации типа @MуMаrkеr:
MуMаrkеr is present.
Обратите внимание на то, что в программе не нужно вставлять круглые скобки после имени интерфейса @ MyMarker при создании аннотации. Таким образом, когда вставляется аннотация MyMarker, просто указывается ее имя, как показано в следующей строке:
@MyMarker



Не будет ошибкой применение пустых скобок, но они не нужны.