﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-血色浪漫-文章分类-Seam 3</title><link>http://www.blogjava.net/FORRogue/category/49706.html</link><description>我会微笑着面对一切，但是与快乐无关！</description><language>zh-cn</language><lastBuildDate>Thu, 16 Feb 2012 11:12:13 GMT</lastBuildDate><pubDate>Thu, 16 Feb 2012 11:12:13 GMT</pubDate><ttl>60</ttl><item><title>Seam3 - 注解和注解工具类</title><link>http://www.blogjava.net/FORRogue/articles/359501.html</link><dc:creator>Rogue</dc:creator><author>Rogue</author><pubDate>Mon, 26 Sep 2011 05:10:00 GMT</pubDate><guid>http://www.blogjava.net/FORRogue/articles/359501.html</guid><wfw:comment>http://www.blogjava.net/FORRogue/comments/359501.html</wfw:comment><comments>http://www.blogjava.net/FORRogue/articles/359501.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/FORRogue/comments/commentRss/359501.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/FORRogue/services/trackbacks/359501.html</trackback:ping><description><![CDATA[<p>&nbsp;</p> <p><font color="#9c85c0" size="2" face="微软雅黑"></font>&nbsp; </p><h6></h6> <p><font color="#666666" size="2" face="微软雅黑">本人也是刚学习Seam 3，由于资料太少，所以就翻译起官方出的参考。由于英文功底太差，每次看得都很吃力，就翻译出来大家一起学习。还请指正错误。</font> </p><p><font color="#9c85c0" size="1" face="微软雅黑">1. Annotated Type Builder&nbsp; 注释类型生成器</font> </p><p><font color="#9c85c0" size="1" face="微软雅黑">2. Annotation Instance Provider&nbsp; 注释实例提供器</font> </p><p><font color="#9c85c0" size="1" face="微软雅黑">3. Annotation Inspector&nbsp; 注解检查器</font> </p><p><font color="#9c85c0" size="1" face="微软雅黑">4. Synthetic Qualifiers&nbsp; 组合修饰符</font> </p><p><font color="#9c85c0" size="1" face="微软雅黑">5. Reflection Utilities&nbsp; 反射公共类</font> </p><p><font color="#4f81bd">Seam Solder provides a number of utilility classes that make working with annotations and AnnotatedTypes easier. This chapter walks you through each utility, and gives you some ideas about how to use it. For more detail, take a look at the JavaDoc on each class.</font> </p><p><font color="#c0504d">Seam Solder提供一些公共类让注解和注解类型更加容易工作。本章将通过每个工具类引导您，并为您提供有关如何使用它的一些想法。对于更多的细节，看看每个类的JavaDoc。</font> </p><p><font color="#c0504d"></font>&nbsp; </p><h2>1.注释类型生成器</h2> <p><font color="#4f81bd">Seam Solder provides an AnnotatedType implementation that should be suitable for the needs of most portable extensions. The AnnotatedType is created from AnnotatedTypeBuilder, typically in an extension's observer method, as follows: </font> </p><p><font color="#c0504d">Seam Solder 提供一个AnnotatedType接口来实现大多数的需求扩展。AnnotatedType 是通过 AnnotatedTypeBuilder的一个方法返回得到，如下：</font> </p><div><pre>AnnotatedTypeBuilder builder = <span style="color: #0000ff">new</span> AnnotatedTypeBuilder().readFromType(type, <span style="color: #0000ff">true</span>)
<span style="color: #008000">/* readFromType can read from an AnnotatedType or a class */</span>
.addToClass(ModelLiteral.INSTANCE); <span style="color: #008000">/* add the @Model annotation */</span>
</pre></div><br />
<p><font color="#4f81bd">Here we create a new builder, and initialize it using an existing AnnotatedType. We can then add or remove annotations from the class, and its members. When we have finished modifying the type, we call create() to spit out a new, immutable, AnnotatedType. </font>
</p><p><font color="#c0504d">这里我们有一个新的对象实例，并初始化了它现有的 AnnotatedType。然后我们可以为这个类以及它的成员添加或者移除注解。当 我们完成修改后，我们可以调用create() 来获得一个新的AnnotatedType对象。</font>
</p><div><pre>AnnotatedType redefinedType = builder.create();</pre></div><br />
<p><font color="#4f81bd">One place this is immensely useful is for replacing the AnnotatedType in an extension that observes the ProcessAnnotatedType event:</font>
</p><p><font color="#c0504d">对ProcessAnnotatedType进行事件监听对修改AnnotatedType是一个非常有用的扩展：</font>
</p><div><pre><span style="color: #0000ff">public</span> &lt;X&gt; <span style="color: #0000ff">void</span> processAnnotatedType(@Observes ProcessAnnotatedType&lt;X&gt; evt) {
AnnotatedTypeBuilder builder = <span style="color: #0000ff">new</span> AnnotatedTypeBuilder()
.readFromType(evt.getAnnotatedType(), <span style="color: #0000ff">true</span>)
.addToClass(ModelLiteral.INSTANCE);
evt.setAnnotatedType(builder.create());
}
</pre></div><br />
<p><font color="#4f81bd">This type is now effectively annotated with @Model, even if the annotation is not present on the class definition in the Java source file. </font>
</p><p><font color="#c0504d">现在这里的注解类型是@Model，即使在Java源文件中对这个类不是这样定义的。</font>
</p><p><font color="#4f81bd">AnnotatedTypeBuilder also allows you to specify a "redefinition", which can be applied to the type, a type of member, or all members. The redefiner will receive a callback for any annotations present which match the annotation type for which the redefinition is applied. </font>
</p><p><font color="#c0504d">AnnotatedTypeBuilder 还允许你自己定义一个&#8220;重定义&#8221;，它可以用于类型，类型的成员或所有成员。重新定义的类型可以接受任何使用重新定义的注解的回调。</font>
</p><p><font color="#4f81bd">For example, to remove the qualifier @Unique from the type and any of its members, use this: </font>
</p><p><font color="#c0504d">例如，对这个类型去掉@Unique修饰符，那么它的所有成员都回改变：</font>
</p><div><pre>AnnotatedTypeBuilder builder = <span style="color: #0000ff">new</span> AnnotatedTypeBuilder().readFromType(type, <span style="color: #0000ff">true</span>).redefine(Unique.<span style="color: #0000ff">class</span>, <span style="color: #0000ff">new</span> AnnotationRedefiner&lt;Unique&gt;() {
<span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> redefine(RedefinitionContext&lt;A&gt; ctx) {
ctx.getAnnotationBuilder().remove(Unique.<span style="color: #0000ff">class</span>);
}
});
AnnotatedType redefinedType = builder.create();
</pre></div><br />
<p><font color="#4f81bd">No doubt, this is a key blade in Solder's army knife arsenal of tools. You can quite effectively change the picture of the type metadata CDI discovers when it scans and processes the classpath of a bean archive. </font>
</p><p><font color="#c0504d">毫无疑问，这是打开Solder工具军库的钥匙。当CDI扫描器查找并处理classpath下bean目录的时候，你可以非常有效地改变类型的元数据。</font>
</p><p>&nbsp; </p><h2>2. Annotation Instance Provider 注解实例提供器</h2>
<p><font color="#4f81bd">Sometimes you may need an annotation instance for an annotation whose type is not known at development time. Seam Solder provides a AnnotationInstanceProvider class that can create an AnnotationLiteral instance for any annotation at runtime. Annotation attributes are passed in via aMap&lt;String,Object&gt;. For example given the follow annotation:</font> 
</p><p><font color="#c0504d">在开发时，有时你可能需要获得一个不知道其类型注解的注解实例。Seam Solder提供了一个AnnotationInstanceProvider类，它可以在运行时创建一个任何注释AnnotationLiteral的实例。注释属性是通过传递Map&lt;String,Object&gt; 。例如，给定以下注解：</font>
</p><div><pre>@Retention(RetentionPolicy.RUNTIME)
<span style="color: #0000ff">public</span> @<span style="color: #0000ff">interface</span> MultipleMembers {
<span style="color: #0000ff">int</span> intMember();
<span style="color: #0000ff">long</span> longMember();
<span style="color: #0000ff">short</span> shortMember();
<span style="color: #0000ff">float</span> floatMember();
<span style="color: #0000ff">double</span> doubleMember();
<span style="color: #0000ff">byte</span> byteMember();
<span style="color: #0000ff">char</span> charMember();
<span style="color: #0000ff">boolean</span> booleanMember();
<span style="color: #0000ff">int</span>[] intArrayMember();
}
</pre></div><br />
<p><font color="#4f81bd">We can create an annotation instance as follows: </font>
</p><p><font color="#c0504d">我们可以创建以下注解实例：</font>
</p><div><pre><span style="color: #008000">/* Create a new provider */</span>
AnnotationInstanceProvider provider = <span style="color: #0000ff">new</span> AnnotationInstanceProvider();
<span style="color: #008000">/* Set the value for each of attributes */</span>
Map&lt;<span style="color: #0000ff">String</span>, Object&gt; values = <span style="color: #0000ff">new</span> HashMap&lt;<span style="color: #0000ff">String</span>, Object&gt;();
values.put("<span style="color: #8b0000">intMember</span>", 1);
values.put("<span style="color: #8b0000">longMember</span>", 1);
values.put("<span style="color: #8b0000">shortMember</span>", 1);
values.put("<span style="color: #8b0000">floatMember</span>", 0);
values.put("<span style="color: #8b0000">doubleMember</span>", 0);
values.put("<span style="color: #8b0000">byteMember</span>", ((<span style="color: #0000ff">byte</span>) 1));
values.put("<span style="color: #8b0000">charMember</span>", 'c');
values.put("<span style="color: #8b0000">booleanMember</span>", <span style="color: #0000ff">true</span>);
values.put("<span style="color: #8b0000">intArrayMember</span>", <span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span>[] { 0, 1 });
<span style="color: #008000">/* Generate the instance */</span>
MultipleMembers an = provider.get(MultipleMembers.<span style="color: #0000ff">class</span>, values);
</pre></div>
<h2>3. Annotation Inspector（注解检查器）</h2>
<p><font color="#4f81bd">The Annotation Inspector allows you to easily discover annotations which are meta-annotated. For example: </font>
</p><p><font color="#c0504d">注解检查器可以轻松地发现那些元注解。例如：</font>
</p><div><pre><span style="color: #008000">/* Discover all annotations on type which are meta-annotated @Constraint */</span>
Set&lt;Annotation&gt; constraints = AnnotationInspector.getAnnotations(type,Constraint.<span style="color: #0000ff">class</span>,beanManager);
<span style="color: #008000">/* Load the annotation instance for @FacesValidator the annotation may declared on the type, */</span>
<span style="color: #008000">/* or, if the type has any stereotypes, on the stereotypes */</span>
FacesValidator validator = AnnotationInspector.getAnnotation(
type, FacesValidator.<span style="color: #0000ff">class</span>, <span style="color: #0000ff">true</span>, beanManager);
</pre></div><br />
<p><font color="#4f81bd">The utility methods work correctly on Stereotypes as well. Let's say you're working with a bean that was decorated @Model, running the following example will still show you the underlying @Named.</font>
</p><p><font color="#c0504d">这些实用方法也能用于Stereotypes（复合注解）。假如我们有一个bean被赋予了@Model注解，运行下面的例子你就可以或者隐藏的注解@Named</font>
</p><div><pre><span style="color: #008000">// assuming you have a class..</span>
@Model
<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> User {
&#8230;
}
<span style="color: #008000">// Assume type represents the User class</span>
assert AnnotationInspector.isAnnotationPresent(type, Named.<span style="color: #0000ff">class</span>, beanManager);
<span style="color: #008000">// Retrieves the underlying @Named instance on the stereotype</span>
Named <span style="color: #0000ff">name</span> = AnnotationInspector.getAnnotation(type, Named.<span style="color: #0000ff">class</span>, <span style="color: #0000ff">true</span>, beanManager);
</pre></div><br />
<p><font color="#4f81bd">The search algorithm will first check to see if the annotation is present directly on the annotated element first, then searches within the stereotype annotations on the element. If you only want to search for Annotations on Stereotypes, then you can use either of the methods AnnotationInspector.getAnnotationFromStereotype. </font>
</p><p><font color="#c0504d">搜索方法会首先查找这个直接添加的注解元素，然后会对复合类型进行查找。如果你要直接对那些复合注解进行查找，你可以调用另外一个方法，AnnotationInspector.getAnnotationFromStereotype. </font>
</p><p><font color="#4f81bd">There is an overloaded form of isAnnotationPresent and getAnnotation to control whether it will search on Stereotypes or not. For both of these methods, a search is performed first directly on the element before searching in stereotypes. </font>
</p><p><font color="#c0504d">这是对 isAnnotationPresent 和 getAnnotation 两个方法的重新调用判断它是不是一个复合类型的注解。以上的无论哪个方法都先对直接类型注解进行查找。</font>
</p><p><font color="#c0504d"></font>&nbsp; </p><h2>4. Synthetic Qualifiers（组合修饰符）</h2>
<p><font color="#4f81bd">When developing an extension to CDI, it can be useful to detect certain injection points, or bean definitions and based on annotations or other metadata, add qualifiers to further disambiguate the injection point or bean definition for the CDI bean resolver. Solder's synthetic qualifers can be used to easily generate and track such qualifers. </font>
</p><p><font color="#c0504d">当对CDI进行扩展时，它可以为CDI bean 分析器有效地发现已有的注入点或bean的定义还有基本的注解或其他元数据，添加修饰符来进一步消除这些注入点或bean的定义。（翻译的很不通）Solder的组合修饰符可以用来方便地生产跟踪这些修饰符。</font>
</p><p><font color="#4f81bd">In this example, we will create a synthetic qualifier provider, and use it to create a qualifier. The provider will track the qualifier, and if a qualifier is requested again for the same original annotation, the same instance will be returned. </font>
</p><p><font color="#c0504d">在这个例子中，我们将创建一个组合修饰符提供器，并用它来创建一个修饰符。这个提供器会跟踪这个qualifier，如果这个qualifier被再次调用，它会返回跟上次相同的实例。</font>
</p><div><pre><span style="color: #008000">/* Create a provider, giving it a unique namespace */</span>
Synthetic.Provider provider = <span style="color: #0000ff">new</span> Synthetic.Provider("<span style="color: #8b0000">com.acme</span>");
<span style="color: #008000">/* Get the a synthetic qualifier for the original annotation instance */</span>
Synthetic synthetic = provider.get(originalAnnotation);
<span style="color: #008000">/* Later calls with the same original annotation instance will return the same instance */</span>
<span style="color: #008000">/* Alternatively, we can "get and forget" */</span>
Synthetic synthetic2 = provider.get();
</pre></div><br />
<h2>5. Reflection Utilities</h2>
<p><font color="#4f81bd">Seam Solder comes with a number miscellaneous reflection utilities; these extend JDK reflection, and some also work on CDI's Annotated metadata. See the javadoc on Reflections for more. </font>
</p><p><font color="#c0504d">Seam Solder 提供了一些多方面的反射工具类；这些是对JDK反射机制的扩展，也用于对CDI元数据的处理。更多得请看有关放射机制的java文档。</font>
</p><p><font color="#4f81bd">Solder also includes a simple utility, PrimitiveTypes for converting between primitive and their respective wrapper types, which may be useful when performing data type conversion. Sadly, this is functionality which is missing from the JDK. </font>
</p><p><font color="#c0504d">Solder 也有一个简单的工具类，在对基本数据类型转化的时候，PrimitiveTypes可以很好地完成对数据在其原始类型跟包装类型之间转化。可悲的是，这是JDK中缺少的功能。</font>
</p><p><font color="#4f81bd">InjectableMethod allows an AnnotatedMethod to be injected with parameter values obtained by following the CDI type safe resolution rules, as well as allowing the default parameter values to be overridden.</font>
</p><p><font color="#c0504d">InjectableMethod允许AnnotatedMethod被作为一个符合CDI类型安全解决规则的参数值注入，也允许默认的参数值被覆盖重写。</font> </p><img src ="http://www.blogjava.net/FORRogue/aggbug/359501.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/FORRogue/" target="_blank">Rogue</a> 2011-09-26 13:10 <a href="http://www.blogjava.net/FORRogue/articles/359501.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>