Java Reference
In-Depth Information
5.2.1 Class Declarations
The codegen() method for JClassDeclaration does the following:
It computes the fully- qualified name for the class, taking any package name into
account.
It invokes an addClass() on the CLEmitter for adding the class header to the start
of the class file.
If there is no explicit constructor with no arguments defined for the class, it invokes
the private method codegenImplicitConstructor() to generate code for the implicit
constructor as required by the language.
It generates code for its members by sending the codegen() message to each of them.
If there are any static field initializations in the class declaration, then it invokes the
private method codegenClassInit() to generate the code necessary for defining a
static block, a block of code that is executed after a class is loaded.
In the case of our Factorial example, there is no explicit constructor, so one is generated.
The method, codegenImplicitConstructor() is invoked to generate the following JVM
code 2 :
public<init>();
Code:
Stack=1,Locals=1,Args_size=1
0:aload_0
1:invokespecial#8;//Methodjava/lang/Object."<init>":()V
4:return
The constructor is simply invoking the superclass constructor, that is, Object() . In Java,
such a constructor would look like
publicFactorial(){
this.super();
}
The Factorial example has one static field with an initialization:
staticintn=5;
During analysis, the initialization was transformed into an explicit assignment, which must
be executed after the Factorial class is loaded. Seasoned Java programmers will recognize
this to be in a static block; in Java, the static block would look like
static{
n=5;
}
and would occur as a member of Factorial . Of course, j-- does not have static blocks 3 ;
they may be represented in the JVM as, for example 4 ,
2 Actually, javap shows the name as Factorial() but when invoking the addMethod() method on
the CLEmitter , one passes the argument "<init>" for the constructor's name; a constructor is simply
an initialization method. The JVM expects this internal name.
3 Its implementation is left as an exercise.
4 Again, javap represents <clinit> as static , but in the argument to the addMethod() , method
is "<clinit>" ; <clinit> stands forclassinitialization. Again, the JVM expects this internal name.
 
Search WWH ::




Custom Search