use of org.kie.memorycompiler.JavaCompiler in project drools by kiegroup.
the class KnowledgeBuilderTest method testCompilerConfiguration.
@Test
public void testCompilerConfiguration() throws Exception {
// test default is eclipse jdt core
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
PackageDescr pkgDescr = new PackageDescr("org.drools.mvel.compiler.test");
builder.addPackage(pkgDescr);
DialectCompiletimeRegistry reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
final Field dialectField = builder.getClass().getDeclaredField("defaultDialect");
dialectField.setAccessible(true);
String dialectName = (String) dialectField.get(builder);
reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
Dialect dialect = reg.getDialect(dialectName);
final Field compilerField = dialect.getClass().getDeclaredField("compiler");
compilerField.setAccessible(true);
JavaCompiler compiler = (JavaCompiler) compilerField.get(dialect);
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
JavaForMvelDialectConfiguration javaConf = (JavaForMvelDialectConfiguration) conf.getDialectConfiguration("java");
switch(javaConf.getCompiler()) {
case NATIVE:
assertSame(NativeJavaCompiler.class, compiler.getClass());
break;
case ECLIPSE:
assertSame(EclipseJavaCompiler.class, compiler.getClass());
break;
default:
fail("Unrecognized java compiler");
}
// test eclipse jdt core with property settings and default source level
conf = new KnowledgeBuilderConfigurationImpl();
javaConf = (JavaForMvelDialectConfiguration) conf.getDialectConfiguration("java");
javaConf.setCompiler(JavaForMvelDialectConfiguration.CompilerType.ECLIPSE);
builder = new KnowledgeBuilderImpl(conf);
builder.addPackage(pkgDescr);
dialectName = (String) dialectField.get(builder);
reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
dialect = reg.getDialect(dialectName);
compiler = (JavaCompiler) compilerField.get(dialect);
assertSame(EclipseJavaCompiler.class, compiler.getClass());
}
use of org.kie.memorycompiler.JavaCompiler in project drools by kiegroup.
the class KieBuilderImpl method compileJavaClasses.
private void compileJavaClasses(JavaConfiguration javaConf, ClassLoader classLoader, List<String> javaFiles, String rootFolder) {
if (!javaFiles.isEmpty()) {
String[] sourceFiles = javaFiles.toArray(new String[javaFiles.size()]);
JavaCompiler javaCompiler = createCompiler(javaConf, rootFolder);
CompilationResult res = javaCompiler.compile(sourceFiles, srcMfs, trgMfs, classLoader);
for (CompilationProblem problem : res.getErrors()) {
results.addMessage(new CompilationProblemAdapter(problem));
}
for (CompilationProblem problem : res.getWarnings()) {
results.addMessage(new CompilationProblemAdapter(problem));
}
}
}
use of org.kie.memorycompiler.JavaCompiler in project drools by kiegroup.
the class CompilerBytecodeLoader method compileUnit.
public <T> T compileUnit(String cuPackage, String cuClass, CompilationUnit cu) {
try {
MemoryResourceReader pReader = new MemoryResourceReader();
pReader.add(cuPackage.replaceAll("\\.", "/") + "/" + cuClass + ".java", cu.toString().getBytes());
JavaCompiler compiler = createNativeCompiler();
MemoryFileSystem pStore = new MemoryFileSystem();
CompilationResult compilationResult = compiler.compile(new String[] { cuPackage.replaceAll("\\.", "/") + "/" + cuClass + ".java" }, pReader, pStore, this.getClass().getClassLoader());
LOG.debug("{}", Arrays.asList(compilationResult.getErrors()));
LOG.debug("{}", Arrays.asList(compilationResult.getWarnings()));
String fqnClassName = cuPackage + "." + cuClass;
Class<T> loaded = (Class<T>) new TemplateLoader(this.getClass().getClassLoader()).load(pStore, fqnClassName);
return loaded.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of org.kie.memorycompiler.JavaCompiler in project drools by kiegroup.
the class AbstractKnowledgeTest method compile.
public List<String> compile(KieModuleModel kproj, MemoryFileSystem srcMfs, MemoryFileSystem trgMfs, List<String> classes) {
for (KieBaseModel kbase : kproj.getKieBaseModels().values()) {
Folder srcFolder = srcMfs.getFolder("src/main/resources/" + kbase.getName());
Folder trgFolder = trgMfs.getFolder(kbase.getName());
copyFolder(srcMfs, srcFolder, trgMfs, trgFolder, kproj);
}
Folder srcFolder = srcMfs.getFolder("META-INF");
Folder trgFolder = trgMfs.getFolder("META-INF");
trgFolder.create();
copyFolder(srcMfs, srcFolder, trgMfs, trgFolder, kproj);
JavaCompiler compiler = new NativeJavaCompiler();
CompilationResult res = compiler.compile(classes.toArray(new String[classes.size()]), trgMfs, trgMfs);
if (res.getErrors().length > 0) {
fail(res.getErrors()[0].getMessage());
}
List<String> classes2 = new ArrayList<String>(classes.size());
for (String str : classes) {
classes2.add(filenameToClassname(str));
}
return classes2;
}
use of org.kie.memorycompiler.JavaCompiler in project drools by kiegroup.
the class KieBuilderImpl method createCompiler.
private JavaCompiler createCompiler(JavaConfiguration javaConf, String sourceFolder) {
JavaCompiler javaCompiler = JavaCompilerFactory.loadCompiler(javaConf);
javaCompiler.setSourceFolder(sourceFolder);
return javaCompiler;
}
Aggregations