Java Reference
In-Depth Information
A field reference resembles a static reference, with the addition of an object
instance that hosts the field. The expression for the object instance could
be a simple local reference, but it could have arbitrary complexity, including
method calls and other field and static references. Marker
41
therefore passes
the visitor to the subtree representing the object instance, so that code can be
generated to compute the object instance. The ensuing getfield instruction
has the same form as the getstatic instruction, but the execution of getfield
will use the object instance to find the specified field.
AST
JVM Instructions
FieldReferencing
;
AST and code for fetching the
name
;
field name from class MyClass:
;
;
n
o.name
;
;
Code generated for the instance
aload 4
LocalReferencing
o
;
Get the field's value
getfield Ljava/lang/String; MyClass/name
The code generated above assumes that the local reference o is in local 4 and
is of type java.lang.String. The visit initiated at Marker
41
is dispatched
to the LocalReferencingvisit method, which generates code to push local 4's
value onto the stack. The type of that reference should be MyClass.The
getfield instruction is then emitted to retrieve the value of field name from
instance o. The instruction specifies the expected type of the field reference:
java.lang.String.
/
Visitor code for Marker
11
/
procedure
visit
(ArrayReferencing
n
)
call
n
.
getArray()
.
accept(
this
)
42
call
n
.
getIndex()
.
accept(
this
)
43
(
n
.
getArray()
.
getType())
call
emit
A
rray
R
eference
44
end
An array reference contains two components, the name of an array and an
index value. In Java, an array name is always a reference to an array object.
In languages like C and C
, an array name can be a global, local, or heap
address. At Marker
42
we visit the subtree representing the array and load
++