<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaChen Blog &#187; Annotation</title>
	<atom:link href="http://www.javachen.com/tag/annotation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javachen.com</link>
	<description>Just some random thoughts about technology,Java and life</description>
	<lastBuildDate>Tue, 07 Sep 2010 15:26:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	
<!-- Start Of Script Generated By WP-PostViews Plus -->
<script type='text/javascript' src='http://www.javachen.com/wp-includes/js/jquery/jquery.js?ver=1.4.2'></script>
<script type="text/javascript">
/* <![CDATA[ */
jQuery.ajax({type:'GET',url:'http://www.javachen.com/wp-content/plugins/wp-postviews-plus/postviews_plus.php',data:'todowppvp=add&type=tag&id=126_1',cache:false,dataType:'script'});
/* ]]> */
</script>
<!-- End Of Script Generated By WP-PostViews Plus -->
	<item>
		<title>Java5中的注释Annotation</title>
		<link>http://www.javachen.com/2010/01/java5-annotation/</link>
		<comments>http://www.javachen.com/2010/01/java5-annotation/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 02:11:43 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Annotation]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=951</guid>
		<description><![CDATA[注释是java5中的新特性，谈到注释，先的谈谈Java元数据（metadata）。元数据，就是“关于数据的数据”。Java元数据有3种基本类型，还有3个Java内置注释类型，另外还有4中元注释类型。你可能用过Javadoc的注释自动生成文档。这就是元数据功能的一种。总的来说，元数据可以用来创建文档，跟踪代码的依赖性，执行编译时格式检查，代替已有的配置文件（如Hibernate也提供了注释配置）。 注释有3中基本类型 a.标记注释 &#8211;没有变量，只有名称标识。例如 @annotation b.单一值注释 &#8211;在标记注释的基础上提供一段数据。如 @annotation（“javachen”） c.完整注释 &#8211;可以包括多个数据成员，每个数据成员由名称和值构成。@annotation(site=&#8221;www.javachen.com&#8221;,author=&#8221;javachen&#8221;) Java中提供3个内置注释类型 a. Override ，只能用于方法（不能用于类，包声明或者其他构造） 作用：可以保证编译时候Override函数的声明正确性 用法： @Override public void fun(){..} b.Deprecated 同样只能作用与方法 作用：对不应再使用的方法进行注解 用法：@Deprecated public void fun{&#8230;} c.SupressWarnings 可以注释一段代码 作用：关闭特定的警告信息，例如你在使用泛型的时候未指定类型 用法： @SupressWarnings(value={&#8220;unchecked&#8221;}) Java中还提供了四种元注释，专门负责注释其他的注释 @Target 表示该注释可以用于什么地方。可用的ElementType参数包括： CONSTRUCTOR : 构造器的声明 FIELD : 域声明（包括enum实例） LOCAL_VARIABLE : 局部变量声明 METHOD : 方法声明 PACKAGE : 包声明 PARAMETER : 参数声明 TYPE : [...]]]></description>
			<content:encoded><![CDATA[<p>注释是java5中的新特性，谈到注释，先的谈谈Java元数据（metadata）。元数据，就是“关于数据的数据”。Java元数据有3种基本类型，还有3个Java内置注释类型，另外还有4中元注释类型。你可能用过Javadoc的注释自动生成文档。这就是元数据功能的一种。总的来说，元数据可以用来创建文档，跟踪代码的依赖性，执行编译时格式检查，代替已有的配置文件（如Hibernate也提供了注释配置）。<br />
<span id="more-951"></span><br />
<strong><span style="color: #0000ff;">注释有3中基本类型</span></strong><br />
a.标记注释      &#8211;没有变量，只有名称标识。例如 @annotation<br />
b.单一值注释    &#8211;在标记注释的基础上提供一段数据。如 @annotation（“javachen”）<br />
c.完整注释      &#8211;可以包括多个数据成员，每个数据成员由名称和值构成。@annotation(site=&#8221;www.javachen.com&#8221;,author=&#8221;javachen&#8221;)</p>
<p><span style="color: #0000ff;"><strong>Java中提供3个内置注释类型</strong></span><br />
a. Override ，只能用于方法（不能用于类，包声明或者其他构造）<br />
作用：可以保证编译时候Override函数的声明正确性<br />
用法：<br />
@Override<br />
public void fun(){..}</p>
<p>b.Deprecated  同样只能作用与方法<br />
作用：对不应再使用的方法进行注解<br />
用法：@Deprecated public void fun{&#8230;}</p>
<p>c.SupressWarnings 可以注释一段代码<br />
作用：关闭特定的警告信息，例如你在使用泛型的时候未指定类型<br />
用法： @SupressWarnings(value={&#8220;unchecked&#8221;})</p>
<p>Java中还提供了四种元注释，专门负责注释其他的注释</p>
<p>@Target   表示该注释可以用于什么地方。可用的ElementType参数包括：<br />
CONSTRUCTOR : 构造器的声明<br />
FIELD : 域声明（包括enum实例）<br />
LOCAL_VARIABLE : 局部变量声明<br />
METHOD : 方法声明<br />
PACKAGE : 包声明<br />
PARAMETER : 参数声明<br />
TYPE : 类、接口 （包括注解类型) 或enum声明</p>
<p>@Retention 表示需要在什么级别保存该注释信息。可选的RetentionPoicy参数包括：<br />
SOURCE : 注释将被编译器丢掉<br />
CLASS : 注释在class文件中可用，但会被VM丢弃<br />
RUNTIME : VM将在运行时也保留注释，因此可以通过反射机制读取注释的信息。</p>
<p>@Documented 将注释包含在JavaDoc中<br />
@Inheried  允许子类继承父类中的注释。</p>
<p><span style="color: #0000ff;"><strong>在Java中定义自己的注释</strong></span></p>
<p>Java语言支持一种新的类型——注释类型（annotation type），跟普通类差不多，在类中以符号（ @ ）的形式注释其他 Java 代码，用@interface 申明自定义注释类型。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.javachen.annotation</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Documented</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.ElementType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Inherited</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Retention</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.RetentionPolicy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Target</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * 用户自定义标签,带有成员变量的MyTag
 */</span>
@Documented <span style="color: #666666; font-style: italic;">//将注释包含在JavaDoc中</span>
@Inherited <span style="color: #666666; font-style: italic;">//允许子类继承父类中的注释。</span>
@Target<span style="color: #009900;">&#40;</span>value <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>ElementType.<span style="color: #006633;">METHOD</span>,ElementType.<span style="color: #006633;">CONSTRUCTOR</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #666666; font-style: italic;">//标注这个注释使用的范围</span>
@Retention<span style="color: #009900;">&#40;</span>value <span style="color: #339933;">=</span> RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span><span style="color: #666666; font-style: italic;">//要想使用反射得到注释信息，这个注释必须使用</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> MyTag <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">String</span> name<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;javachen&quot;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//给自定义注释类的成员加上默认值</span>
	<span style="color: #000066; font-weight: bold;">int</span> age<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">24</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span style="color: #ff0000;">注意：在自定义注释中只有一个成员时，方法名应该为value</span><br />
使用标签最终是为了帮助开发人员提取注释信息，然后根据不同需求做进一步处理，下面我们来看看如何获取注释信息。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.javachen.annotation</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Annotation</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TagTest <span style="color: #009900;">&#123;</span>
	@MyTag<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MyTag&quot;</span>, age <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override <span style="color: #666666; font-style: italic;">//可以保证编译时候Override函数的声明正确性</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Deprecated <span style="color: #666666; font-style: italic;">//对不应再使用的方法进行注解</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> notToUse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		TagTest tt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TagTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">Annotation</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> annotation <span style="color: #339933;">=</span> tt.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span>
					.<span style="color: #006633;">getAnnotations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Annotation</span> tag <span style="color: #339933;">:</span> annotation<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tag is:&quot;</span> <span style="color: #339933;">+</span> tag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tag.name()&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>MyTag<span style="color: #009900;">&#41;</span> tag<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tag.age()&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>MyTag<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>tag<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">age</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NoSuchMethodException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>需要注意的一点是，在执行这段代码之前我们还有一点小工作要做，还需要给我们的自定义标签MyTag加上一个说明标签，@ Retention, 表明注释信息将可以在运行时刻通过反射机制得到。如果不加入这个标签，上面的代码将没有任何输出。</p>
<p><strong><span style="color: #0000ff;">如何使用反射读取注释</span></strong></p>
<p>在以前的JDK版本中，我们可以使用反射得到类的方法、方法的参数以及其它的类成员等信息。那么在J2SE5.0中同样也可以象方法一样得到注释的各种信息。<br />
在使用反射之前必须使用import java.lang.reflect.* 来导入和反射相关的类。</p>
<p>如果要得到某一个类或接口的注释信息，可以使用如下代码：<br />
Annotation annotation = TestAnnotation.class.getAnnotation(MyAnnotation.class);</p>
<p>如果要得到全部的注释信息可使用如下语句：<br />
Annotation[] annotations = TestAnnotation.class.getAnnotations();<br />
或<br />
Annotation[] annotations = TestAnnotation.class.getDeclaredAnnotations();</p>
<p>getDeclaredAnnotations与getAnnotations类似，但它们不同的是getDeclaredAnnotations 得到的是当前成员所有的注释，不包括继承的。而getAnnotations得到的是包括继承的所有注释。</p>
<p>如果要得到其它成员的注释，可先得到这个成员，然后再得到相应的注释。如得到myMethod的注释。<br />
Method method = TestAnnotation.class.getMethod(&#8220;myMethod&#8221;, null);<br />
Annotation annotation = method.getAnnotation(MyAnnotation.class);</p>
<p>注：要想使用反射得到注释信息，这个注释必须使用<br />
@Retention(value = RetentionPolicy.RUNTIME)进行注释。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2010/01/java5-annotation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
