Reference types in the java scheme
The java: scheme represents the Java language, so reference types in the scheme are just regular Java classes and interfaces. The Meso language does not support generics yet.
Currently, we can use compiled Java classes from a Meso program, like the java:java.lang.System class we use in many examples, but we cannot define a new Java class using the Meso language yet. This can be very useful if one wants to leverage the Meso language to work with remote IMOP objects, but then generates a plain old Java class to be used in an existing system. We are planing to implement this capability.
For example, assume that we have an existing Java program has an interface named com.twitter.IUserProfile to represent a Twitter profile as follows:
// This is Java source code
package com.twitter;
import java.io.IOException;
public interface IUserProfile {
String getUserName( String userId ) throws IOException;
// ... (other methods)
}
To implement this interface in Java today, we will need to call Twitter's RESTful API using HTTP and then parse the returned message manually to get to the data buried inside, like so:
// This is Java source code
package com.twitter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
public class UserProfileImpl implements IUserProfile {
public String getUserName(int userId) throws IOException {
InputStream in = null;
try {
// calls the API
URL url = new URL("http://api.twitter.com/1/users/lookup.json?user_id="+userId);
in = url.openStream();
// gets the name value
Reader reader = new InputStreamReader(in);
JSONTokener tokener = new JSONTokener(reader);
JSONArray profiles = new JSONArray(tokener);
JSONObject profile = profiles.getJSONObject(0);
return profile.getString("name");
} catch (JSONException e) {
throw new IOException(e);
} finally {
if (in != null)
in.close();
}
}
// ...
}
Rather than doing this, assume that one day Twitter offers an IMOP version of their APIs:
namespace imop:api.twitter.com/user;
public struct Profile {
int userId;
string name;
// ...
}
namespace imop:api.twitter.com/user;
public object Lookup {
Profile getProfile( int userId ) {
Profile p = // ... gets from the backend system
return p;
}
}
Then we may want to re-write our com.twitter.UserProfileImpl class using the Meso language as below so that we can use the IMOP APIs directly. Since the namespace is declared in the java: scheme, this Meso source code should be compiled to a Java class:
// This is Meso source code
namespace java:com.twitter;
import imop:api.twitter.com/user.Profile;
import imop:api.twitter.com/user.Lookup;
import meso:imop.ImopIOException;
import java:java.io.IOException;
public class UserProfileImpl implements IUserProfile {
public string getUserName(int userId) throws IOException {
try {
// calls the API
Profile profile = Lookup.getProfile( userId );
// gets the name value
return profile.name;
} catch (ImopIOException e) {
throw new IOException( e.getMessage() );
}
}
// ...
}
Not only the code above is much cleaner than the RESTful API version, but an IDE can also perform automatic code completion by listing methods in Lookup and fields in Profile ― something very difficult to achieve with RESTful APIs, yet very useful to simplify the development task.
