Java Reference
In-Depth Information
publicvoidaddMethod(ArrayList<String>accessFlags,
Stringname,
Stringdescriptor,
ArrayList<String>exceptions,
booleanisSynthetic)
where
accessFlags
is a list of method access and property flags,
name
is the name
9
of the
method,
descriptor
is the method descriptor,
exceptions
is a list of exceptions in internal
form that this method throws, and
isSythetic
specifies whether the method is created by
the compiler.
For example, one may add a method using
accessFlags.add("public");
output.addMethod(accessFlags,"factorial","(I)I",exceptions,false);
where
accessFlags
is a list of method access and property ags, \factorial" is the name
10
of the method, \(I)I" is the method descriptor,
exceptions
is a list of exceptions in internal
form that this method throws, and the argument
false
indicates that the method appears
in source code.
A comment on the method descriptor is warranted. The method descriptor describes the
method's signature in a format internal to the JVM. The
I
is the internal type descriptor for
the primitive type
.
, so the \(I)I" species that the method takes one integer argument
and returns a value having integer type.
Add Exception Handlers to Method
An exception handler to code a
try-catch
block can be added to a method by sending to
the
CLEmitter
instance the following message:
publicvoidaddExceptionHandler(StringstartLabel,
StringendLabel,
StringhandlerLabel,
StringcatchType)
where
startLabel
marks the beginning of the
try
block,
endLabel
marks the end of the
try
block,
handlerLabel
marks the beginning of a
catch
block, and
catchType
specifies
the exception that is to be caught in internal form. If
catchType
is
null
, this exception
handler is called for all exceptions; this is used to implement
finally
.
createLabel()
and
addLabel(Stringlabel)
can be invoked on the
CLEmitter
in-
stance to create unique labels and for adding them to mark instructions in the code indi-
cating where to jump to.
A method can specify as many exception handlers as there are exceptions that are being
caught in the method.
Add Optional Method, Field, Class, Code Attributes
Attributes are used in the
ClassFile
(
CLFile
),
field_info
(CLFieldInfo),
method_info
(
CLMethodInfo
), and
Code_attribute
(
CLCodeAttribute
) structures of the class file
format. While there are many kinds of attributes, only some are mandatory; these
include
InnerClasses_attribute
(class attribute),
Synthetic_attribute
(class, field,
and method attribute),
Code_attribute
(method attribute), and
Exceptions_attribute
(method attribute).
CLEmitter
implicitly adds the required attributes to the appropriate structures. The
9
Instance initializers must have the name
<init>
and static initializers must have the name
<clinit>
.
10
Instance constructors must have the name
<init>
.
Search WWH ::
Custom Search