Getting a Class Loader

New to Java. I'd like to implement this line of code using the Invocation API:

ClassLoader clBFU = BinaryFileUtil.class.getClassLoader();

where BinaryFileUtil is a class that someone here wrote. Here's what I think is going on:

1. We have a static reference to BinaryFileUtil.

2. BinaryFileUtil has a static member class named "class".

3. The static inner class "class" has a static method called getClassLoader().

So in terms of JNI,

1. Get a reference to BinaryFileUtil.

2. Get the static field ID for BinaryFileUtil.class.

3. Get the static method ID for BinaryFileUtil.getClassLoader()

4. Call the static method BinaryFileUtil.getClassLoader() using CallStaticObjectMethod() and assign to jobject.

Is this more or less the correct roadmap? Here's the implementation:

// Get a reference to BinaryFileUtil (check)

jclass clsBFU = env->FindClass("foo/bar/BinaryFileUtil);

// Get the static field ID for BinaryFileUtil.class

[code]jjfieldID = env->GetStaticFieldID(clsBFU,"class", ?);

I'm stuck on the signature. I can't find any member named class in BinaryFileUtil, so I guess maybe it's inherited from Object?

Can some kind soul please explain to me how I can call the static method Foo.class.getClassLoader()?

Many Thanks!

[1466 byte] By [caffeinea] at [2007-11-26 13:56:58]
# 1

The BinaryFileUtil.class class literal is a java.lang.Class object.

It represent the BinaryFileUtil class itself.

The .class token does not represent a special field to access.

To get the classloader reference you should call getClassLoader() on

the BinaryFileUtil class reference:// get the BinaryFileUtil class reference (which is also an object reference)

jclass clsBFU = env->FindClass("foo/bar/BinaryFileUtil");

// get the Class class reference (all classes/interfaces are instances of java.lang.Class)

jclass clsClass = env->GetObjectClass(clsBFU);

// get the getClassLoader() id

jmethodID midGetClassLoader = env->GetMethodID(clsClass, "getClassLoader", "()Ljava/lang/ClassLoader;");

// call getClassLoader() on the BinaryFileUtil class reference (which is also an object reference)

jobject classLoader = env->CallObjectMethod(clsBFU, midGetClassLoader);

Regards

jfbrierea at 2007-7-8 1:36:48 > top of Java-index,Java HotSpot Virtual Machine,Specifications...