Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
backport175
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h3>Using the Annotations reader class</h3> <p>You can retrieve the backport175 annotations as well as regular Java 5 annotations (when running Java 5) at runtime using the API in the <code>Annotations</code> class.</p> <p>You retreive the annotations by specifying the:</p> <ul> <li>name of the annotations - this must be the fully qualified name of the annotation interface.</li> <li>class or member that is annotated - this is the <code>java.lang.Class</code> (for class level annotations) or one of the member class in the <code>java.lang.reflect.*</code> package for member level annotations (methods, fields and constructors)</li> </ul> <p>Here is a sample part of the API:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> // class annotations public static boolean isAnnotationPresent(Class annotation, Class klass) public static Annotation getAnnotation(Class annotation, Class klass) public static Annotation[] getAnnotations(Class klass) // method annotations public static boolean isAnnotationPresent(Class annotation, Method method) public static Annotation getAnnotation(Class annotation, Method method) public static Annotation[] getAnnotations(Method method) ... </pre></td></tr></table> <h3>The Annotation interface</h3> <p>All these methods in the <code>Annotations</code> reader class return an instance of the type <code>org.codehaus.backport175.reader.Annotation</code>. </p> <p>This class has some useful methods like:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> /** * Returns the annotation type, e.g. the annotation interface type. * * @return the type */ Class annotationType(); /** * Returns a string representation that is identical to the annotation * that was parsed by the compiler. * * @return the string representation */ String toString(); </pre></td></tr></table> <p>But most likely you want to cast the instance to the correct annotation type (interface). </p> <h3>Example</h3> <p>Here is an example on how to read in the EJB 3 transaction annotation:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> if (Annotations.isAnnotationPresent(javax.ejb.TransactionAttribute.class, method)) { Annotation annotation = Annotations.getAnnotation(javax.ejb.TransactionAttribute.class, method); javax.ejb.TransactionAttribute txAttribute = (javax.ejb.TransactionAttribute)annotation; } </pre></td></tr></table> <h3>Notes</h3> <p>If an annotation has an element with a default value, the reflective API will off course gives you access to it if the<br /> annotated class/member was not specifying it.</p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced