use of org.drools.compiler.compiler.io.memory.MemoryFileSystem in project drools by kiegroup.
the class JavaParserCompiler method compileAll.
public static Map<String, Class<?>> compileAll(KnowledgeBuilderImpl kbuilder, ClassLoader classLoader, List<GeneratedClassWithPackage> classes) {
if (classes == null || classes.isEmpty()) {
return Collections.emptyMap();
}
MemoryFileSystem srcMfs = new MemoryFileSystem();
MemoryFileSystem trgMfs = new MemoryFileSystem();
String[] resources = writeModel(classes, srcMfs);
CompilationResult resultCompilation = getCompiler().compile(resources, srcMfs, trgMfs, classLoader);
CompilationProblem[] errors = resultCompilation.getErrors();
if (errors.length != 0) {
classes.forEach(c -> logger.error(c.toString()));
for (CompilationProblem error : errors) {
kbuilder.addBuilderResult(new CompilationProblemErrorResult(error));
}
return Collections.emptyMap();
}
InternalClassLoader internalClassLoader = new InternalClassLoader(classLoader, trgMfs);
Map<String, Class<?>> result = new HashMap<>();
for (GeneratedClassWithPackage cls : classes) {
final String fullClassName = cls.getPackageName() + "." + cls.getGeneratedClass().getNameAsString();
try {
result.put(fullClassName, Class.forName(fullClassName, true, internalClassLoader));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
return result;
}
use of org.drools.compiler.compiler.io.memory.MemoryFileSystem in project drools by kiegroup.
the class MemoryFileTest method testFilePath.
@Test
public void testFilePath() {
FileSystem fs = new MemoryFileSystem();
Folder mres = fs.getFolder("src/main/java/org/domain");
File f1 = mres.getFile("MyClass.java");
assertEquals("src/main/java/org/domain/MyClass.java", f1.getPath().toPortableString());
}
use of org.drools.compiler.compiler.io.memory.MemoryFileSystem in project drools by kiegroup.
the class MemoryFileTest method testRelativeToParentFilePath.
@Test
public void testRelativeToParentFilePath() {
FileSystem fs = new MemoryFileSystem();
Folder mres = fs.getFolder("src/main/java/org/domain");
Folder f2 = fs.getFolder("src/main/java/org/domain/f1/f2/");
File f1 = mres.getFile("MyClass.java");
assertEquals("../../MyClass.java", f1.getPath().toRelativePortableString(f2.getPath()));
}
use of org.drools.compiler.compiler.io.memory.MemoryFileSystem in project drools by kiegroup.
the class MemoryFileTest method testFileCreation.
@Test
public void testFileCreation() throws IOException {
FileSystem fs = new MemoryFileSystem();
Folder mres = fs.getFolder("src/main/java/org/domain");
File f1 = mres.getFile("MyClass.java");
f1.create(new ByteArrayInputStream("ABC".getBytes()));
mres.create();
f1 = mres.getFile("MyClass.java");
assertTrue(f1.exists());
f1.create(new ByteArrayInputStream("ABC".getBytes()));
f1 = mres.getFile("MyClass.java");
assertTrue(f1.exists());
assertEquals("ABC", StringUtils.toString(f1.getContents()));
f1.create(new ByteArrayInputStream("ABC".getBytes()));
f1.setContents(new ByteArrayInputStream("DEF".getBytes()));
assertEquals("DEF", StringUtils.toString(f1.getContents()));
}
use of org.drools.compiler.compiler.io.memory.MemoryFileSystem in project drools by kiegroup.
the class MemoryKieModuleResourceProviderTest method testGetResourceForFolderWithFilesAndSubFolders.
@Test
public void testGetResourceForFolderWithFilesAndSubFolders() throws Exception {
MemoryFileSystem mfs = new MemoryFileSystem();
mfs.createFolder(new MemoryFolder(mfs, "src/main/java"));
mfs.createFolder(new MemoryFolder(mfs, "src/main/java/org"));
mfs.createFolder(new MemoryFolder(mfs, "src/main/java/com"));
mfs.setFileContents(new MemoryFile(mfs, "my-file1", mfs.getFolder("src/main/java")), new byte[10]);
mfs.setFileContents(new MemoryFile(mfs, "my-file2", mfs.getFolder("src/main/java")), new byte[10]);
MemoryKieModule mkm = new MemoryKieModule(releaseId, kieModuleModel, mfs);
ResourceProvider resourceProvider = mkm.createResourceProvider();
String folderContents = IOUtils.toString(resourceProvider.getResource("src/main/java").openStream());
Assertions.assertThat(folderContents).hasLineCount(4).contains("com", "org", "my-file1", "my-file2");
}
Aggregations