Search in sources :

Example 1 with SourceFile

use of platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile in project L42 by ElvisResearchGroup.

the class Translator method runStringExc.

public Object runStringExc() throws CompilationError, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
    List<SourceFile> files = new ArrayList<>();
    for (String k : map.keySet()) {
        //"generated.Program42";
        files.add(new SourceFile(k, map.get(k)));
    //try{Files.write(Paths.get("/u/staff/servetto/git/L42/Tests/src/generated/"+k+".java"), map.get(k).getBytes());}catch (IOException _e) {throw new Error(_e);}
    //try{Files.write(Paths.get("C:/Users/user/git/L42/Tests/src/generated/"+k+".java"), map.get(k).getBytes());}catch (IOException _e) {throw new Error(_e);}
    }
    //System.out.println("Compilation Iteration ready to compile");
    MapClassLoader cl = ((Facade) Configuration.reduction).getLastLoader();
    Timer.activate("InMemoryJavaCompiler.compile");
    try {
        //can throw, no closure possible
        cl = InMemoryJavaCompiler.compile(cl == null ? ClassLoader.getSystemClassLoader() : cl, files);
        ((Facade) Configuration.reduction).setLastLoader(cl);
    } finally {
        Timer.deactivate("InMemoryJavaCompiler.compile");
    }
    //System.out.println("Compilation Iteration complete compilation, start class loading");
    Class<?> cl0 = cl.loadClass("generated." + this.mainName);
    //System.out.println("Compilation Iteration start method retrival");
    Method m0 = cl0.getDeclaredMethod("execute0");
    //System.out.println("Compilation Iteration ready to execute");
    assert Resources.getP() != null;
    Timer.activate("InMemoryJavaCompiler.execute");
    try {
        Object result = m0.invoke(null);
        //System.out.println("Compilation Iteration execution complete");
        return result;
    } finally {
        Timer.deactivate("InMemoryJavaCompiler.execute");
    }
}
Also used : Facade(reduction.Facade) ArrayList(java.util.ArrayList) MapClassLoader(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.MapClassLoader) SourceFile(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile) Method(java.lang.reflect.Method)

Example 2 with SourceFile

use of platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile in project L42 by ElvisResearchGroup.

the class CompilationInMemoryTest method test2.

@Test
public void test2() throws Throwable {
    SourceFile file = new SourceFile("math.Calculator", "package math;" + "class Calculator2 { " + "  public void testAdd() { " + "    System.out.println(200+300); " + "  }} " + "public class Calculator { " + "  public static void main(String[] args) { " + "    Calculator2 cal = new Calculator2(); " + "    cal.testAdd(); " + "  } " + "} ");
    List<SourceFile> files = Arrays.asList(file);
    ClassLoader classes = InMemoryJavaCompiler.compile(ClassLoader.getSystemClassLoader(), files);
    RunningUtils.runMain(classes, "math.Calculator");
    RunningUtils.runMainStrictSecurity(classes, "math.Calculator", 1000);
}
Also used : SourceFile(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile) Test(org.junit.Test)

Example 3 with SourceFile

use of platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile in project L42 by ElvisResearchGroup.

the class CompilationInMemoryTest method testSecurity2File.

@Test
public void testSecurity2File() throws Throwable {
    SourceFile[] file = { new SourceFile("math.Calculator", "package math;" + "class Calculator2 { " + "  public void testAdd() { " + "    System.out.println(200+300); " + "  }} "), new SourceFile("math.Calculator", "package math;" + "public class Calculator { " + "  public static void main(String[] args) { " + "    Calculator2 cal = new Calculator2(); " + "    cal.testAdd(); " + "  } " + "} ") };
    List<SourceFile> files = Arrays.asList(file);
    ClassLoader classes = InMemoryJavaCompiler.compile(ClassLoader.getSystemClassLoader(), files);
    RunningUtils.runMainStrictSecurity(classes, "math.Calculator", 1000);
}
Also used : SourceFile(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile) Test(org.junit.Test)

Example 4 with SourceFile

use of platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile in project L42 by ElvisResearchGroup.

the class TestCacheOnDisk method test1.

@Test
public void test1() throws Throwable {
    SourceFile fileA = new SourceFile("ab.A", "package ab;" + "public class A { " + "  public int a() {return 1;} " + "  public int a_b() { " + "    return a()+new B().b();" + "  }" + "} ");
    SourceFile fileB = new SourceFile("ab.B", "package ab;" + "public class B { " + "  public int b() {return 2;} " + "  public int a_b() { " + "    return b()+new A().a();" + "  }" + "} ");
    SourceFile fileM = new SourceFile("ab.Main", "package ab;" + "public class Main { " + "  public static int execute() { " + "    return new A().a_b()+new B().a_b();" + "  }" + "} ");
    SourceFile fileA2 = new SourceFile("ab.A2", "package ab;" + "public class A2 { " + "  public int a() {return 10;} " + "  public int a_b() { " + "    return a()+new B2().b()+new A().a_b();" + "  }" + "} ");
    SourceFile fileB2 = new SourceFile("ab.B2", "package ab;" + "public class B2 { " + "  public int b() {return 20;} " + "  public int a_b() { " + "    return b()+new A2().a()+new B().a_b();" + "  }" + "} ");
    SourceFile fileM2 = new SourceFile("ab.Main2", "package ab;" + "public class Main2 { " + "  public static int execute() { " + "    return new A2().a_b()+new B2().a_b();" + "  }" + "} ");
    List<SourceFile> files = Arrays.asList(fileA, fileB);
    MapClassLoader classes1 = InMemoryJavaCompiler.compile(ClassLoader.getSystemClassLoader(), files);
    HashMap<String, ClassFile> map = classes1.map();
    InMemoryJavaCompiler.compile(classes1, Arrays.asList(fileM));
    int res = (int) RunningUtils.runExecute(classes1, "ab.Main");
    Assert.assertEquals(6, res);
    Path path = Paths.get("src/" + this.getClass().getPackage().getName().replace(".", "/") + "/data1.bytes");
    classes1.saveOnFile(path);
    MapClassLoader classes2 = MapClassLoader.readFromFile(path, ClassLoader.getSystemClassLoader());
    InMemoryJavaCompiler.compile(classes2, Arrays.asList(fileA2, fileB2));
    InMemoryJavaCompiler.compile(classes2, Arrays.asList(fileM2));
    int res2 = (int) RunningUtils.runExecute(classes2, "ab.Main2");
    Assert.assertEquals(66, res2);
    try {
        RunningUtils.runExecute(classes1, "ab.Main2");
        Assert.fail();
    } catch (Error t) {
        Assert.assertTrue(t.getCause() instanceof ClassNotFoundException);
    }
}
Also used : Path(java.nio.file.Path) ClassFile(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.ClassFile) MapClassLoader(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.MapClassLoader) SourceFile(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile) Test(org.junit.Test)

Example 5 with SourceFile

use of platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile in project L42 by ElvisResearchGroup.

the class TestTwoClass method test1.

@Test
public void test1() throws Throwable {
    SourceFile file1 = new SourceFile("twoC.A", "package twoC;" + "public class A { " + "  public void testA() { " + "    System.out.println(200+300); " + "  }" + "  public static void main(String[] args) { " + "    B b=new B(); A a=new A();" + "    b.testB();  a.testA();" + "  } " + "} ");
    SourceFile file2 = new SourceFile("twoC.B", "package twoC;" + "public class B { " + "  public void testB() { " + "    System.out.println(20+30); " + "  }" + "  public static void main(String[] args) { " + "    B b=new B(); A a=new A();" + "    b.testB();  a.testA();" + "  } " + "} ");
    SourceFile file3 = new SourceFile("twoC.C", "package twoC;" + "public class C { " + "  public void testC() { " + "    System.out.println(2+3); " + "  }" + "  public static void main(String[] args) { " + "    B b=new B(); A a=new A(); C c=new C();" + "    b.testB();  a.testA(); c.testC();" + "  } " + "} ");
    List<SourceFile> files = Arrays.asList(file1, file2);
    List<SourceFile> files3 = Arrays.asList(file3);
    ClassLoader classes = InMemoryJavaCompiler.compile(ClassLoader.getSystemClassLoader(), files);
    RunningUtils.runMain(classes, "twoC.A");
    RunningUtils.runMain(classes, "twoC.B");
    RunningUtils.runMainStrictSecurity(classes, "twoC.A", 1000);
    RunningUtils.runMainStrictSecurity(classes, "twoC.B", 1000);
    ClassLoader classes3 = InMemoryJavaCompiler.compile(classes, files3);
    RunningUtils.runMain(classes3, "twoC.C");
    RunningUtils.runMainStrictSecurity(classes3, "twoC.C", 1000);
}
Also used : SourceFile(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile) Test(org.junit.Test)

Aggregations

SourceFile (platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile)9 Test (org.junit.Test)8 MapClassLoader (platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.MapClassLoader)2 Method (java.lang.reflect.Method)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 ClassFile (platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.ClassFile)1 Facade (reduction.Facade)1