Local variables
The ClassMaker methods to declare and access local variables are:
public Type Declare(String name, Class javaClass, int modifiers) throws ClassMakerException
public Type Get(String name) throws ClassMakerException
public Type Set(String name, Type type) throws ClassMakerException
The methods are overloaded to handle local variables, member variables and static variables.
| Java code | ClassMaker code |
{
int a;
int b;
a = 2
b = a
{
|
Begin();
Declare("a", int.class, 0);
Declare("b", int.class, 0);
Eval(Set("a", Literal(2)));
Eval(Set("b", Get("a")));
End();
|
Formal variables
ClassMaker uses the same methods to declare and access formal parameters.
public Type Declare(String name, Class javaClass, int modifiers) throws ClassMakerException
public Type Get(String name) throws ClassMakerException
public Type Set(String name, Type type) throws ClassMakerException
The only difference is that the Declare method is called between the
Method and Begin methods.
| Java code | ClassMaker code |
void exec(int a, int b)
{
int c;
c = a + b;
a = c;
}
|
Method("exec", void.class, 0);
Declare("a", int.class, 0);
Declare("b", int.class, 0);
Begin();
Declare("c", int.class, 0);
Eval(Set("c", Add(Get("a"), Get("b")));
Eval(Set("a", Get("c")));
End();
|
Member variables
ClassMaker uses the following methods to declare and access member variables.
public Type Declare(String name, Class javaClass, int modifiers) throws ClassMakerException
public Type Get(Type classType, String fieldName) throws ClassMakerException
public Type Set(Type classType, String name, Type type) throws ClassMakerException
The Declare method is the same as that used for local variables and method parameters
but it is used outside any method.
The Get and Set methods take an extra parameter which is a reference to the
appropriate Object.
To access a variable in the current instance, the this reference
must be provided. ClasMaker provides a special method This() to push a reference
to the current instance onto the stack.
To access a variable in another class instance, a reference to the appropriate object must
be pushed onto the stack. This is usually provided by one of the Get methods.
| Java code | ClassMaker code |
public int a;
protected int b;
private int c;
public void exec(MyClass other)
{
c = 2; // implicit this reference
b = c; // implicit this reference
a = other.a;
other.a = c;
}
|
Declare("a", int.class, ACC_PUBLIC);
Declare("b", int.class, ACC_PROTECTED);
Declare("c", int.class, ACC_PRIVATE);
Method("exec", void.class, ACC_PUBLIC);
Declare("other", getClassType(), 0);
Begin();
Eval(Set(This(), "c", Literal(2)));
Eval(Set(This(), "b", Get(This(), "c")));
Eval(Set(This(), "a", Get(Get("other"), "a")));
Eval(Set(Get("other"), "a", Get(This(), "c")));
End();
|
The other object is of type MyClass, which is assumed to be the class being generated.
This Type of the class being generated is available through the getClassType()
method of the ClassMaker instance.
Static variables
Static variables are declared and accessed in ClassMaker using the following methods.
public Type Declare(String name, Class javaClass, int modifiers) throws ClassMakerException
public Type Get(String className, String fieldName) throws ClassMakerException
public Type Set(String className, String name, Type type) throws ClassMakerException
The Declare method is the same as that used for all other declarations. For a variable
to be declared as static, the Declare method must be called outside any method declaration
and the modifiers parameter must include the ACC_STATIC bit-mask.
The Get and Set methods take the name of the class containing the static variable
instead of an object reference.
| Java code | ClassMaker code |
public static int s;
public void run()
{
int a;
MyClass.s = a;
a = MyClass.s;
}
|
Declare("s", int.class, ACC_PUBLIC | ACC_STATIC);
Method("exec", void.class, ACC_PUBLIC);
Begin();
Declare("a", int.class, 0);
Set("MyClass", "s", Get("a"));
Set("a", Get("MyClass", "s"));
End();
|
Array variables
FIXME - Example of array variables
Variable scope
FIXME - Variable scope is implemented so need an example