Java Reference
In-Depth Information
are used much like the analogous
GetFieldID()
and
GetStaticFieldID()
functions. Instead of a field type signature, they are passed a method signature.
22.9.1 The method ID and method signature
Method signatures use an extension of the notation described in Section 22.8.1
for type signatures. A method signature begins with a parenthesized list of its
parameters, each using the type signature notation. After the closing paren-
thesis appears the return type of the method (in type signature form). There
are no commas or other separators between the parameter types. For exam-
ple, the signature
“
(II)F
„
describes a method that takes two
int
parame-
ters and returns a
float
- such as
float some
-
method (int a, int
b)
. The letter
V
is used for a void return type. If there are no parameters,
then the method descriptor begins with
“
()
„
, not
“
(V)
„
,which is an error
since there is no void type in Java. The same
“
L
„
notation is used for object
types and
“
[
„
notation for arrays. For example, a method that takes an array
of Java
String
types as a parameter and returns a
String
is described as
“
([Ljava/lang/String;)Ljava/lang/String;
„
.Aswith type signa-
tures, the easiest and most error-free way to determine method signatures is to use
javap -s
as described above. If we add the following method to our
JNIDemo
class:
int callback (int x)
{
return 2*x;
}
that method's signature is
“
(I)I
„
either by inspection or by examining the output
of
javap
.
To get the method ID, we use
jmethodID callback
-
mid
=
jenv-
>
GetMethodID (cls,
"
callback
"
,
"
(I)I
"
);
The function arguments are a
jclass
reference to the class where the method
resides, the name of the method, and the method's signature. There is a corre-
sponding
GetStaticMethodID()
function for static methods.
GetMethodID()
can fail in three ways, the most likely being
NoSuchMe-
thodError
in which no method with the given name and signature can be
found in the class referenced by
cls
, most likely due to a mispelled name or a
bad signature. The other two possible error conditions are
ExceptionInIni-
tializerError
and
OutOfMemoryError
.Ifany of these occur, the function
returns
NULL
which we must handle in the usual way:
if (callback
-
mid
==
NULL)
return NULL;
Search WWH ::
Custom Search