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
Dashboard
Groovy
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>Groovy provides some very convenient ways to implement interfaces.</p><h2>Implement interfaces with a closure</h2><p>An interface with a single method can be implemented with a closure like so:</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>// a readable puts chars into a CharBuffer and returns the count of chars added def readable = { it.put("12 34".reverse()); 5 } as Readable // the Scanner constructor can take a Readable def s = new Scanner(readable) assert s.nextInt() == 43 </pre></td></tr></table><p>You can also use a closure to implement an interface with more than one method. The closure will be invoked for each method on the interface. Since you need a closure whose parameter list matches that of all of the methods you typically will want to use an array as the sole parameter. This can be used just as it is for any Groovy closure and will collect all of the arguments in an array. For example:</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>interface X { void f() void g(int n) void h(String s, int n) } x = {Object[] args -> println "method called with $args"} as X x.f() x.g(1) x.h("hello",2) </pre></td></tr></table><h2>Implement interfaces with a map</h2><p>More commonly an interface with multiple methods would be implemented with a map like so:</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>impl = [ i: 10, hasNext: { impl.i > 0 }, next: { impl.i-- }, ] iter = impl as Iterator while ( iter.hasNext() ) println iter.next() </pre></td></tr></table><p>Note this is a rather contrived example, but illustrates the concept.</p><p>You only need to implement those methods that are actually called, but if a method is called that doesn't exist in the map a <em>NullPointerException</em> is thrown. For example:</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>interface X { void f(); void g(int n); void h(String s, int n); } x = [ f: {println "f called"} ] as X x.f() //x.g() // NPE here </pre></td></tr></table><p>Be careful that you don't accidentally define the map with { }. Can you guess what happens with the following?</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>x = { f: {println "f called"} } as X x.f() x.g(1) </pre></td></tr></table><p>What we've defined here is a closure with a label and a block. Since we've just defined a single closure every method call will invoke the closure. Some languages use { } to define maps so this is an easy mistake until you get used to using [:] to define maps in Groovy.</p><p>Note that using the "as" operator as above requires that you have a static reference to the interface you want to implement with a map. If you have a reference to the java.lang.Class object representing the interface (i.e. do not know or can not hard code the type at script write time) you want to implement, you can use the asType method like this:</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>def loggerInterface = Class.forName( 'my.LoggerInterface' ) def logger = [ log : { Object[] params -> println "LOG: ${params[0]}"; if( params.length > 1 ) params[1].printStackTrace() }, close : { println "logger.close called" } ].asType( loggerInterface ) </pre></td></tr></table><p>See also:</p><ul><li><a class="confluence-link" href="/display/GROOVY/Developer+Testing+using+Closures+instead+of+Mocks" data-linked-resource-id="66905" data-linked-resource-type="page" data-linked-resource-default-alias="Developer Testing using Closures instead of Mocks" data-base-url="http://docs.codehaus.org">Developer Testing using Closures instead of Mocks</a></li></ul>
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