0 Replies Latest reply on May 29, 2005 4:28 AM by richard aillet

    How to execute code known only at runtime related on a class

    richard aillet Newbie

      Hi,

      I'am wondering if it's possible to generate a Filter java class on the fly using javassist. The Filter class could have a method getResult() with a code block (=code condition which tests fields of a Foo instance) and returns the result of it as given below.
      Other requirement is: the code condition is known at runtime after instanciation of Foo and can change in the time.

      Public class Foo {
      int _a ;
      public Foo (int a){
      _a=a;
      }
      }
      public class Main {
      public static void main(String[] args) {

      // instanciate a new object
      Foo f = new Foo(args[0]).intValue());

      // generate a specific new class Filter on-the-fly,load, construct an instance knowing taking into account the code condition
      byte[] bytes = createFilter();
      Class clas = s_classLoader.load(cname, bytes);
      IAccess filter = null;
      f = (IAccess)clas.newInstance();

      System.out.println(f.getResult())
      }
      }


      Executing the Main, the expected result would be:

      %java Main 10 "a>5"
      true
      %java Main 2 "a>5"
      false

      What could be the design of this class generated on-the-fly ? Is there alternatives or another design ?

      Thanks in advance,