简体   繁体   中英

Create anonymous java class in C via JNI?

Given a java interface, I want to create, within a native C function, an instance of an anonymous java class that implements that interface. I also want this instance to have its own internal state, hidden from java. Is such a thing possible with JNI?

Implementing an anonymous class directly in JNI looks to be quite difficult although perhaps possible since the JNI function defineClass takes a raw stream of bytes as a class definition. If you are able to generate the appropriate class file byte code then there would appear to be no barrier (other than difficulty) in returning a custom generated class which implements the interfaces of your choosing.

Although an easier workaround may be to have the Java code generate an appropriate Proxy instance that takes a handler that delegates to your JNI code. The proxy is able to implement any interfaces and defer all action implementations to the handler which can then defer to JNI for the implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM