use of org.drools.compiler.commons.jci.compilers.JavaCompiler in project drools by kiegroup.
the class KieBuilderImpl method compileJavaClasses.
private void compileJavaClasses(JavaDialectConfiguration javaConf, ClassLoader classLoader, List<String> javaFiles, String rootFolder, ResourceReader source) {
if (!javaFiles.isEmpty()) {
String[] sourceFiles = javaFiles.toArray(new String[javaFiles.size()]);
File dumpDir = javaConf.getPackageBuilderConfiguration().getDumpDir();
if (dumpDir != null) {
String dumpDirName;
try {
dumpDirName = dumpDir.getCanonicalPath().endsWith("/") ? dumpDir.getCanonicalPath() : dumpDir.getCanonicalPath() + "/";
for (String srcFile : sourceFiles) {
String baseName = (srcFile.startsWith(JAVA_ROOT) ? srcFile.substring(JAVA_ROOT.length()) : srcFile).replaceAll("/", ".");
String fname = dumpDirName + baseName;
byte[] srcData = source.getBytes(srcFile);
try (FileOutputStream fos = new FileOutputStream(fname)) {
fos.write(srcData);
} catch (IOException iox) {
results.addMessage(Level.WARNING, fname, "Unable to 'dump' the generated Java class: " + fname);
}
}
} catch (IOException e) {
results.addMessage(Level.WARNING, "", "Unable to get the 'dump directory for the generated Java classes");
}
}
JavaCompiler javaCompiler = createCompiler(javaConf, rootFolder);
CompilationResult res = javaCompiler.compile(sourceFiles, source, trgMfs, classLoader);
for (CompilationProblem problem : res.getErrors()) {
results.addMessage(problem);
}
for (CompilationProblem problem : res.getWarnings()) {
results.addMessage(problem);
}
}
}
use of org.drools.compiler.commons.jci.compilers.JavaCompiler in project drools by kiegroup.
the class KieBuilderImpl method compileJavaClasses.
private void compileJavaClasses(JavaDialectConfiguration 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(problem);
}
for (CompilationProblem problem : res.getWarnings()) {
results.addMessage(problem);
}
}
}
use of org.drools.compiler.commons.jci.compilers.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.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();
JavaDialectConfiguration javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
switch(javaConf.getCompiler()) {
case NATIVE:
assertSame(NativeJavaCompiler.class, compiler.getClass());
break;
case ECLIPSE:
assertSame(EclipseJavaCompiler.class, compiler.getClass());
break;
case JANINO:
assertSame(JaninoJavaCompiler.class, compiler.getClass());
break;
default:
fail("Unrecognized java compiler");
}
// test JANINO with property settings
conf = new KnowledgeBuilderConfigurationImpl();
javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
javaConf.setCompiler(JavaDialectConfiguration.CompilerType.JANINO);
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(JaninoJavaCompiler.class, compiler.getClass());
// test eclipse jdt core with property settings and default source level
conf = new KnowledgeBuilderConfigurationImpl();
javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
javaConf.setCompiler(JavaDialectConfiguration.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());
}
Aggregations