Java Reference
In-Depth Information
}
//Computedescriptor
descriptor="(";
for(JFormalParameterparam:params){
descriptor+=param.type().toDescriptor();
}
descriptor+=")"+returnType.toDescriptor();
//Generatethemethodwithanemptybody(fornow)
partialCodegen(context,partial);
}
Basically, preAnalyze() does the following in a method declaration:
1. It resolves the types of its formal parameters and its return type.
2. It checks that any abstract modifier is proper.
3. It computes the method descriptor, which codies the method's signature as a string 4 .
For example, in the Factorial program above,
Method factorial() has the descriptor (I)I , which indicates a method taking
an int for an argument and returning an int result, and
Method main() has the descriptor ([Ljava.lang.String;)V , which indicates a
method taking a String[] argument and not returning anything (that is, a void
return type).
4. Finally, it calls upon partialCodegen( ) to generate code for the method, but without
the body. So the Class object that is generated for the enclosing class declaration has,
after pre-analysis, at least the interface information for methods, including the types
of parameters and the return type.
The code for partialCodegen() is as follows:
publicvoidpartialCodegen(Contextcontext,CLEmitterpartial){
//Generateamethodwithanemptybody;needareturnto
//maketheclassverifierhappy.
partial.addMethod(mods,name,descriptor,null,false);
//AddimplicitRETURN
if(returnType==Type.VOID){
partial.addNoArgInstruction(RETURN);
}
elseif(returnType==Type.INT||
returnType==Type.BOOLEAN||
returnType==Type.CHAR){
partial.addNoArgInstruction(ICONST_0);
partial.addNoArgInstruction(IRETURN);
}
else{
//Areferencetype.
partial.addNoArgInstruction(ACONST_NULL);
partial.addNoArgInstruction(ARETURN);
}
}
4 Method descriptors are discussed in Appendix D.
 
Search WWH ::




Custom Search