Saturday, March 23, 2013

My First Xtend Program

Hi all! This is Ross again. For those who don't know, Oracle created a spinoff of Java called Xtend. It was created to make JVM prgramming easier. After much trial-and-error, I got Xtend to work on my Windows 8 laptop. Right now, as far as I know Xtend in available only for Eclipse. If I find a NetBeans version, I'll post about it. Eclipse translates Xtend code into Java when building the project. I'm using Morphia and MongoDB, and I assume you have their JARs in the appropriate JRE directory.

First of all, you can find Xtend at http://www.eclipse.org/xtend/ . Click on the "Download" button, then choose the "Full Eclipse" most appropriate for your operating system. If you choose a Windows version, it will come as a Zip folder. I'm redoing everything for the purpose of this blog. For this purpose, I'm extracting the Zip to C:\Xtend . You will find the Eclipse application in the "eclipse" subdirectory.

Next, I created a Java Project called "test1". When I created it, I added the Xtend library to its classpath. Next, I added an Xtend class called "Name" in the new package "org.main". Here is the code for the Name class:



package org.main

import com.google.code.morphia.annotations.Entity
import com.google.code.morphia.annotations.Id
import com.google.code.morphia.annotations.Serialized

@Entity("names")
@Data public class Name {

@Id
String id

@Serialized("firstName")
String firstName

@Serialized("lastName")
String lastName

public new(String first, String last) {
_id = null
_firstName = first
_lastName = last
}

public new() {
_id = ""
_firstName = ""
_lastName = ""
}

}



The "Entity", "Id" and "Serialized" annotations are for MongoDB's and Morphia's benefit. The "Data" annotation tells the  Java generator to create a contructor, as well as getters and setters. The "new" methods are Xtend constructors. The underscores are necessary when using "@Data", as it also translates your members into private members with names prefixed by underscores.  The constructor with the empty argument list is so that Morphia can convert MongoDB objects to the Java class.  If we weren't using "@Data", this("default") would work instead of manually initializing the members to null strings.

Now, for the main attraction... the "main" method. I created an Xtend class named "Program" in the "org.main" package. Here is that entire class:


package org.main

import java.net.UnknownHostException
import java.util.Scanner
import com.mongodb.MongoClient
import com.mongodb.BasicDBObject
import com.google.code.morphia.Morphia

class Program {
def public static void main(String[] args) {
try {
var scanner = new Scanner(System::in)
print("First name: ")
var first = scanner.nextLine
print("Last name: ")
var last = scanner.nextLine

var client = new MongoClient
var db = client.getDB("xtend2")
var collection = db.getCollection("names")

var query = new BasicDBObject("firstName", first).append("lastName", last)
var count = collection.find(query).count

if (count > 0) {
println("That name is already on file.")
}
else {
println("Proceeding...")
var morphia = new Morphia
var name = new Name(first, last)
var jname = morphia.toDBObject(name)
collection.save(jname)
println("The new name was successfully added.")
}

var cursor = collection.find() // get the entire collection
var morphia = new Morphia().map(typeof(Name))
for (obj : cursor) {
var BasicDBObject basic = obj as BasicDBObject
var name = morphia.fromDBObject(typeof(Name), basic)
println(name.id + " : " + name.firstName + " " + name.lastName)
}

client.close

catch(UnknownHostException e) {
println("Error: " + e.message)
}

}


I know you might be scratching your head, thinking "What kind of Java is this?". This first thing you might notice is there aren't and semicolons. You usually don't need then in Xtend. Also, instead of System.in, you use System::in . Console output has been greatly simplified; all you need are print and println. Empty method parentheses can be omitted. Unlike Java, Xtend can handle implicit type determination. In Xtend, you send class type arguments using typeof. Explicit casting is also done differently. You can see an example in the above  obj as BasicDBObject . Lastly, since I used the "Data" annotation earlier, I can access the members of the "Name" class as though they were public.

What does this program do? It asks for your name, then checks it against the database. If there is no match,your name gets added. Lastly, the updated document list is displayed.

There are other differences between Xtend and Java, but I won't go into them here. If you need more clarification of my project, e-mail me at euric.reiks@comcast.net . Thanks for reading this post, and God be with you!

2 comments:

  1. Interesting. It looks like a wrapper of syntactical sugar for java.

    ReplyDelete
  2. Oracle had nothing to do with Xtend. It was created by Itemis, a German company.

    ReplyDelete