Search in sources :

Example 1 with MapClassLoader

use of platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.MapClassLoader 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 MapClassLoader

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

the class Translator method translateProgram.

public static Translator translateProgram(Program p, ExpCore e) {
    Translator t = new Translator();
    t.map = new HashMap<>();
    //Resources.clearRes();
    Map<String, ClassB> map = new LinkedHashMap<String, ClassB>();
    Map<String, ClassB> mapNorm = new LinkedHashMap<String, ClassB>();
    addP(0, p, map, p);
    {
        StringBuilder res = new StringBuilder();
        t.mainName = Functions.freshName("Execute_", L42.usedNames);
        res.append("package generated;");
        res.append("@SuppressWarnings(\"all\")");
        res.append("public class " + t.mainName + "{\n");
        res.append("public static Object execute0()");
        TranslateExpression.of(e, res, Collections.emptyList());
        res.append("\n");
        res.append("}");
        t.map.put(t.mainName, res.toString());
    }
    MapClassLoader cl = ((Facade) Configuration.reduction).getLastLoader();
    for (String s : map.keySet()) {
        if (cl != null && cl.map().containsKey("generated." + s)) {
            continue;
        //ClassB cb=map.get(s);
        //if(!cb.getDoc1().getS().contains("##@")){continue;}
        }
        if (map.get(s).getPhase() != Phase.Coherent) {
            continue;
        }
        //Hope it work, it was normalized before
        ClassB cbNorm = map.get(s);
        assert cbNorm.getPhase() == Phase.Coherent;
        mapNorm.put(s, cbNorm);
    }
    for (String s : mapNorm.keySet()) {
        StringBuilder resi = new StringBuilder();
        resi.append("package generated;");
        resi.append("@SuppressWarnings(\"all\")");
        ClassB cbNorm = mapNorm.get(s);
        assert cbNorm.getPhase() == Phase.Coherent;
        TranslateClass.of(p, s, mapNorm.get(s), resi);
        String resiS = resi.toString();
        t.map.put(s, resiS);
    }
    return t;
}
Also used : Facade(reduction.Facade) MapClassLoader(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.MapClassLoader) ClassB(ast.ExpCore.ClassB) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with MapClassLoader

use of platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.MapClassLoader 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)

Aggregations

MapClassLoader (platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.MapClassLoader)3 SourceFile (platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile)2 Facade (reduction.Facade)2 ClassB (ast.ExpCore.ClassB)1 Method (java.lang.reflect.Method)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1 ClassFile (platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.ClassFile)1