Pages

Showing posts with label xtend. Show all posts
Showing posts with label xtend. Show all posts

Sunday, November 2, 2014

Initial experience with Xtend + RoboVM

Setting stuff up


I woke up feeling like doing something for Xtend developers, so I decided to check out RoboVM.

RoboVM is a project sponsored by Trillian Mobile AB, Gothenborg, Sweden.


I had so much fun contributing to the Xtendroid, I thought I could pull the same trick for the RoboVM project; the trick being writing massively intelligent boilerplate killers for common coding tasks.

I plan to use Xtend and its active annotations to cut away RoboVM boilerplate.

I set up the following things:
  • Installed java 8; (\o/ yea for lambdas)
  • Installed Xcode, installed the command-line tools (you must have a mac);
  • Installed Eclipse (version Luna, updated it);
  • Installed Xtend on eclipse via the update site;
  • Installed RoboVM on eclipse via the update site;
  • Cloned (via git) and imported (eclipse) the official RoboVM sample projects;
  • Cloned an imported the Xtend+RoboVM sample project
Also checkout these RoboVM bindings, they might be useful later.

Caveat emptor!

Beware buyer! I immediately got the feeling that the samples aren't being maintained much after the recent release; some of the method names declared in the official RoboVM samples are slightly fubar. Eclipse will complain that certain @Override declarations are not required. You will have to find the correct method and rename the borken method to that correct one. Most of the work requires matching method signatures.

If you feel generous, you can do a pull-request via github where the samples are hosted; that's what I'm planning on doing too.

The other thing that bothered me is the following error that occurs after you push the compile button, on certain sample projects:

Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/ref/FinalReference

After I filtered through the noise on StackOverflow and some additional mucking around with java and eclipse.ini, it dawned on me, that I should use "compile as..." build option instead of trusting the default compilation settings.

Everything went smoothly afterwards.

Watch this space

Like I said, I plan on doing a Xtendroid on RoboVM. I think I'll call it XtendRobo, because it sounds cute.

Cheerio.

Wednesday, June 18, 2014

Full support for Xtend on Spark

Spark and Xtend

Spark (the micro web framework) accidentally started supporting Xtend lambda syntax to define Routes, since Spark started supporting java8 lambdas.

 For instance:

package spark

import static spark.Spark.before
import static spark.Spark.get
import static spark.Spark.halt

class XtendTest {
    def static void main(String[] args) {
        get("/hello/:name", [request, response |
            System.out.println("request = " + request.pathInfo());
            return "Hello " + request.params("name")
        ])
        
        val boilerplate = "is a necessary evil"
        val builtInTemplatingSystem = "woohoo, gotta love xtend, I ain't gotta escape no nothin'."
        before("/protected/*", "application/json", [request, response |
            halt(401,
            '''
            {"«boilerplate»": "«builtInTemplatingSystem»"}
            ''')
        ])
        
        get("/love/me/some/xml", "application/xml", [rq, rp | '''
        
  «boilerplate»
        
        '''])
    }
}
And the generated code:
package spark;

import org.eclipse.xtend2.lib.StringConcatenation;
import spark.Filter;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.Spark;

@SuppressWarnings("all")
public class XtendTest {
  public static void main(final String[] args) {
    final Route _function = new Route() {
      public Object handle(final Request request, final Response response) {
        String _pathInfo = request.pathInfo();
        String _plus = ("request = " + _pathInfo);
        System.out.println(_plus);
        String _params = request.params("name");
        return ("Hello " + _params);
      }
    };
    Spark.get("/hello/:name", _function);
    final String boilerplate = "is a necessary evil";
    final String builtInTemplatingSystem = "woohoo, gotta love xtend, I ain\'t gotta escape no nothin\'.";
    final Filter _function_1 = new Filter() {
      public void handle(final Request request, final Response response) throws Exception {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("{\"");
        _builder.append(boilerplate, "");
        _builder.append("\": \"");
        _builder.append(builtInTemplatingSystem, "");
        _builder.append("\"}");
        _builder.newLineIfNotEmpty();
        Spark.halt(401, _builder.toString());
      }
    };
    Spark.before("/protected/*", "application/json", _function_1);
    final Route _function_2 = new Route() {
      public Object handle(final Request rq, final Response rp) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("        ");
        _builder.append("");
        _builder.newLine();
        _builder.append(boilerplate, "");
        _builder.newLineIfNotEmpty();
        _builder.append("        ");
        _builder.append("");
        _builder.newLine();
        return _builder;
      }
    };
    Spark.get("/love/me/some/xml", "application/xml", _function_2);
  }
}

So, what do you get for it?
  1. Everything Xtend has to offer, e.g. lambdas, Active Annotations;
  2. Another native templating system, i.e. the ''' blah ''' syntax, that translates to native java, thus negating the necessity for a third party templating system.
So, how do I start playing with it?
  1. Git clone and import the project thru git into eclipse;
  2. (Optional) Setup maven, on your IDE and OS;
  3. Ensure that you have the proper Xtext/Xtend dependencies installed in eclipse, look here for instructions;
  4. Start hacking Xtend code using the Spark framework to build your API or web site, and forget about MVC... and start writing unmanageable, unbuzzworded (e.g. separation of concerns, MVC, etc.) code.

Caveat

It's not necessary to have java8 installed on your system, but some of the examples that rely on java8 features will break. Xtend/Spark with java7 works just fine.

The author (me) tested this stuff on Eclipse Juno, using the latest stable Xtend version (2.3.*) and the latest release of Spark, using apache maven 3.0.5.

Edit (24 June 2014):

A few days after discovering this. I found out about Sparkle. Sparkle is bundled with Xtend and everything else, it was a prescient refactoring of Sparky, somewhere around a year ago, it hasn't been updated for a while.