Eiffel-Like seperate Classes
Extending Java's concurrency model with asynchronous objects
JDJ Issue 05-11; p.56

Listing 1: Sample class code for later processing
separate public class A {
 void m1(){
    //... m1's code
 }

 public void m2(Integer x) {
  // ... m2's code
 }

   // ... Other class' code
}

Listing 2: A straightforward implementation of separate behavior attached statically to a class
import java.lang.reflect.*;

public class A implements Runnable {
 // Data needed for each method invocation
 private Method _method;    // Method to invoke
 private Object[] _params; // Method's parameters
 private Object _ret;    // Return value

 public void run(){
  try {
  _ret = __method.invoke(this,_params);
 } catch(Exception e){
  System.out.println("Error: " + e.getMessage());
 };
 }

 // Methods to execute the separate way
 synchronized public void m1(){
  Class [] FormalParams = new Class[0];
 _params = new Object[0];
 try {
    _method = this.getClass().getMethod("orig_m1",
                                     FormalParams);
   } catch(NoSuchMethodException e){
  System.out.println("Error: " + e.getMessage());
 };
   new Thread(this).start();
 return;
 }

 synchronized public void m2(Integer x) {
  Class [] FormalParams = { x.getClass() };
   _params = new Object[1];
   _params[0] = x;
   try {
     _method = this.getClass().getMethod("orig_m2",
                                     FormalParams);
   } catch(NoSuchMethodException e){
     System.out.println("Error: " + e.getMessage());
 };
   new Thread(this).start();
 return;
 }

 // The original programmer's methods
 public void orig_m1(){
   // ... m1's code
 }

 public void orig_m2(Integer x){
    // ... m2's code
 }
 
  // ... Other class' code
}
Listing 3: A Java class applying separate to a class's field
class A {
 public void m1(){
    //... m1's code
 }
}

class B {
   public static void main(String[] args) {
      separate A _a = new A();
  _a.m1();
 }
}

Listing 4: Separate behavior implementation for a class's field
import java.lang.reflect.*;
class A {
 // Elements for SEPARATE behavior attached to
  //  fields.
 synchronized public void separate_m1()  {
  try {
  new _Aux.callMethod(this.getClass().getDeclaredMethod("m1", new Class[0]),
                         new Object[0]) ; ???FMA - This line is too long !!!
   } catch(Exception e) {
     System.out.println("Error: " + e.getMessage());
   }
 }

   public class _Aux implements Runnable {
  private Method _method;
  private Object[] _params;
  private Object _ret;

  public void callMethod(Method _method,
                            Object[] _params)  {
   this._method = _method;
   this._params = _params;
   new Thread(this).start();
  }

  public void run() {
   try {
       _ret = _method.invoke(A.this,_params);
    } catch(Exception e){}
  }
 } // End of the inner class
 
 // Original class code
    public void m1() {
// ... m1's code
 }
}

class B {
  public static void main(String[] a) {
 A _a = new A();
 _a.separate_m1();
  }
}
Listing 5: Separate added to the set of the language's reserved words and literals
TOKEN :
{
... // All Java tokens plus ...
| < SEPARATE: "separate" >
}

Listing 6: Changes made to the Java language grammar
...
void TypeDeclaration() :
{}
{
  LOOKAHEAD( ( "abstract" | "final" | "public" |
               "separate" )* "class" )
  ClassDeclaration()
|
  InterfaceDeclaration()
|
  ";"
}

void ClassDeclaration() :
{}
{
  ( "abstract" | "final" | "public" | "separate" )*
  "class" <IDENTIFIER> [ "extends" Name() ]
  [ "implements" NameList() ]
  "{" ( ClassBodyDeclaration() )* "}"
}

void ClassFieldDeclaration() :
{}
{
  ( "public" | "protected" | "private"  | "static" |
    "final"  | "transient" | "volatile" | "separate" )*
  Type() VariableDeclarator() ( ","
         VariableDeclarator() )* ";"