#1931 Extending Java classes - puzzler

geo Thu 28 Jun 2012

If I extend a Java class that has a default (no arg) constructor, and my Fantom sub-class also has a simple no arg default constructor new make():super(){} I am able to instantiate an instance of my Fantom class.

But, if I want my Fantom sub-class to take arguments in its constructor new make(Str s) : super(){echo(s)} and still call the default super constructor why do I get an exception when I try to instantiate an instance of my Fantom class?

Exception in Application constructor

TEST FAILED

sys::Err: java.lang.RuntimeException: Unable to construct Application instance: class fan.bgFantomFX.TestApp

com.sun.javafx.application.LauncherImpl.launchApplication1 (LauncherImpl.java:389)
com.sun.javafx.application.LauncherImpl.access$000 (LauncherImpl.java:47)
com.sun.javafx.application.LauncherImpl$1.run (LauncherImpl.java:115)
java.lang.Thread.run (Unknown)

Application is the Java class that I am extending, and TestApp is the sub-class

brian Fri 29 Jun 2012

That might be a bug, @geo do you have a small test case that doesn't require the whole fx thing? If so please email and lets work offline to investigate

geo Sat 30 Jun 2012

I think I have figured this out, it's not a problem with Fantom, it the fact that JavaFX creates instances of Application via reflection using the default (no params) constructor. I will have to think of another way to inject "params".

geo Sat 30 Jun 2012

This raises the question, is there a way to convert a Fantom Type to a Java Class at runtime?

geo Mon 2 Jul 2012

Would it be possible to have a method qnameNative on Type that returns the qualified name of the type as it would be on the platform as a Str?

e.g in Java

If you had a pod called MyPod, with a class called MyClass the equivalent Java class would (I believe) be fan.MyPod.MyClass

That way you could pass the Str to your Java code and use Class.forName(name) to work with it!

brian Mon 2 Jul 2012

You can use ->toClass to get Java class off Type:

fansh> OutStream#->toClass                                                                                                                                     
class fan.sys.OutStream       

Login or Signup to reply.