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
Annogen
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
<p>This page provides a quick run-through of what the <code>stored_override</code> example does. For more details, you should refer to the source, which you will find in the download kit under:</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> examples/stored_override </pre></td></tr></table> <p>You can build and run the example by simply <code>cd</code>'ing into that directory and typing <code>ant</code>. </p> <h3>Example Scenario</h3> <p>Suppose that we have a simple JSR175 annotation type called <code>DeploymentInfo</code> which specifies a 'cacheSize' attribute for some kind of deployment unit:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="borderStyle=solid|title=DeploymentInfo.java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6Ym9yZGVyU3R5bGU9c29saWR8dGl0bGU9RGVwbG95bWVudEluZm8uamF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> @AnnogenInfo( annoBeanClass = "org.codehaus.annogen.examples.stored_override.DeploymentInfoAnnoBean" ) @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface DeploymentInfo { public int cacheSize(); } </pre></td></tr></table> <p>Suppose that we also have an deployment unit called <code>TinyCacheEJB</code> which has a <code>DeploymentInfo</code> annotation:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="borderStyle=solid|title=TinyCacheEJB.java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6Ym9yZGVyU3R5bGU9c29saWR8dGl0bGU9VGlueUNhY2hlRUpCLmphdmF9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> @DeploymentInfo(cacheSize = 3) public class TinyCacheEJB { //... } </pre></td></tr></table> <p>This example demonstrates a very simple way in which we might use Annogen to allow for the value of <code>cacheSize</code> to be overridden. </p> <h3>Generate Phase</h3> <p>The first thing we have to do is generate an <code>AnnoBean</code> that will act as a proxy to instances of <code>DeploymentInfo</code>. To do this, we simply run Annogen's compilation task on <code>DeploymentInfo</code>:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="borderStyle=solid|title=build.xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6Ym9yZGVyU3R5bGU9c29saWR8dGl0bGU9YnVpbGQueG1sfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <annogen outputdir='${codegen_dir}' classpathref='classpath' srcdir='src' includes='org/codehaus/annogen/examples/stored_override/DeploymentInfo.java' implementAnnotationTypes='true'/> </pre></td></tr></table> <p><br class="atl-forced-newline" /></p> <p>This generates a <code>DeploymentInfoAnnoBean</code> that implements <code>DeploymentInfo</code>:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="borderStyle=solid|title=DeploymentInfoAnnoBean.java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6Ym9yZGVyU3R5bGU9c29saWR8dGl0bGU9RGVwbG95bWVudEluZm9Bbm5vQmVhbi5qYXZhfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> public class DeploymentInfoAnnoBean extends AnnoBeanBase implements DeploymentInfo { private int cacheSize; public int cacheSize() { return this.cacheSize; } public void set_cacheSize(int in) { this.cacheSize = in; } } </pre></td></tr></table> <h3>Override Phase</h3> <p>Notice that the generated <code>DeploymentInfoAnnoBean</code> class has a setter for <code>cacheSize</code>. We're now going to create an <table class="wysiwyg-macro" data-macro-name="unmigrated-inline-wiki-markup" data-macro-parameters="atlassian-macro-output-type=BLOCK" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3VubWlncmF0ZWQtaW5saW5lLXdpa2ktbWFya3VwOmF0bGFzc2lhbi1tYWNyby1vdXRwdXQtdHlwZT1CTE9DS30&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>{link:AnnoOverrider|http://annogen.codehaus.org/annogen-api/org/codehaus/annogen/override/AnnoOverrider.html}{link}</pre></td></tr></table> which uses that setter to effectively change the size of the cache on <code>TinyCacheEJB</code>. We create an instance of <table class="wysiwyg-macro" data-macro-name="unmigrated-inline-wiki-markup" data-macro-parameters="atlassian-macro-output-type=BLOCK" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3VubWlncmF0ZWQtaW5saW5lLXdpa2ktbWFya3VwOmF0bGFzc2lhbi1tYWNyby1vdXRwdXQtdHlwZT1CTE9DS30&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>{link:StoredAnnoOverrider|http://annogen.codehaus.org/annogen-api/org/codehaus/annogen/override/StoredAnnoOverrider.html}{link}</pre></td></tr></table>, which is a helper class Annogen provides for building simple <code>AnnoOverriders</code>.</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="borderStyle=solid|title=User.java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6Ym9yZGVyU3R5bGU9c29saWR8dGl0bGU9VXNlci5qYXZhfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> StoredAnnoOverrider storedOverrides = StoredAnnoOverrider.Factory.create(); </pre></td></tr></table> <p><br class="atl-forced-newline" /> <code>StoredAnnoOverrider</code> requires us to make an <table class="wysiwyg-macro" data-macro-name="unmigrated-inline-wiki-markup" data-macro-parameters="atlassian-macro-output-type=BLOCK" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3VubWlncmF0ZWQtaW5saW5lLXdpa2ktbWFya3VwOmF0bGFzc2lhbi1tYWNyby1vdXRwdXQtdHlwZT1CTE9DS30&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>{link:ElementId|http://annogen.codehaus.org/annogen-api/org/codehaus/annogen/override/ElementId.html}{link}</pre></td></tr></table> for the <code>TinyCacheEJB</code> class:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="borderStyle=solid" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6Ym9yZGVyU3R5bGU9c29saWR9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> ReflectElementIdPool elementPool = ReflectElementIdPool.Factory.create(); ElementId tinyCacheEJBClassId = elementPool.getIdFor(TinyCacheEJB.class); </pre></td></tr></table> <p><br class="atl-forced-newline" /> ...which we will use to get an instance of the <code>DeploymentInfoAnnoBean</code> which applies to <code>TinyCacheEJB</code>.</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="borderStyle=solid" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6Ym9yZGVyU3R5bGU9c29saWR9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> AnnoBeanSet annos = storedOverrides.findOrCreateStoredAnnoSetFor(tinyCacheEJBClassId); DeploymentInfoAnnoBean annoBean = (DeploymentInfoAnnoBean) annos.findOrCreateBeanFor(DeploymentInfo.class); </pre></td></tr></table> <p><br class="atl-forced-newline" /> The <code>StoredAnnoOverrider</code> will provide this <code>DeplyomentInfoAnnoBean</code> as a replacement for the <code>DeploymentInfo</code> annotation on <code>TinyCacheEJB</code>. So, if we change the value of cacheSize on it:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="borderStyle=solid" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6Ym9yZGVyU3R5bGU9c29saWR9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> annoBean.set_cacheSize(newCacheSize); </pre></td></tr></table> <p><br class="atl-forced-newline" /> We have changed the value of cache size for <code>TinyCacheEJB</code> as it will appear when our framework code views its <code>DeploymentInfo</code> annotation.</p> <h3>View Phase</h3> <p>Compared to the Override phase, the View phase is very simple. All we have to do is create a <table class="wysiwyg-macro" data-macro-name="unmigrated-inline-wiki-markup" data-macro-parameters="atlassian-macro-output-type=BLOCK" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3VubWlncmF0ZWQtaW5saW5lLXdpa2ktbWFya3VwOmF0bGFzc2lhbi1tYWNyby1vdXRwdXQtdHlwZT1CTE9DS30&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>{link:ReflectAnnoViewer|http://annogen.codehaus.org/annogen-api/org/codehaus/annogen/view/ReflectAnnoViewer.html}{link}</pre></td></tr></table> which knows about the <code>AnnoOverrider</code> we created above:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="borderStyle=solid|title=DeployerTool.java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6Ym9yZGVyU3R5bGU9c29saWR8dGl0bGU9RGVwbG95ZXJUb29sLmphdmF9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> annoViewer = ReflectAnnoViewer.Factory.create(myAnnoOverrider); </pre></td></tr></table> <p><br class="atl-forced-newline" /> This <code>annoViewer</code> can now tell us about any annotations we want to know about, and it will take our overrides into account. So, if we get the <code>DeploymentInfo</code> for <code>TinyCacheEJB</code>:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="borderStyle=solid" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6Ym9yZGVyU3R5bGU9c29saWR9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> DeploymentInfo deploymentInfo = (DeploymentInfo) annoViewer.getAnnotation(DeploymentInfo.class, TinyCacheEJB.class); </pre></td></tr></table> <p><br class="atl-forced-newline" /> we will get back a DeploymentInfo annotation object which reflects our cacheSize override. </p> <p>In reality, <code>deploymentInfo</code> is an instance of <code>DeploymentInfoAnnoBean</code>, but one nice thing here is that it is an instance of our regular 175 annotation type, <code>DeploymentInfo</code>. We can look at the <code>cacheSize</code> value on the TinyCacheEJB and just go deploy it without a bunch of extra messy logic to check whether the value was overridden.</p> <p>This illustrates one of the key advantages that Annogen provides: the logic for overriding annotations is cleanly paritioned away from the code which is trying to do something with those annotations. </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