Annotations.xhtml 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Annotations of declarations</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Enumerations.xhtml" title="Enumeration types"/>
  10. <link rel="next" href="Module-classes.xhtml" title="Modules and how they are compiled to classes"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Annotations of declarations" epub:type="subchapter" id="Annotations">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Annotations of declarations</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>The Java platform lets you associate with each declaration zero or more
  23. <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/guide/language/annotations.html" target="_top">annotations</a>.
  24. They provide an extensible mechanism to associate properties
  25. with declarations.
  26. Kawa support for annotations is not complete (the most important
  27. functionality missing is being able to declare annotation types),
  28. but is fairly functional.
  29. Here is a simple example illustrating use of
  30. <a class="ulink" href="http://jcp.org/en/jsr/detail?id=222" target="_top">JAXB annotations</a>:
  31. an <code class="literal">XmlRootElement</code> annotation on a class,
  32. and an <code class="literal">XmlElement</code> annotation on a field:
  33. </p>
  34. <pre class="screen">(import (class javax.xml.bind.annotation XmlRootElement XmlElement))
  35. (define-simple-class Bib ( ) (@XmlRootElement name: "bib")
  36. (books (@XmlElement name: "book" type: Book) ::java.util.ArrayList))
  37. (define-simple-class Book () ...)
  38. </pre>
  39. <p><a class="ulink" href="http://per.bothner.com/blog/2011/Using-JAXB-annotations" target="_top">This tutorial</a>
  40. explains the JAXB example in depth.
  41. </p>
  42. <p>Here is the syntax:
  43. </p>
  44. <div class="literallayout">
  45. <p><a id="idm139667871641840" class="indexterm"/><span id="meta-annotation"/><em class="replaceable"><code>annotation</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>(@</strong></span></code><a class="link" href="Annotations.xhtml#meta-annotation-typename"><em class="replaceable"><code>annotation-typename</code></em></a> <a class="link" href="Annotations.xhtml#meta-annotations-element-values"><em class="replaceable"><code>annotations-element-values</code></em></a><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  46. <a id="idm139667871636816" class="indexterm"/><span id="meta-annotations-element-values"/><em class="replaceable"><code>annotations-element-values</code></em> <code class="literal">::=</code> <a class="link" href="Annotations.xhtml#meta-annotation-element-value"><em class="replaceable"><code>annotation-element-value</code></em></a><br/>
  47.   | <a class="link" href="Annotations.xhtml#meta-annotation-element-pair"><em class="replaceable"><code>annotation-element-pair</code></em></a> ...<br/>
  48. <a id="idm139667871633040" class="indexterm"/><span id="meta-annotation-element-pair"/><em class="replaceable"><code>annotation-element-pair</code></em> <code class="literal">::=</code> <a class="link" href="Keywords.xhtml#meta-keyword"><em class="replaceable"><code>keyword</code></em></a> <a class="link" href="Annotations.xhtml#meta-annotation-element-value"><em class="replaceable"><code>annotation-element-value</code></em></a><br/>
  49. <a id="idm139667871629376" class="indexterm"/><span id="meta-annotation-element-value"/><em class="replaceable"><code>annotation-element-value</code></em> <code class="literal">::=</code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
  50. <a id="idm139667871626528" class="indexterm"/><span id="meta-annotation-typename"/><em class="replaceable"><code>annotation-typename</code></em> <code class="literal">::=</code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
  51. </p>
  52. </div>
  53. <p>An <em class="replaceable"><code>annotations-element-values</code></em> consisting of just
  54. a single <em class="replaceable"><code>annotation-element-value</code></em> is equivalent to an
  55. <em class="replaceable"><code>annotation-element-pair</code></em> with a <code class="literal">value:</code> keyword.
  56. </p>
  57. <p>Each <em class="replaceable"><code>keyword</code></em> must correspond to the name of
  58. an element (a zero-argument method) in the annotation type.
  59. The corresponding <em class="replaceable"><code>annotation-element-value</code></em> must be compatible with the
  60. element type (return type of the method) of the annotation type.
  61. </p>
  62. <p>Allowed element types are of the following kinds:
  63. </p>
  64. <div class="itemizedlist" epub:type="list">
  65. <ul class="itemizedlist" style="list-style-type: disc; ">
  66. <li class="listitem" epub:type="list-item">
  67. <p>Primitive types, where the <em class="replaceable"><code>annotation-element-value</code></em> must
  68. be number or boolean coercible to the element type.
  69. </p>
  70. </li>
  71. <li class="listitem" epub:type="list-item">
  72. <p>Strings, where the <em class="replaceable"><code>annotation-element-value</code></em> is normally a string literal.
  73. </p>
  74. </li>
  75. <li class="listitem" epub:type="list-item">
  76. <p>Classes, where the <em class="replaceable"><code>annotation-element-value</code></em> is normally
  77. a classname.
  78. </p>
  79. </li>
  80. <li class="listitem" epub:type="list-item">
  81. <p>Enumeration types. The value usually has the form <code class="literal"><em class="replaceable"><code>ClassName</code></em>:<em class="replaceable"><code>enumFieldname</code></em></code>.
  82. </p>
  83. </li>
  84. <li class="listitem" epub:type="list-item">
  85. <p>Nested annotation types, where the <em class="replaceable"><code>annotation-element-value</code></em> must
  86. be a compatible <em class="replaceable"><code>annotation</code></em> value.
  87. </p>
  88. </li>
  89. <li class="listitem" epub:type="list-item">
  90. <p>An array of one of the allowable types.
  91. An array constructor expression works, but using the
  92. square bracket syntax is recommended.
  93. </p>
  94. </li>
  95. </ul>
  96. </div>
  97. <p>Annotations are usually used in declarations,
  98. where they are required to be “constant-folded” to compile-time
  99. constant annotation values.
  100. This is so they can be written to class files.
  101. However, in other contexts an annotation can be used as an expression
  102. with general sub-expressions evaluated at run-time:
  103. </p>
  104. <pre class="screen">(define bk-name "book")
  105. (define be (@XmlElement name: bk-name type: Book))
  106. (be:name) ⇒ "book"
  107. </pre>
  108. <p>(This may have limited usefulness: There are some bugs, including
  109. lack of support for default values for annotation elements.
  110. These bugs can be fixed if someone reports a need for
  111. runtime construction of annotation values.)
  112. </p>
  113. </section>
  114. <footer>
  115. <div class="navfooter">
  116. <p>
  117. Up: <a accesskey="u" href="Objects-Classes-and-Modules.xhtml">Object, Classes and Modules</a></p>
  118. <p>
  119. Previous: <a accesskey="p" href="Enumerations.xhtml">Enumeration types</a></p>
  120. <p>
  121. Next: <a accesskey="n" href="Module-classes.xhtml">Modules and how they are compiled to classes</a></p>
  122. </div>
  123. </footer>
  124. </body>
  125. </html>