OpenJava Tutorial


3. API and Framework at a Glance

3.1. Overriding for Translation

As already describe before, we define a translation on a class by defining a new subclass of the metaclass openjava.mop.OJClass. The new metaclass overrides some methods in OJClass to change how to translate sourcecode related to the base class.

The main part of translation may be the translation on class definition. We call this kind of translation callee-side translation. For the convenience of explanation, suppose a base class MyObject extended by a metaclass MyClass, like following:


public class MyObject instantiates MyClass
{
   public String str;
   public int f() { return 1; }
}

To modify the source code at the callee-side of classes, we should override the following method:

public void translateDefinition()
    throws MOPException
in the metaclassMyClass, which is a subclass of the metaclassOJClass.

Furthermore, the part possible to translate is where the class is used. There are several part related to the use of class. i.e. object allocations, method calls, field accesses, .. We call this kind of translation caller-side translation. Similarly to callee-side translation, we should override appropriate methods in OJClass.

The following methods are for translation of object allocation parts.

public Expression expandAllocation(Evnironment,AllocationExpression)
    throws MOPException
is applied to explessions like: new MyObject(), and
public Expression expandArrayAllocation(Evnironment,ArrayAllocationExpression)
    throws MOPException
is applied for explessions like: new MyObject[10] or new MyObject[]{ null, null }

Here, suppose a variable obj is declared somewhere as follows:


MyObject obj;

The following methods are for translation of member access on either class or object.

public Expression expandFieldRead(Evnironment,FieldAccess)
    throws MOPException
is applied to explessions like: obj.str,
public Expression expandMethodCall(Evnironment,MethodCall)
    throws MOPException
is applied to explessions like: obj.f(), and
public Expression expandFieldWrite(Evnironment,AssignmentExpression)
    throws MOPException
is applied to explessions like: obj.str = "Hello"

The following methods are for translation of where the name of the class appears.

public Expression expandTypeName(Evnironment,TypeName)
    throws MOPException


public Expression expandArrayAccess(Evnironment,ArrayAccess)
    throws MOPException
public Expression expandAssignmentExpression(Evnironment,AssignmentExpression)
    throws MOPException
public Expression expandExpression(Evnironment,Expression)
    throws MOPException
public Statement expandVariableDeclaration(Evnironment,VariableDeclaration)
    throws MOPException

3.2. Getting Information of Class Metaobjects

In the overriding methods, we can get information about the class by using the methods defined in OJClass such as follows:

3.3. Modifying a Class Metaobject

In the overriding translateDefinition() method, we can modify the class definition by using the methods defined in OJClass such as follows:


Please send any message to :
mich@acm.org

Copyright (C) 1999 by Michaki Tatsubori.
Java(TM) is a trademark of Sun Microsystems, Inc.