Java Reference
In-Depth Information
NoSuchFieldError
can also result from passing the wrong
jclass
refer-
ence, resulting in a lookup of the correct field name and signature but in a class
that does not contain that field.
Like most everything else in JNI, type signatures can appear messy, but they
are well-defined and straightforward. It is easiest to introduce the type signature
notation by way of an example. Suppose we have a Java class named
JNIDemo
like this:
package javatech.jni22;
class JNIDemo
{
static float a
-
static
-
float;
int some
-
int;
int[] array;
int[][] array2d;
String some
-
string;
MyCustomObject my
-
custom;
...
}
The type signatures follow a well-defined and deterministic formula. All the
Java primitive types have a single-letter designation -
I
for
int
,
F
for
float
,
D
for
double
, etc. So the first two fields are simple. The type signature of
a
-
static
-
float
is
“
F
„
and for
some
-
int
, the type signature is
“
I
„
.
With this information, we can get the field IDs of the
float
and
int
fields
as follows:
jfieldID a
-
static
-
float
-
fid =
jenv->GetStaticFieldID (cls, "a
-
static
-
float", "F");
if (a
-
static
-
float
-
fid == NULL) return NULL;
and
jfieldID some
-
int
-
fid =
jenv->GetFieldID (cls, "some
-
int", "I");
if (some
-
int
-
fid == NULL) return NULL;
Arrays of primitives use the same single-letter mapping as the primitive prefixed
with a left square bracket. So the signature of the
int[]
array is
“
[I
„
.Youcan
think of this
“
[I
„
notation meaning “array of type I.” Thus,
jfieldID array
-
fid
=
jenv-
>
GetFieldID (cls,
"
array
"
,
"
[I
"
);
A
float[]
array would obviously be
“
[F
„
and the 2D
int
array (
int[][]
on the Java side) has a type signature of
“
[[I
„
.
The type signature of an object field is more complicated. It begins with the
letter
L
followed by the fully qualified name of the object type with slashes (
/
)
Search WWH ::
Custom Search