开发者

Any example usage of @Target({}) for java.lang.annotation.Target?

http://download.orac开发者_JS百科le.com/javase/6/docs/api/java/lang/annotation/Target.html

This meta-annotation indicates that the declared type is intended solely for use as a member type in complex annotation type declarations. It cannot be used to annotate anything directly:

@Target({}) 
public @interface MemberType {
    ...
}

What does this mean? Any example code using this?

NB: I'm referring the the empty args use of @Target, as documented in the JavaDoc, and not to the well-documented use of Target with enum constants.


An empty target is used when the annotation can only be used within other annotations (with non-empty target sets), and can't be attached to anything directly. An example of use of this is in JAXB, where the @XmlNs annotation has an empty target list; the code below is extracted from my own code (with some very minor changes) and is the complete package-info.java file for this particular package:

@XmlSchema(namespace = Namespaces.MAIN,
    xmlns = { @XmlNs(prefix = "xlink", namespaceURI = Namespaces.XLINK) },
    elementFormDefault = QUALIFIED, attributeFormDefault = QUALIFIED)
package example.bindings;

import static javax.xml.bind.annotation.XmlNsForm.QUALIFIED;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;
import example.common.Namespaces;

The @XmlNs annotation is used to instruct JAXB what prefix to use for the XLink namespace, but that information can only ever be placed at the schema level (a restriction from general XML, but not a problem for the most part) and because there may be many such mappings, it can't be attached to the package directly but instead has to go inside an array-valued property of the main @XmlSchema annotation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜