metagloss 0.0.2

metagloss is an annotation-centric android library for reducing boilerplate code.

See:
          Description

Packages
net.sf.metagloss Provides a sense of warmth for the classes deeper down the package hierarchy.
net.sf.metagloss.db Provides classes for working with databases.
net.sf.metagloss.injection Provides the InjectionManager and AnnotatedMethod.
net.sf.metagloss.preference Provides the AnnotatedPreferenceActivity along with its set annotations for binding and constraining data.
net.sf.metagloss.xml Provides classes for binding XML data to objects.

 

metagloss is an annotation-centric android library for reducing boilerplate code. Relying on run-time parsing and compile-time code generation, metagloss provides facilities for mapping XML and databases to data objects, dealing with preference screens and working with DB queries. Annotations are validated during compilation in order to detect typos and misconfigurations.

Features

Propaganda

Enough! or too much.

Creating data objects from an SQLite Cursor

The only thing required to make a class eligible for setter injection is annotating the proper fields or methods with CursorBind. A similar approach is used when injecting from XML sources (see DOMFeeder and XMLBind).

metagloss

List hosts = new ArrayList();
Cursor cursor = ...;

// conserving space for side-by-side comp.
AssistCursor<UserHostTable> ac =
    new AssistCursor<UserHostTable>(
        UserHostTable.class, cursor);

while (ac.next())
{
    Host host = new Host();
    ac.inject(host);
    hosts.add(host);
}
cursor.close(); // etc
    

Stock android

List hosts = new ArrayList();
Cursor cursor = ...;

int id = 
    cursor.getColumnIndex(UserHostTable.ID);
int userTableId =
    cursor.getColumnIndex(UserHostTable.USER_TABLE_ID);
int cpid =
    cursor.getColumnIndex(UserHostTable.CPID);
int name = ...
int hostId = ...
int createdOn = ...
int credit = ...
int rac = ...
int cpuCount = ...
int flops = ...
int mips =  ...
int vendor = ...
int model =  ...
int os =  ...

while (cursor.moveToNext())
{
    Host host = new Host();
    host.setDatabaseId(cursor.getInt(id));
    host.setUserId(cursor.getInt(userTableId));
    host.setCPID(cursor.getString(cpid));
    host.setName(cursor.getString(name));
    host.setHostId(cursor.getInt(hostId));
    host.setCreationTime(cursor.getLong(createdOn));
    host.setCredit(cursor.getInt(credit));
    host.setCreditRAC(cursor.getInt(rac));
    host.setCPUCount(cursor.getInt(cpuCount));
    host.setFlops(cursor.getInt(flops));
    host.setMips(cursor.getInt(mips));
    host.setVendor(cursor.getInt(vendor));
    host.setModel(cursor.getInt(model));
    host.setOSName(cursor.getInt(os));

    hosts.add(host);
}
cursor.close(); // etc
    

Creating ContentValues from data object

Instances of classes annotated with CursorBind are simple to parse into ContentValues.

metagloss

Host host = ...;
ContentValues cv = new ContentValues();
DBUtils.buildContentValues(cv, host);
    

Stock android

Host host = ...;
ContentValues cv = new ContentValues();
cv.put(UserHostTable.ID, host.getDatabaseId());
cv.put(UserHostTable.USER_TABLE_ID, host.getUserId());
cv.put(UserHostTable.CPID, host.getCPID());
cv.put(UserHostTable.NAME, host.getName());
cv.put(UserHostTable.HOST_ID, host.getHostId());
cv.put(UserHostTable.CREATED_ON, host.getCreationTime());
cv.put(UserHostTable.CREDIT, host.getCredit());
cv.put(UserHostTable.RAC, host.getCreditRAC());
cv.put(UserHostTable.CPU_COUNT, host.getCPUCount());
cv.put(UserHostTable.FLOPS, host.getFLOPS());
cv.put(UserHostTable.MIPS, host.getMIPS());
cv.put(UserHostTable.VENDOR, host.getVendor());
cv.put(UserHostTable.MODEL, host.getModel());
cv.put(UserHostTable.OS, host.getOSName());
    

Installing metagloss in Eclipse

As released binary

  1. Make sure that Eclipse is installed and that ADT is at least at version 0.9.8
  2. Download the latest version.
  3. Go to File | Import...
  4. In the Import window, choose General / Existing Projects into Workspace
  5. Choose Select archive file and point it to the location of the downloaded metagloss.tar.gz
  6. Check metagloss and click Finish
  7. RMB click on your android project of choice, choose Properties | Android
  8. Press the appropriate Add..., choose metagloss

From svn

  1. Make sure that Eclipse is installed and that ADT is at least at version 0.9.8
  2. Open Eclipse and check out metagloss project from http://metagloss.svn.sourceforge.net/svnroot/metagloss/trunk/metagloss
  3. RMB click on your android project of choice, choose Properties | Android
  4. Press the appropriate Add..., choose metagloss

Dependency: Project Lombok

Project Lombok is internally used by some parts of metagloss to reduce the boilerplate code (the irony...). To install Project Lombok, follow the instructions on http://projectlombok.org/features/index.html - the binary is already distributed inside the lib folder - to configure Eclipse.

To make it play (relatively) nice with Eclipse/ADT, configure as follows:

  1. Install Project Lombok (see above).
  2. Open Eclipse
  3. RMB click your android project, choose Build Path | Add Libraries...
  4. Choose User Library, next window, click User Libraries
  5. Click New... and give it an appropriate name
  6. Select your newly created library, click Add JARs...
  7. Locate and add lombok.jar, typically in your $ECLIPSE_HOME
  8. Click OK to return to the previous window
  9. Check your newly crated lombok (user) library
In the future, metagloss will also provide a delomboked release.

Setting up the annotation processors

To ease the life of programmers, metagloss provides compile-time validation of annotated classes via a collection of annotation processors. Rudimentary compliance checks are made to ensure that annotations decorate eligible elements and contain sensible data. Any discovered errors and erroneous conditions are emitted as markers inside Eclipse's editor and problem view.

Video is a little out-of-date in regards to metagloss' processors, but the procedure is the same.

Screencasts and videos online

To enable metagloss' annotation processor, bring up your project's properties window: Enable project specific settings for Java Compiler | Annotation Processing and Java Compiler | Annotation Processing | Factory Path. In the Factory Path window, choose Add JARs... and point it to metagloss/lib/metagloss-annotationprocessor.jar.

Naively childish

The library is still maturing from its infant state; as such, drastic changes may occur in-between releases. Future versions will be less prone to invasive changes.

The original code was extrapolated from the more refined parts of BOINC Echo - it's been using metagloss without incident, though it hasn't been extensively tested in other configurations.

Paying tribute where tribute is due

The Taglets Collection is a lib and collection of ready-to-use javadoc taglets/custom tags. The {@at} and {@source} taglets are truly wonderful and should have been part of the javadoc tool long ago.

Project Lombok is a compile-time, annotation-based code generator capable of @Getter/@Setter generation plus @ToString, @EqualsAndHashCode and many more. Inspired piece of work.

Resources

Sourceforge project page:
http://sf.net/projects/metagloss
Subversion repository
http://metagloss.svn.sourceforge.net/svnroot/metagloss/
mailing-list / google group:
metagloss-android@googlegroups.com
http://groups.google.com/group/metagloss-android
metagloss license
http://www.apache.org/licenses/LICENSE-2.0.html
ohloh page
https://www.ohloh.net/p/metagloss