#930 Using jstub ed fan classes in java project

jessevdam Mon 25 Jan 2010

To be able to call for example the fan compiler class from a java class without making use of reflection calls did I the following things.

I included the jstub ed jar and the sys.jar to my class path and used the following code

-MyClassLoader.java import fan.sys.FanClassLoader;

public class MyClassLoader extends FanClassLoader {

public Class<?> loadClass(String name) throws ClassNotFoundException
{
  return this.findClass(name);
}

}

-Main.java import fan.sys.FanClassLoader;

public class Main {

public static void main(String args[])
{
  System.setProperty("fan.home", "/home/jesse/programs/fantom");
  //Tell fantom to use the precompiled jars instead of generating them on the fly 
  //from the pods
  fan.sys.Sys.usePrecompiledOnly = true;
  //I load fan type in the pod using my class loader type, to make sure the 
  //load and init code on the pod is ran so all stuff in my pod get initialized
  MyClassLoader loader = new MyClassLoader();
  try
  {
    loader.loadClass("fan.compiler.CompilerInput");
  }
  catch(Exception e)
  {     
    e.printStackTrace();
  }
  //Call my main which make use of the compiler from the compiler.pod
  new TestMain();
}

}

If I would have my own classloader which loads my clasess from a jar or other url, then it would not need to do a precall to loader.loadClass("fan.compiler.CompilerInput");. But I my case I make use of a different class loader to load my class files. I would like to know whether there is a nicer way in doing this. If not then it would be nice there would be somethng like: Pod.preLoadPod(String podName) or the class file them self contain some code to init the pod when used for the first time.

That the fan system needs the original pods for the fan reflection system is good and ok with me. Maybe the addition of a stand alone jar which holds a pod file, which only the contains definitions and no code would be nice for the future.

brian Mon 25 Jan 2010

You'll always need the Fan meta-data for reflection. But eventually I think that could just be packaged up in the jar itself.

All of these sorts of scenarios should become fairly easy to build once we make the runtime more pluggable per ticket #867

Login or Signup to reply.