Esempio n. 1
0
 public static void main(String[] args) throws Exception {
   // creates the expression tree corresponding to
   //   exp(i) = i > 3 && 6 > i
   Exp exp = new And(new GT(new Var(0), new Cst(3)), new GT(new Cst(6), new Var(0)));
   // compiles this expression into an Expression class
   Compile main = new Compile();
   byte[] b = exp.compile("Example");
   FileOutputStream fos = new FileOutputStream("Example.class");
   fos.write(b);
   fos.close();
   Class expClass = main.defineClass("Example", b, 0, b.length);
   // instantiates this compiled expression class...
   Expression iexp = (Expression) expClass.newInstance();
   // ... and uses it to evaluate exp(0) to exp(9)
   for (int i = 0; i < 10; ++i) {
     boolean val = iexp.eval(i, 0) == 1;
     System.out.println(i + " > 3 && " + i + " < 6 = " + val);
   }
 }
Esempio n. 2
0
  @Test
  public void testCompile() throws Exception {

    Compilation compilation =
        Compile.compile(
            IO.load("test1.java"),
            IO.load("mixin.java"),
            "public class Book { String title; int x = 1234; } class Foo {}",
            "public class Bar extends Foo {}",
            "public class Fg extends Foo {}");

    Set<Class<?>> classes = compilation.loadClasses();
    eq(classes.size(), 10);

    Set<String> classNames = compilation.getClassNames();
    D.print(classNames);

    eq(Cls.classMap(classes).get("Main").getAnnotations().length, 2);

    Set<String> expectedClasses =
        U.set(
            "abc.Main",
            "abc.Main$1",
            "abc.Person",
            "abc.Person$Insider",
            "abc.PersonService",
            "Book",
            "Foo",
            "Bar",
            "Fg",
            "mixo.Mixin");
    eq(classNames, expectedClasses);

    for (String clsName : expectedClasses) {
      Class<?> cls = compilation.loadClass(clsName);
      notNull(cls);
      eq(cls.getName(), clsName);
    }

    Class<?> mainClass = compilation.loadClass("abc.Main");
    Method main = Cls.getMethod(mainClass, "main", String[].class);

    Cls.invoke(main, null, new Object[] {new String[] {}});
  }