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:
in the metaclassMyClass, which is a subclass of the metaclassOJClass.public void translateDefinition() throws MOPException
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.
is applied to explessions like:public Expression expandAllocation(Evnironment,AllocationExpression) throws MOPException
new
MyObject()
, and
is applied for explessions like:public Expression expandArrayAllocation(Evnironment,ArrayAllocationExpression) throws MOPException
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.
is applied to explessions like:public Expression expandFieldRead(Evnironment,FieldAccess) throws MOPException
obj.str
,
is applied to explessions like:public Expression expandMethodCall(Evnironment,MethodCall) throws MOPException
obj.f()
, and
is applied to explessions like:public Expression expandFieldWrite(Evnironment,AssignmentExpression) throws MOPException
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
In the overriding methods, we can get information about the class by using the methods defined in OJClass such as follows:
public String getName()
public OJClass getSuperclass()
public OJClass[] getInterfaces()
public OJField[] getDeclaredFields()
public OJField[] getFields(OJClass situation)
public OJMethod[] getDeclaredMethods()
public OJMethod[] getMethods(OJClass situation)
public OJConstructor[] getDeclaredConstructors()
public OJConstructor[] getConstructors(OJClass situation)
public OJField getField(String name)
public OJMethod getMethod(String name,OJClass[] parameterTypes)
public OJConstructor getConstructor(OJClass[] parameterTypes)
In the overriding translateDefinition() method, we can modify the class definition by using the methods defined in OJClass such as follows:
protected OJField addField(OJField field)
protected OJField removeField(OJField field)
protected OJMethod addMethod(OJMethod method)
protected OJMethod removeMethod(OJMethod method)
protected OJConstructor addConstructor(OJConstructor constr)
protected OJConstructor removeConstructor(OJConstructor constr)