<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FReflection%2FAnnotation</id>
		<title>Java Tutorial/Reflection/Annotation - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FReflection%2FAnnotation"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Reflection/Annotation&amp;action=history"/>
		<updated>2026-04-21T21:28:39Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Reflection/Annotation&amp;diff=2949&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Reflection/Annotation&amp;diff=2949&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:26Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 17:44, 31 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Reflection/Annotation&amp;diff=2950&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Reflection/Annotation&amp;diff=2950&amp;oldid=prev"/>
				<updated>2010-05-31T15:19:22Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Get annotation by Annotation class ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.lang.annotation.Retention;&lt;br /&gt;
import java.lang.annotation.RetentionPolicy;&lt;br /&gt;
import java.lang.reflect.Method;&lt;br /&gt;
@Retention(RetentionPolicy.RUNTIME)&lt;br /&gt;
@interface MySingle {&lt;br /&gt;
  int value(); // this variable name must be value&lt;br /&gt;
}&lt;br /&gt;
class Single {&lt;br /&gt;
  @MySingle(100)&lt;br /&gt;
  public static void myMeth() {&lt;br /&gt;
    Single ob = new Single();&lt;br /&gt;
    try {&lt;br /&gt;
      Method m = ob.getClass().getMethod(&amp;quot;myMeth&amp;quot;);&lt;br /&gt;
      MySingle anno = m.getAnnotation(MySingle.class);&lt;br /&gt;
      System.out.println(anno.value()); // displays 100&lt;br /&gt;
    } catch (NoSuchMethodException exc) {&lt;br /&gt;
      System.out.println(&amp;quot;Method Not Found.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    myMeth();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Get annotation by type ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.lang.annotation.Annotation;&lt;br /&gt;
import javax.ejb.Entity;&lt;br /&gt;
public class GetAnnotation {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        Annotation[] as = Person.class.getAnnotations();&lt;br /&gt;
        for(int i=0;i&amp;lt;as.length;i++){&lt;br /&gt;
            System.out.println(as[i].annotationType());        &lt;br /&gt;
        }&lt;br /&gt;
        Annotation a =Person.class.getAnnotation(Entity.class);&lt;br /&gt;
        System.out.println(a);  &lt;br /&gt;
        Annotation[] das = Person.class.getDeclaredAnnotations();&lt;br /&gt;
        for(int i=0;i&amp;lt;das.length;i++){&lt;br /&gt;
            System.out.println(das[i].annotationType());        &lt;br /&gt;
        }        &lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
///&lt;br /&gt;
&lt;br /&gt;
import javax.ejb.Entity;&lt;br /&gt;
import javax.ejb.AccessType;&lt;br /&gt;
import javax.ejb.Id;&lt;br /&gt;
import javax.ejb.GeneratorType;&lt;br /&gt;
import java.io.Serializable;&lt;br /&gt;
@Entity(access = AccessType.FIELD)&lt;br /&gt;
public class Person implements Serializable {&lt;br /&gt;
  @Id(generate = GeneratorType.AUTO)&lt;br /&gt;
  Integer id;&lt;br /&gt;
  String name;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Show all annotations for a class and a method. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.lang.annotation.Annotation;&lt;br /&gt;
import java.lang.annotation.Retention;&lt;br /&gt;
import java.lang.annotation.RetentionPolicy;&lt;br /&gt;
import java.lang.reflect.Method;&lt;br /&gt;
@Retention(RetentionPolicy.RUNTIME)&lt;br /&gt;
@interface MyAnno {&lt;br /&gt;
  String str();&lt;br /&gt;
  int val();&lt;br /&gt;
}&lt;br /&gt;
@Retention(RetentionPolicy.RUNTIME)&lt;br /&gt;
@interface What {&lt;br /&gt;
  String description();&lt;br /&gt;
}&lt;br /&gt;
@What(description = &amp;quot;An annotation test class&amp;quot;)&lt;br /&gt;
@MyAnno(str = &amp;quot;Meta2&amp;quot;, val = 99)&lt;br /&gt;
class Meta2 {&lt;br /&gt;
  @What(description = &amp;quot;An annotation test method&amp;quot;)&lt;br /&gt;
  @MyAnno(str = &amp;quot;Testing&amp;quot;, val = 100)&lt;br /&gt;
  public static void myMeth() {&lt;br /&gt;
    Meta2 ob = new Meta2();&lt;br /&gt;
    try {&lt;br /&gt;
      Annotation annos[] = ob.getClass().getAnnotations();&lt;br /&gt;
      System.out.println(&amp;quot;All annotations for Meta2:&amp;quot;);&lt;br /&gt;
      for (Annotation a : annos)&lt;br /&gt;
        System.out.println(a);&lt;br /&gt;
      Method m = ob.getClass().getMethod(&amp;quot;myMeth&amp;quot;);&lt;br /&gt;
      annos = m.getAnnotations();&lt;br /&gt;
      System.out.println(&amp;quot;All annotations for myMeth:&amp;quot;);&lt;br /&gt;
      for (Annotation a : annos)&lt;br /&gt;
        System.out.println(a);&lt;br /&gt;
    } catch (NoSuchMethodException exc) {&lt;br /&gt;
      System.out.println(&amp;quot;Method Not Found.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    myMeth();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Use reflection to display the annotation associated with a method. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.lang.annotation.Retention;&lt;br /&gt;
import java.lang.annotation.RetentionPolicy;&lt;br /&gt;
import java.lang.reflect.Method;&lt;br /&gt;
@Retention(RetentionPolicy.RUNTIME)&lt;br /&gt;
@interface MyAnno {&lt;br /&gt;
  String str();&lt;br /&gt;
  int val();&lt;br /&gt;
}&lt;br /&gt;
class Meta {&lt;br /&gt;
  @MyAnno(str = &amp;quot;Annotation Example&amp;quot;, val = 100)&lt;br /&gt;
  public static void myMeth() {&lt;br /&gt;
    Meta ob = new Meta();&lt;br /&gt;
    try {&lt;br /&gt;
      Class c = ob.getClass();&lt;br /&gt;
      Method m = c.getMethod(&amp;quot;myMeth&amp;quot;);&lt;br /&gt;
      MyAnno anno = m.getAnnotation(MyAnno.class);&lt;br /&gt;
      System.out.println(anno.str() + &amp;quot; &amp;quot; + anno.val());&lt;br /&gt;
    } catch (NoSuchMethodException exc) {&lt;br /&gt;
      System.out.println(&amp;quot;Method Not Found.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    myMeth();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>