| |
| Posted by Adam D. Ruppe in reply to fp | PermalinkReply |
|
Adam D. Ruppe
| On Wednesday, 4 March 2020 at 09:35:31 UTC, fp wrote:
> What is the correct way to declare/use the nested Inner class ?
Lemme hsow you the code first:
---
import std.stdio;
import arsd.jni;
final class Outer : JavaClass!("", Outer) {
@Import this();
}
@JavaName("Outer$Inner") // the name here has teh separator dot replaced with a $. note it is the same as the .class file if you need help figuring it out
final class Inner : JavaClass!("", Inner)
{
@Import this(Outer); // then add a constructor taking the outer class as an arg
@Import void printHello();
}
void main()
{
auto jvm = createJvm();
auto o = new Outer();
// and then construct it like this
Inner inner = new Inner(o);
inner.printHello();
}
---
Basically, direct inner classes hit a bunch of compiler bugs, so the way to make it work is to flatten it, taking the inner class out and declaring as if it was a top-level thing. Which is what the JVM does internally and that's why it works btw.
If you try to add a convenience method in Outer for it, you're liable to hit a "forward reference" error, which is again a compiler bug brought on by the recursive reflection done inside jni.d (I hit more compiler bugs working on this project than I have in the last 5 years of writing D combined).
As seen above, you don't need to declare them together, but if you do want to try, just be sure Inner is declared before Outer in the file. Then you can do a `final` method (not @Import) that just `return new .Inner(this);` or something like that.
But I think you'll probably have better luck keeping it simple and flattening like this.
|