Accessing an external instance of a nested class using JNI

In Java, I have an inner class that has its own method:

public class A {
    class B {
        public native void foo();
    }
}

The native method is implemented in JNI:

JNIEXPORT void JNICALL A_0024B_foo(JNIEnv* env, jobject b);

Obviously, the native function has access to B.this; it was transmitted as b. How to access a A.thisspanning external instance A?

+3
source share
2 answers

Using javap, I am sure the answer this$0

$ javac A.java
$ javap -s -p 'A$B'
Compiled from "A.java"
class A$B extends java.lang.Object{
final A this$0;
  Signature: LA;
A$B(A);
  Signature: (LA;)V
public native void foo();
  Signature: ()V
}

Note that if you are running a unix-style command line, you need quotation marks to interpret $ as the beginning of a shell variable.

( , ), , , native, .

+2

, jni- $ , , B.this .

0

All Articles