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>This example looks at the issues surrounding a mixed Java/Groovy application. This issue only arises when there is mutual dependencies between your mixed language source files. So, if part of your system is pure Java for instance, you won't have this problem. You would just compile that part of your system first and reference the resulting class/jar file(s) from the part of your system that was written in Groovy.</p> <h2>The legacy version of our application</h2> <p>Suppose you have an initial application written in Java. It deals with packages sent through the post. It has the following <code>Postpack</code> class (you would typically need some additional fields but we have simplified our domain to keep the example simple):</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package v1; public class Postpack implements Comparable { private final int weight; private final int zip; public int getWeight() { return weight; } public int getZip() { return zip; } public String toString() { return "Postpack[Weight=" + weight + ", Zip=" + zip + "]"; } public int compareTo(Object o) { Postpack other = (Postpack) o; return zip - other.getZip(); } public Postpack(int weight, int zip) { this.weight = weight; this.zip = zip; } } </pre></td></tr></table> <p>Now suppose you also have a sort helper class <code>ZipSorter</code> as follows:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package v1; import java.util.List; import java.util.Collections; public class ZipSorter { public List sort(List items) { Collections.sort(items); return items; } } </pre></td></tr></table> <p>Finally, you have a main application as follows:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package v1; import java.util.Arrays; public class SortMain { private static Postpack[] packs = { new Postpack(60, 12345), new Postpack(55, 98765), new Postpack(50, 54321) }; private static ZipSorter sorter = new ZipSorter(); public static void main(String[] args) { System.out.println("Sorted=" + sorter.sort(Arrays.asList(packs))); } } </pre></td></tr></table> <h2>A futile attempt at a quick hack</h2> <p>We have been asked to make a version 2 of our application which supports not only <code>Postpack</code> objects but also <code>Box</code> objects. We must also support the ability to sort by weight as well as Zip. We have been given incredibly short time scales to develop the application, so we decide to write all of the new functionality using Groovy.</p> <p>We start be creating a Groovy <code>Box</code> class and make it behave in the way <code>ZipSorter</code> is expecting, e.g. we make it implement <code>Comparable</code> even though this wouldn't be needed if everything was going to be in Groovy. We then modify <code>ZipSorter</code> to know about <code>Box</code>. We then create <code>WeightSorter</code> and write it to know about both <code>Postpack</code> and <code>Box</code>.</p> <p>To simplify development (we think) we create separate <code>groovy</code> and <code>java</code> source directories. We develop our files incrementally in our IDE and everything works fine. We think we are finished, so we do a rebuild all for our project. All of a sudden the project won't compile. We dive out to ant and use the <code>javac</code> and <code>groovyc</code> tasks to put compile our separated source directories. Still no luck. What happened?</p> <p>We inadvertently introduced a cyclic dependency into our codebase and we only got away with it originally because the incremental development style we were using hid away the problem. The issue is that IDEs and current build systems like Ant use different compilers for Java and Groovy. So while Java and Groovy are the same at the bytecode level, their respective compilers no nothing about the source code of the other language. (Recent discussions have begun about how to eventually remove this separation).</p> <p>Our hack failed because if we run <code>javac</code> first, <code>ZipSorter</code> won't have the <code>Box</code> class available because it is written in Groovy. If we run <code>groovyc</code> first, <code>WeightSorter</code> doesn't have the <code>Postpack</code> class because it is written in Java. Similarly, if using our IDE, we will face the same deadly embrace problem.</p> <h2>A proper version 2 of our application</h2> <p>The way we get around this problem is to define some common interfaces as follows:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package v2; public interface Parcel { int getWeight(); int getZip(); } </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package v2; import java.util.List; public interface Sorter { List sort(Parcel[] unsorted); } </pre></td></tr></table> <p>Now we write our Java and Groovy parts of the system being careful to refer only to the interfaces, e.g. the Java files would become:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package v2; public class Postpack implements Parcel, Comparable { private final int weight; private final int zip; public int getWeight() { return weight; } public int getZip() { return zip; } public String toString() { return "Postpack[Weight=" + weight + ", Zip=" + zip + "]"; } public Postpack(int weight, int zip) { this.weight = weight; this.zip = zip; } public int compareTo(Object o) { Parcel other = (Parcel) o; return zip - other.getZip(); } } </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package v2; import java.util.List; import java.util.Arrays; public class ZipSorter implements Sorter { public List sort(Parcel[] items) { Arrays.sort(items); return Arrays.asList(items); } } </pre></td></tr></table> <p>And the Groovy ones look like:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package v2 public class Box implements Parcel, Comparable { int weight int zip String toString() { return "Box[Weight=" + weight + ", Zip=" + zip + "]" } int compareTo(other) { return zip - other.zip } } </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package v2 public class WeightSorter implements Sorter { List sort(Parcel[] items) { items.toList().sort{ p1, p2 -> p1.weight <=> p2.weight } } } </pre></td></tr></table> <p>Finally, our main method looks like:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> package v2; import java.util.Arrays; public class SortMain { private static Box box1 = new Box(); private static Parcel[] packs = { new Postpack(60, 12345), box1, new Postpack(50, 54321) }; private static Sorter zipSorter = new ZipSorter(); private static Sorter weightSorter = new WeightSorter(); public static void main(String[] args) { box1.setWeight(55); box1.setZip(99999); System.out.println("Unsorted: " + Arrays.asList(packs)); System.out.println("Sorted by weight: " + weightSorter.sort(packs)); System.out.println("Sorted by zip: " + zipSorter.sort(packs)); } } </pre></td></tr></table> <p>We need to compile the interfaces first, then we can compile the Groovy or Java files (excluding <code>SortMain</code>) in either order. Finally we compile <code>SortMain</code> as it is the class that knows about the concrete implementations. If we were using dependency injection or our own factory methods we could have reduced or eliminated the need to treat <code>SortMain</code> as a special case, e.g. using Spring we could have the concrete classes listed in an external <code>beans.xml</code> file and <code>SortMain</code> whether it was written in Java or Groovy could have been compiled along with all the other files written in its language.</p> <p>Now when we run the program we get the following output:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Unsorted: [Postpack[Weight=60, Zip=12345], Box[Weight=55, Zip=99999], Postpack[Weight=50, Zip=54321]] Sorted by weight: [Postpack[Weight=50, Zip=54321], Box[Weight=55, Zip=99999], Postpack[Weight=60, Zip=12345]] Sorted by zip: [Postpack[Weight=60, Zip=12345], Postpack[Weight=50, Zip=54321], Box[Weight=55, Zip=99999]] </pre></td></tr></table> <p>For more details, see chapter 11 of <a href="http://groovy.canoo.com/gina">GINA</a>.</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