Search in sources :

Example 1 with MemoryByteCode

use of org.snt.inmemantlr.memobjects.MemoryByteCode in project inmemantlr by julianthome.

the class SpecialClassLoader method findClass.

/**
 * find a class that is already loaded
 *
 * @param name class name
 * @return the actual class
 * @throws ClassNotFoundException if the class could not be found
 */
protected Class<?> findClass(String name) throws ClassNotFoundException {
    MemoryByteCode mbc = m.get(name);
    if (mbc == null) {
        mbc = m.get(name.replace(".", "/"));
        if (mbc == null) {
            LOGGER.error("Could not find {}", name);
            return super.findClass(name);
        }
    }
    byte[] bseq = mbc.getBytes();
    return defineClass(name, bseq, 0, bseq.length);
}
Also used : MemoryByteCode(org.snt.inmemantlr.memobjects.MemoryByteCode)

Example 2 with MemoryByteCode

use of org.snt.inmemantlr.memobjects.MemoryByteCode in project inmemantlr by julianthome.

the class SpecialJavaFileManager method getJavaFileForOutput.

/**
 * get a java file (memory byte code)
 *
 * @param location path
 * @param name     filename
 * @param kind     file kind
 * @param sibling  file sibling
 * @return memory byte code object
 * @throws IOException if an error occurs getting the java file
 */
@Override
public JavaFileObject getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
    MemoryByteCode mbc = new MemoryByteCode(name);
    // bookkeeping of memory bytecode
    mb.put(mbc.getClassName(), mbc);
    xcl.addClass(mbc);
    return mbc;
}
Also used : MemoryByteCode(org.snt.inmemantlr.memobjects.MemoryByteCode)

Example 3 with MemoryByteCode

use of org.snt.inmemantlr.memobjects.MemoryByteCode in project inmemantlr by julianthome.

the class TestMemObjects method testAntlrObjectAccess.

@Test
public void testAntlrObjectAccess() {
    GenericParser gp = null;
    try {
        gp = new GenericParser(grammar);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Assertions.assertNotNull(gp);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    Assertions.assertTrue(compile);
    String s = FileUtils.loadFileContent(sfile.getAbsolutePath());
    Assertions.assertTrue(s != null && !s.isEmpty());
    MemoryTupleSet set = gp.getAllCompiledObjects();
    Assertions.assertTrue(set != null && set.size() == 4);
    for (MemoryTuple tup : set) {
        LOGGER.debug("tuple name {}", tup.getClassName());
        // for printing the source code
        LOGGER.debug("source {}", tup.getSource().getClassName());
        // for printing the byte code
        for (MemoryByteCode mc : tup.getByteCodeObjects()) {
            Objects.requireNonNull(mc, "MemoryByteCode must not be null");
            LOGGER.debug("bc name: {}", mc.getClassName());
            if (!mc.isInnerClass()) {
                mc.getClassName().equals(tup.getSource().getClassName());
            } else {
                mc.getClassName().startsWith(tup.getSource().getClassName());
            }
        }
    }
}
Also used : MemoryByteCode(org.snt.inmemantlr.memobjects.MemoryByteCode) MemoryTupleSet(org.snt.inmemantlr.memobjects.MemoryTupleSet) MemoryTuple(org.snt.inmemantlr.memobjects.MemoryTuple) FileNotFoundException(java.io.FileNotFoundException) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 4 with MemoryByteCode

use of org.snt.inmemantlr.memobjects.MemoryByteCode in project inmemantlr by julianthome.

the class StringCompiler method compile.

/**
 * do the compilation for the antlr artifacts
 * @param units string code generation pipeline
 * @param oprov compiler option provider
 * @throws CompilationErrorException if the compilation was not successful
 */
public void compile(Set<CunitProvider> units, CompilerOptionsProvider oprov) throws CompilationErrorException {
    JavaCompiler javac = new EclipseCompiler();
    StandardJavaFileManager sjfm = javac.getStandardFileManager(null, null, null);
    SpecialJavaFileManager fileManager = new SpecialJavaFileManager(sjfm, cl);
    List<MemorySource> cunit = new ArrayList<>();
    Set<MemorySource> mset = new HashSet<>();
    for (CunitProvider sc : units) {
        cunit.addAll(sc.getItems());
        for (MemorySource ms : sc.getItems()) {
            LOGGER.debug(ms.toString());
        }
    }
    mset.addAll(cunit);
    DiagnosticListener<? super JavaFileObject> dlistener = null;
    Iterable<String> classes = null;
    Writer out = new StringWriter();
    List<String> optionList = new ArrayList<>();
    optionList.addAll(oprov.getOptions());
    JavaCompiler.CompilationTask compile = javac.getTask(out, fileManager, dlistener, optionList, classes, cunit);
    boolean ret = compile.call();
    if (!ret) {
        throw new CompilationErrorException(out.toString());
    }
    // the corresponding byte code
    for (MemorySource ms : mset) {
        Set<MemoryByteCode> mb = fileManager.getByteCodeFromClass(ms.getClassName());
        if (mb.size() == 0)
            throw new IllegalArgumentException("MemoryByteCode must not be empty");
        // book keeping of source-bytecode tuples
        mt.addMemoryTuple(ms, mb);
    }
}
Also used : CompilationErrorException(org.snt.inmemantlr.exceptions.CompilationErrorException) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) StringWriter(java.io.StringWriter) MemoryByteCode(org.snt.inmemantlr.memobjects.MemoryByteCode) MemorySource(org.snt.inmemantlr.memobjects.MemorySource) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

MemoryByteCode (org.snt.inmemantlr.memobjects.MemoryByteCode)4 FileNotFoundException (java.io.FileNotFoundException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 EclipseCompiler (org.eclipse.jdt.internal.compiler.tool.EclipseCompiler)1 Test (org.junit.jupiter.api.Test)1 GenericParser (org.snt.inmemantlr.GenericParser)1 CompilationErrorException (org.snt.inmemantlr.exceptions.CompilationErrorException)1 MemorySource (org.snt.inmemantlr.memobjects.MemorySource)1 MemoryTuple (org.snt.inmemantlr.memobjects.MemoryTuple)1 MemoryTupleSet (org.snt.inmemantlr.memobjects.MemoryTupleSet)1