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
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 is a powerful tool. Like other powerful tools (think of a chainsaw) it requires a certain amount of user expertise and attention. Otherwise, results may be fatal.</p> <p>Following code fragments are allowed in Groovy but usually result in unintended behaviour or incomprehensible problems. Simply don't do this and spare yourself some of the frustration that new and unskilled Groovy programmers sometimes experience.</p> <p><strong>1. Accessing an object's type like a property</strong></p> <p>Using <code>.class</code> instead of <code>.getClass()</code> is ok - as long as you know exactly what kind of object you have. But then you don't need that at all. Otherwise, you run in the risk of getting <code>null</code> or something else, but not the class of the object.</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 = [:] println a.class.simpleName // NullPointerException, because a.class is null. </pre></td></tr></table> <p><strong>2. Omitting parentheses around method arguments when not appropriate</strong></p> <p>Better use parentheses for argument lists unless you are sure.<br /> For instance, a left parenthesis after a method name must always enclose the parameter list.</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> println a*(b+c) // is OK println (a+b)*c // NullPointer exception, because println (a+b) results in null. </pre></td></tr></table> <p><strong>3. Putting a newline at the wrong place into the code</strong></p> <p>This may cause the compiler to to assume a complete statement although it is meant as continuing in the next line. It is not always as obvious as in the following 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> myVariable = "This is a very long statement continuing in the next line. Result is=" + 42 // this line has no effect </pre></td></tr></table> <p><strong>4. Forgetting to write the second equals sign of the equals operator</strong></p> <p>As a result, a comparison expression turns into an assignment.</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> while (a=b) { ... } // will be executed as long as b is true (Groovy-truth) String s = "A"; boolean b = s = "B" // b becomes true </pre></td></tr></table> <p>(One measure is to put constant expressions in comparisons always before the equals operator.)</p> <p><strong>5. Specifying the wrong return type when overriding a method</strong> (Groovy version < 1.5.1 only)</p> <p>Such a method may be considered as overloaded (a different method with the same name) and not be called as expected.</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 toString() { ... } // does not override toString() inherited from Object </pre></td></tr></table> <p><strong>6. Disregarding other objects' privacy</strong></p> <p>When accessing methods, fields, or properties of other classes, make sure that you do not interfere with private or protected members. Currently Groovy doesn't distinguish properly between public, private, and protected members, so watch out yourself.</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> z = [1,2,3] z.size = 10 // size is private; errors when trying to access z[3] to z[9], although z.size()==10 'more'.value = 'less' // results in "more"=="less" </pre></td></tr></table> <p><strong>7. Thoughtless dynamic programming</strong></p> <p>Check for conflicts with intended class functionality before adding or changing methods or properties using Groovy's dynamic facilities.</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> String.metaClass.class = Integer // results in 'abc'.getClass()==java.lang.Integer </pre></td></tr></table> <p><strong>8. String concatenation</strong><br /> As in Java, you can concatenate Strings with the "+" symbol. But Java only needs that one of the two items of a "+" expression to be a String, no matter if it's in the first place or in the last one. Java will use the <code>toString()</code> method in the non-String object of your "+" expression. But in Groovy, you just should be safe the first item of your "+" expression implements the <code>plus()</code> method in the right way, because Groovy will search and use it. In Groovy GDK, only the Number and String/StringBuffer/Character classes have the <code>plus()</code> method implemented to concatenate strings. To avoid surprises, always use GStrings.</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> // Java code, it works boolean v = true; System.out.println(" foo "+v); System.out.println(v+" foo "); </pre></td></tr></table> <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> // Groovy code boolean v = true println " foo "+v // It works println v+" foo " // It fails with MissingMethodException: No signature of method: java.lang.Boolean.plus() </pre></td></tr></table>
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