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);
}
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;
}
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());
}
}
}
}
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);
}
}
Aggregations