Search in sources :

Example 1 with JavaCode

use of abs.backend.java.codegeneration.JavaCode in project abstools by abstools.

the class JavaJob method generateJavaCode.

/**
 * Generates .java files (no .class files).
 * If 'product' is set, will flatten accordingly.
 * @param monitor - must not be null
 * @param path - where to add the modules / java-files
 * @param project - the ABS project
 * @throws IOException, if unable to create java files
 * @throws AbsJobException, if unable to generate java files
 * @throws JavaCodeGenerationException, if unable to generate java files
 * @throws CoreException
 * @throws NoModelException
 */
private void generateJavaCode(IProgressMonitor monitor, Path path, IProject project) throws AbsJobException, IOException, JavaCodeGenerationException, CoreException, NoModelException {
    assert monitor != null;
    monitor.subTask("Creating java source files");
    AbsNature nat = UtilityFunctions.getAbsNature(project);
    synchronized (nat.modelLock) {
        Model model = nat.getCompleteModel();
        if (model == null)
            throw new NoModelException();
        JavaCode javaCode = new JavaCode(path.toFile());
        if (product != null) {
            /* [stolz] Flattening for a product will mangle the model according to [ramus]...
                 */
            // work on a copy:
            model = model.treeCopyNoTransform();
            model.flushTreeCache();
            String productN = product.getName();
            try {
                model.flattenForProduct(productN);
                model.flushCache();
                if (model.hasErrors() || model.hasTypeErrors()) {
                    nat.createMarkers(model);
                    throw new AbsJobException("An ABS file in the project has type errors after applying deltas");
                }
            } catch (WrongProgramArgumentException e) {
                throw new AbsJobException(e);
            } catch (DeltaModellingException e) {
                throw new AbsJobException(e);
            }
        }
        // TODO: the second parameter toggles listener code creation (3
        // Java method calls per ABS statement); make this configurable
        model.generateJavaCode(javaCode, true);
        int countUnits = model.getNumCompilationUnit();
        if (countUnits == 0)
            throw new AbsJobException("No compilation unit found");
    }
}
Also used : NoModelException(org.absmodels.abs.plugin.internal.NoModelException) Model(abs.frontend.ast.Model) WrongProgramArgumentException(abs.common.WrongProgramArgumentException) JavaCode(abs.backend.java.codegeneration.JavaCode) AbsNature(org.absmodels.abs.plugin.builder.AbsNature) UtilityFunctions.getAbsNature(org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature) DeltaModellingException(abs.frontend.delta.DeltaModellingException) AbsJobException(org.absmodels.abs.plugin.exceptions.AbsJobException)

Example 2 with JavaCode

use of abs.backend.java.codegeneration.JavaCode in project abstools by abstools.

the class JavaBackendTest method assertValidJavaExecution.

void assertValidJavaExecution(boolean withStdLib, String... codeLines) throws Exception {
    StringBuilder absCode = new StringBuilder();
    for (String line : codeLines) {
        absCode.append(line);
        absCode.append("\n");
    }
    JavaCode javaCode = getJavaCode(absCode.toString(), withStdLib ? Config.WITH_STD_LIB : null);
    try {
        String genDir = javaCode.getSrcDir().getAbsolutePath() + "/gen/test";
        javaCode.compile("-classpath", "bin", "-d", genDir);
        final ABSRuntime r = makeAbsRuntime();
        r.enableDebugging(true);
        final boolean[] finished = new boolean[] { false };
        final List<ABSException> exceptions = Collections.synchronizedList(new ArrayList<ABSException>());
        r.addSystemObserver(new SystemObserver() {

            @Override
            public void systemStarted() {
            }

            @Override
            public void systemFinished() {
                synchronized (finished) {
                    finished[0] = true;
                    finished.notifyAll();
                }
            }

            @Override
            public void systemError(ABSException e) {
                exceptions.add(e);
            }

            @Override
            public void newCOGCreated(COGView cog, ObjectView initialObject) {
            }
        });
        r.start(new File(genDir), "Test.Main");
        while (!finished[0]) {
            synchronized (finished) {
                finished.wait(100);
            }
        }
        r.shutdown();
        for (ABSException e : exceptions) {
            throw e;
        }
    } catch (Exception e) {
        System.out.println(javaCode);
        throw e;
    } finally {
        javaCode.deleteCode();
    }
}
Also used : COGView(abs.backend.java.observing.COGView) SystemObserver(abs.backend.java.observing.SystemObserver) JavaCode(abs.backend.java.codegeneration.JavaCode) ObjectView(abs.backend.java.observing.ObjectView) JavaCodeGenerationException(abs.backend.java.codegeneration.JavaCodeGenerationException) ABSException(abs.backend.java.lib.runtime.ABSException) ABSRuntime(abs.backend.java.lib.runtime.ABSRuntime) ABSException(abs.backend.java.lib.runtime.ABSException)

Example 3 with JavaCode

use of abs.backend.java.codegeneration.JavaCode in project abstools by abstools.

the class JavaBackendTest method assertEvalEquals.

public void assertEvalEquals(String absCode, boolean value) throws Exception {
    JavaCode javaCode = getJavaCode(absCode, Config.WITH_STD_LIB);
    if (DEBUG)
        System.err.println(javaCode);
    boolean res = runJavaAndTestResult(javaCode, false);
    if (value != res) {
    // System.out.println(javaCode);
    }
    assertEquals(value, res);
}
Also used : JavaCode(abs.backend.java.codegeneration.JavaCode)

Example 4 with JavaCode

use of abs.backend.java.codegeneration.JavaCode in project abstools by abstools.

the class JavaDebuggingTest method assertOutputContains.

private void assertOutputContains(String absCode, String expectedOutput) throws Exception {
    JavaCode code = getJavaCode("module JavaTest;" + absCode, Config.WITH_STD_LIB);
    // System.out.println(java);
    String output = runJava(code, "-Dabs.debugger=" + TestDebugger.class.getName()).toString().trim();
    Assert.assertTrue("Expected to find " + expectedOutput + ", but output was:\n" + output, output.contains(expectedOutput));
}
Also used : JavaCode(abs.backend.java.codegeneration.JavaCode)

Example 5 with JavaCode

use of abs.backend.java.codegeneration.JavaCode in project abstools by abstools.

the class JavaObservationTest method assertOutputContains.

private void assertOutputContains(String absCode, String expectedOutput) throws Exception {
    JavaCode code = getJavaCode("module JavaTest;" + STDDATA + absCode, Config.WITH_STD_LIB);
    // System.out.println(java);
    String output = runJava(code, "-Dabs.systemobserver=" + TestSystemObserver.class.getName()).toString().trim();
    Assert.assertTrue("Expected to find " + expectedOutput + ", but output was:\n" + output, output.contains(expectedOutput));
}
Also used : JavaCode(abs.backend.java.codegeneration.JavaCode)

Aggregations

JavaCode (abs.backend.java.codegeneration.JavaCode)11 JavaCodeGenerationException (abs.backend.java.codegeneration.JavaCodeGenerationException)1 ABSException (abs.backend.java.lib.runtime.ABSException)1 ABSRuntime (abs.backend.java.lib.runtime.ABSRuntime)1 COGView (abs.backend.java.observing.COGView)1 ObjectView (abs.backend.java.observing.ObjectView)1 SystemObserver (abs.backend.java.observing.SystemObserver)1 WrongProgramArgumentException (abs.common.WrongProgramArgumentException)1 Model (abs.frontend.ast.Model)1 DeltaModellingException (abs.frontend.delta.DeltaModellingException)1 AbsNature (org.absmodels.abs.plugin.builder.AbsNature)1 AbsJobException (org.absmodels.abs.plugin.exceptions.AbsJobException)1 NoModelException (org.absmodels.abs.plugin.internal.NoModelException)1 UtilityFunctions.getAbsNature (org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature)1