Java Reference
In-Depth Information
public<clinit>{};
Code:
Stack=2,Locals=0,Args_size=0
0:iconst_5
1:putstatic#19;//Fieldn:I
4:return
}
5.2.2 Method Declarations
The code generated for a JMethodDeclaration is pretty simple:
publicvoidcodegen(CLEmitteroutput){
output.addMethod(mods,name,descriptor,null,false);
if(body!=null){
body.codegen(output);
}
//AddimplicitRETURN
if(returnType==Type.VOID){
output.addNoArgInstruction(RETURN);
}
}
It generates code for the method header, the body and then, for void methods, an implicit
return-statement. Of course, the return may be superfluous if one already exits in the source
code, but any good optimizer would remove the extra one.
5.2.3 Constructor Declarations
A constructor declaration is handled very much like a method declaration, but requires two
additional tasks:
1. After the method header <init> has been emitted, JConstructorDeclaration 's
codegen() looks to see if a super class constructor has been explicitly invoked:
if(!invokesConstructor){
output.addNoArgInstruction(ALOAD_0);
output.addMemberAccessInstruction(INVOKESPECIAL,
((JTypeDecl)context.classContext().definition())
.superType().jvmName(),"<init>","()V");
}
The ag invokesConstructor is set to false by default and is set to true during
analysis of the body if the first statement in that body is an invocation of the super
class constructor.
2. Any instance field initializations (after analysis, represented as assignments) are gen-
erated:
for(JFieldDeclarationfield:definingClass
.instanceFieldInitializations()){
field.codegenInitializations(output);
}
Notice that JConstructorDeclaration extends JMethodDeclaration .
 
Search WWH ::




Custom Search