StateJ
one partial circle and three smaller circles in various shades of blue one blue circle

A state abstraction for coordination in Java-like languages

Ferruccio Damiani, Elena Giachino, Paola Giannini, Nick Cameron and Sophia Drossopoulou.

Abstract

Objects' "state", intended as some abstraction over the value of fields, is always in the mind of a COOL (Concurrent Object-Oriented Language) programmer. In fact, as the "state" of an object changes so does usually the coordination behaviour. We introduce a language feature for expressing the notion of "state" in Java-like languages.

The proposed feature takes the form of a new kind of class (that we call "state class") which is equipped with a type and effect system guaranteeing that, even though the "state" of the objects may vary through states with different fields, no attempt will be made to access non-existing fields.

Implementation

Here is a prototype compiler for StateJ by Nicholas Cameron. The compiler is a preprocessor - it translates StateJ (Java extended with cojnstructs to specify explicit state) to Java. Currently only a subset of Java can be used. StateJ classes are mapped directly to Java classes and inheritance is mapped straightforwardly so that if StateJ class A extends StateJ class B, then the translation will be Java class A' extends Java class B'.

It should be noted that the compiler compiles a slightly different language from that described in the paper: fields can not be specified in states and therefore an effect system is not used. Furthermore there are slight differences in the syntax of the language: for example all classes are regarded as state classes and therefore the 'state' keyword is not necessary.

Download source code
Download compiled class files

Usage: Java sjc sourceFileName [sourceFileName...]

The compiler will read any number of source files (requires at least one). There is no enforced correlation between files and classes as in Java.

Example StateJ program: Buffer.statej

import java.util.ArrayList;

class Buffer {
	protected ArrayList cells = new ArrayList();
	protected int current = 0;
	protected int max = 0;

	public Buffer (int max)	{
		super();
		current = 0;
		this.max = max;
		this!!Empty;
	}
	
	state Empty	{
		public synchronized void put(Object datum) {
			cells.set(current, datum);
			current = current + 1;
			
			if (current >= max)
				this!!Full;
			if (current < max)
				this!!Partial; //this only needs doing if we are in Empty, not Partial
		}
	}
	
	state Full {
		public synchronized Object get() {
			current = current -1;
			Object returnValue = cells.get(current);
			
			if (current <= 0)
				this!!Empty;
			if (current > 0)
				this!!Partial; //this only needs doing if we are in Full
				
			return returnValue;
		}
	}
	
	state Partial {
		public synchronized void put(Object datum) as Empty
		public synchronized Object get() as Full
	}
	
}

View the translated Java file (buffer.java)

one blue circle